<?php

declare(strict_types=1);

namespace App\Policies;

use App\Models\EnhanceJob;
use App\Models\User;

final class EnhanceJobPolicy
{
    public function before(?User $user, string $ability): ?bool
    {
        if ($user && ($user->isAdmin() || $user->isModerator())) {
            return true;
        }

        return null;
    }

    public function viewAny(?User $user): bool
    {
        return $user !== null;
    }

    public function view(User $user, EnhanceJob $enhanceJob): bool
    {
        return (int) $enhanceJob->user_id === (int) $user->id;
    }

    public function create(?User $user): bool
    {
        if ($user === null) {
            return false;
        }

        return ! method_exists($user, 'hasVerifiedEmail') || $user->hasVerifiedEmail();
    }

    public function delete(User $user, EnhanceJob $enhanceJob): bool
    {
        return $enhanceJob->canBeDeletedBy($user);
    }

    public function retry(User $user, EnhanceJob $enhanceJob): bool
    {
        return (int) $enhanceJob->user_id === (int) $user->id
            && $enhanceJob->isFailed();
    }

    public function markFailed(User $user, EnhanceJob $enhanceJob): bool
    {
        return false;
    }
}