<?php

namespace App\Policies\Tour;

use App\Models\Tour\Tour;
use App\Models\User;

class TourPolicy
{
    /**
     * Determine whether the user can view any tours.
     */
    public function viewAny(User $user): bool
    {
        return $user->hasAnyRole(['super-admin', 'agency-owner', 'agency-manager', 'agency-staff']);
    }

    /**
     * Determine whether the user can view the tour.
     */
    public function view(User $user, Tour $tour): bool
    {
        // Super admin can view all tours
        if ($user->isSuperAdmin()) {
            return true;
        }

        // Agency users can only view their own agency's tours
        return $user->currentAgencyId() === $tour->agency_id;
    }

    /**
     * Determine whether the user can create tours.
     */
    public function create(User $user): bool
    {
        return $user->hasAnyRole(['agency-owner', 'agency-manager', 'agency-staff'])
            && $user->hasAgency();
    }

    /**
     * Determine whether the user can update the tour.
     */
    public function update(User $user, Tour $tour): bool
    {
        // Super admin can update all tours
        if ($user->isSuperAdmin()) {
            return true;
        }

        // Must belong to same agency
        if ($user->currentAgencyId() !== $tour->agency_id) {
            return false;
        }

        // Agency owner and manager can update all tours
        if ($user->hasAnyRole(['agency-owner', 'agency-manager'])) {
            return true;
        }

        // Staff can only update tours they created
        if ($user->hasRole('agency-staff')) {
            return $tour->created_by === $user->id;
        }

        return false;
    }

    /**
     * Determine whether the user can delete the tour.
     */
    public function delete(User $user, Tour $tour): bool
    {
        // Super admin can delete all tours
        if ($user->isSuperAdmin()) {
            return true;
        }

        // Must belong to same agency
        if ($user->currentAgencyId() !== $tour->agency_id) {
            return false;
        }

        // Agency owner can delete all tours
        if ($user->isAgencyOwner()) {
            return true;
        }

        // Agency manager can delete tours except those with bookings
        if ($user->hasRole('agency-manager')) {
            return !$tour->tourDates()->where('sold_count', '>', 0)->exists();
        }

        // Staff cannot delete tours
        return false;
    }

    /**
     * Determine whether the user can publish/unpublish the tour.
     */
    public function publish(User $user, Tour $tour): bool
    {
        // Must belong to same agency
        if ($user->currentAgencyId() !== $tour->agency_id) {
            return false;
        }

        // Only owner and manager can publish
        return $user->hasAnyRole(['agency-owner', 'agency-manager']);
    }

    /**
     * Determine whether the user can manage tour bookings.
     */
    public function manageBookings(User $user, Tour $tour): bool
    {
        // Must belong to same agency
        if ($user->currentAgencyId() !== $tour->agency_id) {
            return false;
        }

        return $user->hasAnyRole(['agency-owner', 'agency-manager', 'agency-staff']);
    }

    /**
     * Determine whether the user can view tour analytics.
     */
    public function viewAnalytics(User $user, Tour $tour): bool
    {
        // Must belong to same agency
        if ($user->currentAgencyId() !== $tour->agency_id) {
            return false;
        }

        return $user->hasAnyRole(['agency-owner', 'agency-manager']);
    }
}
