<?php

declare(strict_types=1);

namespace App\Policies;

use App\Models\Producer;
use App\Models\User;

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

    public function view(?User $user, Producer $producer): bool
    {
        if (! $user) {
            return $producer->status === Producer::STATUS_APPROVED;
        }

        return $user->hasRole('admin')
            || $user->id === $producer->user_id
            || $user->id === $producer->created_by
            || $producer->status === Producer::STATUS_APPROVED;
    }

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

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

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

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

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