<?php

namespace App\Policies;

use App\Enums\UserRole;
use App\Models\Appointment;
use App\Models\User;
use App\Services\AccessService;

class AppointmentPolicy
{
    public function __construct(private readonly AccessService $access) {}

    public function view(User $user, Appointment $appointment): bool
    {
        return $this->access->scopeAppointments(
            Appointment::query()->whereKey($appointment->id),
            $user
        )->exists();
    }

    public function update(User $user, Appointment $appointment): bool
    {
        if ($this->access->isAdmin($user)) {
            return true;
        }

        if ($user->role === UserRole::PATIENT) {
            return false;
        }

        return $this->access->canUseDoctorLocation(
            $user,
            $appointment->doctor_id,
            $appointment->location_id,
            $user->role === UserRole::ASSISTANT ? 'can_book_appointment' : null
        );
    }
}
