<?php

namespace Modules\VetClinic\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Routing\Controller;
use Modules\VetClinic\Entities\VcAppointmentReason;
use Yajra\DataTables\Facades\DataTables;

class VcAppointmentReasonController extends Controller
{
    public function index()
    {
        if (!auth()->user()->can('vet_clinic.access_settings')) {
            abort(403, 'Unauthorized action.');
        }

        return view('vetClinic::settings.appointment-reasons');
    }

    public function list(Request $request)
    {
        if (!auth()->user()->can('vet_clinic.access_settings')) {
            abort(403, 'Unauthorized action.');
        }

        $businessId = request()->session()->get('user.business_id');
        $query = VcAppointmentReason::forBusiness($businessId)->orderBy('sort_order')->orderBy('name');

        return DataTables::of($query)
            ->addColumn('status_badge', function ($row) {
                $color = $row->status === 'active' ? 'success' : 'danger';
                return '<span class="label label-' . $color . '">' . ucfirst($row->status) . '</span>';
            })
            ->addColumn('action', function ($row) {
                $edit = '<button class="btn btn-xs btn-primary btn-edit-reason" data-id="' . $row->id . '"><i class="fa fa-edit"></i></button> ';
                $del  = '<button class="btn btn-xs btn-danger btn-delete-reason" data-id="' . $row->id . '"><i class="fa fa-trash"></i></button>';
                return $edit . $del;
            })
            ->rawColumns(['action', 'status_badge'])
            ->make(true);
    }

    public function store(Request $request)
    {
        if (!auth()->user()->can('vet_clinic.access_settings')) {
            abort(403, 'Unauthorized action.');
        }

        $request->validate(['name' => 'required|string|max:150']);

        $businessId = request()->session()->get('user.business_id');
        VcAppointmentReason::create([
            'business_id' => $businessId,
            'name'        => $request->name,
            'sort_order'  => (int) $request->input('sort_order', 0),
            'status'      => $request->input('status', 'active'),
        ]);

        return response()->json(['success' => true, 'msg' => __('vetClinic::lang.saved_successfully')]);
    }

    public function edit($id)
    {
        if (!auth()->user()->can('vet_clinic.access_settings')) {
            abort(403, 'Unauthorized action.');
        }

        $businessId = request()->session()->get('user.business_id');
        return response()->json(VcAppointmentReason::forBusiness($businessId)->findOrFail($id));
    }

    public function update(Request $request, $id)
    {
        if (!auth()->user()->can('vet_clinic.access_settings')) {
            abort(403, 'Unauthorized action.');
        }

        $request->validate(['name' => 'required|string|max:150']);

        $businessId = request()->session()->get('user.business_id');
        VcAppointmentReason::where('business_id', $businessId)->findOrFail($id)->update([
            'name'       => $request->name,
            'sort_order' => (int) $request->input('sort_order', 0),
            'status'     => $request->input('status', 'active'),
        ]);

        return response()->json(['success' => true, 'msg' => __('vetClinic::lang.updated_successfully')]);
    }

    public function destroy($id)
    {
        if (!auth()->user()->can('vet_clinic.access_settings')) {
            abort(403, 'Unauthorized action.');
        }

        $businessId = request()->session()->get('user.business_id');
        VcAppointmentReason::where('business_id', $businessId)->findOrFail($id)->delete();

        return response()->json(['success' => true, 'msg' => __('vetClinic::lang.deleted_successfully')]);
    }
}
