<?php

namespace App\Policies;

use App\Enums\TravelStatus;
use App\Models\TravelRequest;
use App\Models\User;

class TravelRequestPolicy
{
    public function viewAny(User $user): bool
    {
        return true;
    }

    public function view(User $user, TravelRequest $travelRequest): bool
    {
        return $user->id === $travelRequest->user_id
            || $user->hasAnyRole(['travel_approver', 'administrator']);
    }

    public function create(User $user): bool
    {
        return $user->hasAnyRole(['staff', 'travel_approver', 'administrator']);
    }

    public function update(User $user, TravelRequest $travelRequest): bool
    {
        return $user->id === $travelRequest->user_id
            && $travelRequest->status === TravelStatus::Draft;
    }

    public function approve(User $user, TravelRequest $travelRequest): bool
    {
        return $user->hasAnyRole(['travel_approver', 'administrator'])
            && $travelRequest->status === TravelStatus::Pending;
    }

    public function delete(User $user, TravelRequest $travelRequest): bool
    {
        return $user->hasRole('administrator');
    }
}
