<?php

declare(strict_types=1);

namespace App\Policies;

use App\Models\Expert;
use App\Models\User;

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

    public function view(?User $user, Expert $expert): bool
    {
        if (! $user) {
            return in_array($expert->status, [Expert::STATUS_APPROVED, 'active'], true);
        }

        return $user->hasRole('admin')
            || $user->id === $expert->user_id
            || $user->id === $expert->created_by
            || in_array($expert->status, [Expert::STATUS_APPROVED, 'active'], true);
    }

    public function create(User $user): bool
    {
        return $user->hasRole('admin') || $user->hasAnyRole(['user', 'employer', 'expert']);
    }

    public function update(User $user, Expert $expert): bool
    {
        return $user->hasRole('admin')
            || $user->id === $expert->user_id
            || $user->id === $expert->created_by;
    }

    public function delete(User $user, Expert $expert): bool
    {
        return $user->hasRole('admin') || $user->id === $expert->user_id;
    }

    public function restore(User $user, Expert $expert): bool
    {
        return $user->hasRole('admin');
    }

    public function forceDelete(User $user, Expert $expert): bool
    {
        return $user->hasRole('admin');
    }
}
