<?php

namespace App\Policies;

use App\Models\EntraGroupSyncRun;
use App\Models\Tenant;
use App\Models\User;
use Illuminate\Auth\Access\HandlesAuthorization;

class EntraGroupSyncRunPolicy
{
    use HandlesAuthorization;

    public function viewAny(User $user): bool
    {
        $tenant = Tenant::current();

        if (! $tenant) {
            return false;
        }

        return $user->canAccessTenant($tenant);
    }

    public function view(User $user, EntraGroupSyncRun $run): bool
    {
        $tenant = Tenant::current();

        if (! $tenant) {
            return false;
        }

        if (! $user->canAccessTenant($tenant)) {
            return false;
        }

        return (int) $run->tenant_id === (int) $tenant->getKey();
    }
}
