<?php

namespace App\Policies;

use App\Models\OperationRun;
use App\Models\Tenant;
use App\Models\User;
use Illuminate\Auth\Access\HandlesAuthorization;
use Illuminate\Auth\Access\Response;

class OperationRunPolicy
{
    use HandlesAuthorization;

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

        if (! $tenant) {
            return false;
        }

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

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

        if (! $tenant) {
            return false;
        }

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

        if ((int) $run->tenant_id !== (int) $tenant->getKey()) {
            return Response::denyAsNotFound();
        }

        return true;
    }
}
