<?php

namespace Modules\Vrc\Http\Controllers;

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

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

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

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

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

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

    public function store(Request $request)
    {
        $this->guard('vrc.vehicle.manage');
        $data = $this->rules($request);
        try {
            $data['business_id'] = $this->businessId();
            $data['is_active'] = $request->boolean('is_active', true);
            VrcVehicleMake::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.vehicle.manage');
        $make = VrcVehicleMake::where('business_id', $this->businessId())->findOrFail($id);
        return view('vrc::makes.edit', compact('make'));
    }

    public function update(Request $request, $id)
    {
        $this->guard('vrc.vehicle.manage');
        $data = $this->rules($request);
        try {
            $make = VrcVehicleMake::where('business_id', $this->businessId())->findOrFail($id);
            $data['is_active'] = $request->boolean('is_active', true);
            $make->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.vehicle.manage');
        try {
            VrcVehicleMake::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',
            'is_active' => 'nullable',
        ]);
    }
}
