<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Http\Requests\UpdateAppointmentStatusRequest;
use App\Models\Appointment;
use App\Services\Appointments\AppointmentService;
use App\Services\Tenancy\CurrentBusiness;
use Illuminate\Http\RedirectResponse;

class AppointmentStatusController extends Controller
{
    public function __construct(private readonly AppointmentService $appointmentService)
    {
    }

    public function update(UpdateAppointmentStatusRequest $request, Appointment $appointment, CurrentBusiness $currentBusiness): RedirectResponse
    {
        $business = $currentBusiness->get() ?? abort(403);

        abort_unless((int) $appointment->business_id === (int) $business->id, 404);

        $this->appointmentService->changeStatus(
            $business,
            $appointment,
            $request->validated('status'),
            $request->validated('status_note'),
            $request->user()
        );

        return redirect()
            ->back()
            ->with('status', 'Appointment status updated successfully.');
    }
}
