<?php

namespace App\Http\Controllers\Services;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;
use App\Models\Services\SchServiceGroup;
use Exception;
use Illuminate\Queue\QueueManager;
use Illuminate\Support\Facades\DB;
class GroupsController extends Controller
{
    public function __construct()
    {
        $this->middleware('auth');
    }

    public function group()
    {
        return view('services.group');
    }
    /**
     * Summary of create department
     * Author: kaysar
     * Date: 08-Aug-2021
     * @param Request $data
     * @return \Illuminate\Http\JsonResponse
     */
    public function createGroup(Request $data)
    {
        try {
            $validator = Validator::make($data->toArray(), [
                'name' => ['required', 'string', 'max:200', 'unique:sch_service_groups'],
            ]);

            if (!$validator->fails()) {
                $data['created_by'] = auth()->id();
                $data['modified_by'] = auth()->id();

                SchServiceGroup::create($data->all());
                return $this->apiResponse(['status' => '1', 'data' => ''], 200);
            }
            return $this->apiResponse(['status' => '500', 'data' => $validator->errors()], 400);
        } catch (Exception $ex) {
            return $this->apiResponse(['status' => '501', 'data' => $ex], 400);
        }
    }

    /**
     * Summary of update department
     * Author: kaysar
     * Date: 08-Aug-2021
     * @param Request $data
     * @return \Illuminate\Http\JsonResponse
     */
    public function updateGroup(Request $data)
    {
        try {
            $validator = Validator::make($data->toArray(), [
                'name' => ['required', 'string', 'max:200'],
            ]);
            if (!$validator->fails()) {
                SchServiceGroup::where('id', $data->id)->update([
                    'name' => $data->name,
                    'created_by' => $data->created_by,
                    'modified_by' => $data->modified_by,
                ]);
                return $this->apiResponse(['status' => '1', 'data' => ''], 200);
            }
            return $this->apiResponse(['status' => '500', 'data' => $validator->errors()], 400);
        } catch (Exception $ex) {
            return $this->apiResponse(['status' => '501', 'data' => $ex], 400);
        }
    }
    /**
     * Summary of delete Department
     * Author: Kaysar
     * Date: 8-Aug-2021
     * @param Request $data
     * @return \Illuminate\Http\JsonResponse
     */
    public function deleteGroup(Request $data)
    {
        try {

            $rtr = SchServiceGroup::where('id', $data->id)->delete();
            return $this->apiResponse(['status' => '1', 'data' => $rtr], 200);
        } catch (Exception $ex) {
            return $this->apiResponse(['status' => '501', 'data' => $ex], 400);
        }
    }
    /**
     * Summary of get brandepartment list
     * Author: Kaysar
     * Date: 8-Aug-2021
     * @return \Illuminate\Http\JsonResponse
     */
    public function getGrouptList()
    {
        try {
 $data = DB::select(DB::raw('
    SELECT
        sch_service_groups.id,
        sch_service_groups.name,
        COUNT(sch_service_categories.group_id) as total_service
    FROM
        sch_service_groups
    LEFT JOIN
        sch_service_categories ON sch_service_groups.id = sch_service_categories.group_id
    GROUP BY
        sch_service_groups.id, sch_service_groups.name
'));

            return $this->apiResponse(['status' => '1', 'data' => $data], 200);
        } catch (QueueManager $qx) {
            return $this->apiResponse(['status' => '403', 'data' => $qx], 400);
        }
    }
}
