<?php

declare(strict_types=1);

namespace App\Policies;

use App\Models\User;
use App\Models\WalletTransaction;

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

    public function view(User $user, WalletTransaction $walletTransaction): bool
    {
        if ($user->hasRole('admin')) {
            return true;
        }

        if ($walletTransaction->user_id === $user->id) {
            return true;
        }

        return $walletTransaction->project && (
            $walletTransaction->project->user_id === $user->id
            || $walletTransaction->project->created_by === $user->id
            || ($walletTransaction->project->producer && $walletTransaction->project->producer->user_id === $user->id)
        );
    }
}
