<?php

declare(strict_types=1);

namespace App\Policies;

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

class DisciplinaryReportPolicy
{
    use ScopedByDepartment;

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

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

    public function view(User $user, DisciplinaryReport $disciplinaryReport): 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, $disciplinaryReport->employee?->department_id);
        }

        return false;
    }

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

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

        return $user->hasRole('department_manager');
    }

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

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

        if ($user->hasRole('department_manager')) {
            return $this->userManagesDepartment($user, $disciplinaryReport->employee?->department_id);
        }

        return false;
    }

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

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

        if ($user->hasRole('department_manager')) {
            return $this->userManagesDepartment($user, $disciplinaryReport->employee?->department_id);
        }

        return false;
    }

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

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

        if ($user->hasRole('department_manager')) {
            return $this->userManagesDepartment($user, $disciplinaryReport->employee?->department_id);
        }

        return false;
    }

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

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