<?php

namespace App\Policies;

use App\Models\Booking;
use App\Models\User;

class BookingPolicy
{
    public function before(User $user): ?bool
    {
        return $user->isSuperAdmin() ? true : null;
    }

    public function viewAny(User $user): bool
    {
        return $user->company_id !== null;
    }

    public function view(User $user, Booking $booking): bool
    {
        if ($user->company_id !== $booking->company_id) {
            return false;
        }

        // Empleado solo ve sus propias reservas
        if ($user->isEmployee() && $user->employee) {
            return $booking->employee_id === $user->employee->id;
        }

        return true;
    }

    public function create(User $user): bool
    {
        return $user->isCompanyAdmin() || $user->isEmployee();
    }

    public function update(User $user, Booking $booking): bool
    {
        return $this->view($user, $booking);
    }

    public function cancel(User $user, Booking $booking): bool
    {
        return $this->view($user, $booking) && $booking->isCancellable();
    }

    public function delete(User $user, Booking $booking): bool
    {
        return $user->isCompanyAdmin() && $user->company_id === $booking->company_id;
    }
}
