<?php

namespace Modules\Vrc\Http\Controllers;

use Illuminate\Http\Request;
use Modules\Vrc\Entities\VrcServiceCategory;
use Yajra\DataTables\Facades\DataTables;

class ServiceCategoryController extends VrcBaseController
{
    public function index()
    {
        $this->guard('vrc.service.manage');

        if (request()->ajax()) {
            $cats = VrcServiceCategory::where('business_id', $this->businessId())
                ->withCount('services')
                ->select(['id', 'name', 'name_ar', 'is_active']);

            return DataTables::of($cats)
                ->editColumn('is_active', fn ($c) => $c->is_active
                    ? '<span class="label label-success">' . __('vrc::lang.active') . '</span>'
                    : '<span class="label label-default">' . __('vrc::lang.inactive') . '</span>')
                ->addColumn('action', fn ($c) => '<button data-href="' . route('vrc.service-categories.edit', $c->id) . '" class="btn btn-xs btn-primary vrc-modal-btn"><i class="fa fa-edit"></i> ' . __('messages.edit') . '</button> '
                    . '<button data-href="' . route('vrc.service-categories.destroy', $c->id) . '" class="btn btn-xs btn-danger vrc-delete"><i class="fa fa-trash"></i></button>')
                ->rawColumns(['is_active', 'action'])
                ->make(true);
        }

        return view('vrc::service_categories.index');
    }

    public function create()
    {
        $this->guard('vrc.service.manage');
        return view('vrc::service_categories.create');
    }

    public function store(Request $request)
    {
        $this->guard('vrc.service.manage');
        $data = $this->rules($request);
        try {
            $data['business_id'] = $this->businessId();
            $data['is_active'] = $request->boolean('is_active', true);
            VrcServiceCategory::create($data);
            return $this->ok(__('vrc::lang.added_success'));
        } catch (\Exception $e) {
            \Log::emergency($e->getMessage());
            return $this->fail();
        }
    }

    public function edit($id)
    {
        $this->guard('vrc.service.manage');
        $category = VrcServiceCategory::where('business_id', $this->businessId())->findOrFail($id);
        return view('vrc::service_categories.edit', compact('category'));
    }

    public function update(Request $request, $id)
    {
        $this->guard('vrc.service.manage');
        $data = $this->rules($request);
        try {
            $cat = VrcServiceCategory::where('business_id', $this->businessId())->findOrFail($id);
            $data['is_active'] = $request->boolean('is_active', true);
            $cat->update($data);
            return $this->ok(__('vrc::lang.updated_success'));
        } catch (\Exception $e) {
            \Log::emergency($e->getMessage());
            return $this->fail();
        }
    }

    public function destroy($id)
    {
        $this->guard('vrc.service.manage');
        try {
            VrcServiceCategory::where('business_id', $this->businessId())->findOrFail($id)->delete();
            return $this->ok(__('vrc::lang.deleted_success'));
        } catch (\Exception $e) {
            \Log::emergency($e->getMessage());
            return $this->fail();
        }
    }

    protected function rules(Request $request): array
    {
        return $this->validate($request, [
            'name'      => 'required|string|max:191',
            'name_ar'   => 'nullable|string|max:191',
            'parent_id' => 'nullable|integer',
            'is_active' => 'nullable',
        ]);
    }
}
