<?php

namespace App\Policies\Tour;

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

class TourCategoryPolicy
{
    public function viewAny(User $user): bool
    {
        return $user->hasAgency() || $user->isSuperAdmin();
    }

    public function view(User $user, TourCategory $tourCategory): bool
    {
        if ($user->isSuperAdmin()) {
            return true;
        }

        // Ana kategorileri herkes görebilir (salt okunur)
        if ($tourCategory->is_master_category) {
            return $user->hasAgency();
        }

        return $user->hasAgency() && $tourCategory->agency_id === $user->currentAgencyId();
    }

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

    public function update(User $user, TourCategory $tourCategory): bool
    {
        // Ana kategorileri sadece super admin düzenleyebilir
        if ($tourCategory->is_master_category) {
            return $user->isSuperAdmin();
        }

        if ($user->isSuperAdmin()) {
            return true;
        }

        // Acente kategorilerini sadece kendi acente kullanıcıları düzenleyebilir
        return $user->hasAgency() && $tourCategory->agency_id === $user->currentAgencyId();
    }

    public function delete(User $user, TourCategory $tourCategory): bool
    {
        // Ana kategorileri sadece super admin silebilir
        if ($tourCategory->is_master_category) {
            return $user->isSuperAdmin();
        }

        if ($user->isSuperAdmin()) {
            return true;
        }

        // Acente kategorilerini sadece kendi acente kullanıcıları silebilir
        return $user->hasAgency() && $tourCategory->agency_id === $user->currentAgencyId();
    }

    public function restore(User $user, TourCategory $tourCategory): bool
    {
        return $this->delete($user, $tourCategory);
    }

    public function forceDelete(User $user, TourCategory $tourCategory): bool
    {
        return $user->isSuperAdmin();
    }
}
