<?php

namespace App\Policies\Definition;

use App\Models\Definition\Definition;
use App\Models\User;

class DefinitionPolicy
{
    public function viewAny(User $user): bool
    {
        return $user->hasAgency() && $user->can('definitions.view');
    }

    public function view(User $user, Definition $definition): bool
    {
        return $user->hasAgency() &&
            $user->currentAgencyId() === $definition->agency_id;// && $user->can('definitions.view');
    }

    public function create(User $user): bool
    {
        return $user->hasAgency();// && $user->can('definitions.create');
    }

    public function update(User $user, Definition $definition): bool
    {
        // Eğer definition'ın agency_id'si null ise (global tanımlama), herkes düzenleyebilir
        if ($definition->agency_id === null) {
            return $user->hasAgency();
        }

        return $user->hasAgency() &&
            $user->currentAgencyId() === $definition->agency_id; //&&$user->can('definitions.update');
    }

    public function delete(User $user, Definition $definition): bool
    {
        return $user->hasAgency() &&
            $user->currentAgencyId() === $definition->agency_id;// && $user->can('definitions.delete');
    }

    public function restore(User $user, Definition $definition): bool
    {
        return $user->hasAgency() &&
            $user->currentAgencyId() === $definition->agency_id;// && $user->can('definitions.restore');
    }

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