<?php

declare(strict_types=1);

namespace App\Policies;

use App\Models\Department;
use App\Models\User;
use App\Policies\Concerns\ScopedByDepartment;

class DepartmentPolicy
{
    use ScopedByDepartment;

    public function viewAny(User $user): bool
    {
        if ($this->isSuperAdmin($user)) {
            return true;
        }

        return $this->canRead($user);
    }

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

        if ($this->hasFullAccess($user) || $user->hasRole('supervisor')) {
            return true;
        }

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

        if ($user->hasRole('department_manager')) {
            return $this->userManagesDepartment($user, $department->id);
        }

        return false;
    }

    public function create(User $user): bool
    {
        if ($this->isSuperAdmin($user)) {
            return true;
        }

        return $this->hasFullAccess($user);
    }

    public function update(User $user, Department $department): bool
    {
        if ($this->isSuperAdmin($user)) {
            return true;
        }

        return $this->hasFullAccess($user);
    }

    public function delete(User $user, Department $department): bool
    {
        if ($this->isSuperAdmin($user)) {
            return true;
        }

        return $this->hasFullAccess($user);
    }

    public function restore(User $user, Department $department): bool
    {
        if ($this->isSuperAdmin($user)) {
            return true;
        }

        return $this->hasFullAccess($user);
    }

    public function forceDelete(User $user, Department $department): bool
    {
        if ($this->isSuperAdmin($user)) {
            return true;
        }

        return $this->canForceDelete($user);
    }
}
