<?php

namespace Modules\Vrc\Http\Controllers;

use Illuminate\Http\Request;
use Modules\Vrc\Entities\VrcServiceReminder;
use Modules\Vrc\Entities\VrcVehicle;
use Modules\Vrc\Services\Contracts\ContactServiceContract;
use Yajra\DataTables\Facades\DataTables;

class ReminderController extends VrcBaseController
{
    public function __construct(protected ContactServiceContract $contacts)
    {
    }

    public function index()
    {
        $this->guard('vrc.reminder.manage');

        if (request()->ajax()) {
            $rows = VrcServiceReminder::where('vrc_service_reminders.business_id', $this->businessId())
                ->leftJoin('vrc_vehicles as v', 'v.id', '=', 'vrc_service_reminders.vehicle_id')
                ->select(['vrc_service_reminders.id', 'vrc_service_reminders.type', 'vrc_service_reminders.due_date', 'vrc_service_reminders.status', 'v.plate_number']);

            return DataTables::of($rows)
                ->editColumn('type', fn ($r) => __('vrc::lang.reminder_' . $r->type))
                ->editColumn('status', fn ($r) => '<span class="label label-default">' . __('vrc::lang.rem_' . $r->status) . '</span>')
                ->addColumn('action', fn ($r) => '<button data-href="' . route('vrc.service-reminders.destroy', $r->id) . '" class="btn btn-xs btn-danger vrc-delete"><i class="fa fa-trash"></i></button>')
                ->rawColumns(['status', 'action'])->make(true);
        }

        return view('vrc::reminders.index', [
            'customers' => $this->contacts->customersDropdown($this->businessId()),
        ]);
    }

    public function store(Request $request)
    {
        $this->guard('vrc.reminder.manage');
        $data = $this->validate($request, [
            'vehicle_id'   => 'required|integer',
            'type'         => 'required|in:mileage,date,next_service',
            'due_date'     => 'nullable|date',
            'due_odometer' => 'nullable|integer|min:0',
            'channel'      => 'nullable|in:sms,email,whatsapp',
            'note'         => 'nullable|string',
        ]);

        try {
            $vehicle = VrcVehicle::where('business_id', $this->businessId())->findOrFail($data['vehicle_id']);
            VrcServiceReminder::create(array_merge($data, [
                'business_id'     => $this->businessId(),
                'erp_customer_id' => $vehicle->erp_customer_id,
                'scheduled_for'   => $data['due_date'] ?? null,
                'status'          => 'pending',
                'channel'         => $data['channel'] ?? 'sms',
            ]));
            return $this->ok(__('vrc::lang.added_success'));
        } catch (\Exception $e) {
            \Log::emergency($e->getMessage());
            return $this->fail();
        }
    }

    public function destroy($id)
    {
        $this->guard('vrc.reminder.manage');
        try {
            VrcServiceReminder::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();
        }
    }
}
