<?php

declare(strict_types=1);

namespace App\Policies;

use App\Enums\UserRole;
use App\Models\ImportJob;
use App\Models\User;
use App\Policies\Concerns\ChecksBranchAccess;

class ImportJobPolicy
{
    use ChecksBranchAccess;

    public function viewAny(User $user): bool
    {
        return true;
    }

    public function view(User $user, ImportJob $importJob): bool
    {
        if ($importJob->branch_id === null) {
            return $user->role === UserRole::SuperAdmin;
        }

        return $this->canAccessBranch($user, $importJob->branch_id);
    }

    public function create(User $user, ?int $branchId): bool
    {
        if (! in_array($user->role, [UserRole::SuperAdmin, UserRole::RegionAdmin, UserRole::BranchAdmin], true)) {
            return false;
        }

        if ($branchId === null) {
            return $user->role === UserRole::SuperAdmin;
        }

        return $this->canAccessBranch($user, $branchId);
    }
}
