<?php

namespace App\Http\Controllers\Tenant;

use App\Http\Controllers\Controller;
use App\Http\Requests\RecallRequest;
use App\Models\Dentist;
use App\Models\Recall;

class RecallController extends Controller
{
    public function index()
    {
        $this->authorize('viewAny', Recall::class);

        return view('recalls.index', [
            'due' => Recall::with(['patient:id,full_name,phone', 'dentist:id,name'])
                ->whereIn('status', ['scheduled', 'sent'])
                ->orderBy('due_date')->paginate(30),
            'dentists' => Dentist::active()->get(['id', 'name']),
        ]);
    }

    public function store(RecallRequest $request)
    {
        $this->authorize('create', Recall::class);

        Recall::create($request->validated() + ['status' => 'scheduled']);

        return back()->with('status', __('Recall scheduled.'));
    }

    public function update(Recall $recall)
    {
        $this->authorize('update', $recall);

        request()->validate(['status' => ['required', 'in:scheduled,booked,dismissed']]);
        $recall->update(['status' => request('status')]);

        return back()->with('status', __('Recall updated.'));
    }
}
