<?php

namespace App\Modules\HR\Attendance\Policies;

use App\Models\User;
use App\Modules\HR\Attendance\Models\AttendanceCorrectionRequest;
use App\Modules\HR\Attendance\Support\AttendanceCorrectionStatus;

class AttendanceCorrectionRequestPolicy
{
    public function viewAny(User $user): bool
    {
        return $user->can('attendance.corrections.review');
    }

    public function view(User $user, AttendanceCorrectionRequest $row): bool
    {
        if ((int) $user->tenant_id !== (int) $row->tenant_id) {
            return false;
        }
        if ($user->can('attendance.corrections.review')) {
            return true;
        }

        return $user->employee_id !== null
            && (int) $user->employee_id === (int) $row->employee_id;
    }

    public function create(User $user): bool
    {
        return ! $user->is_admin
            && $user->tenant_id !== null
            && $user->employee_id !== null;
    }

    public function review(User $user, AttendanceCorrectionRequest $row): bool
    {
        if (! $user->can('attendance.corrections.review')) {
            return false;
        }
        if ((int) $user->tenant_id !== (int) $row->tenant_id) {
            return false;
        }

        return $row->status === AttendanceCorrectionStatus::PENDING;
    }
}
