<?php

namespace Kylin\LawCase\Policies;

use App\Models\User;
use Kylin\LawCase\Models\Fee;

/**
 * 费用权限策略
 */
class FeePolicy
{
    /**
     * 查看费用 - 管理员/合伙人/财务可查看所有，其他仅可查看关联案件的费用
     */
    public function view(User $user, Fee $fee): bool
    {
        if ($user->hasRole('super_admin') || $user->hasRole('partner') || $user->hasRole('finance')) {
            return true;
        }

        // 律师/助理：仅可查看自己参与案件的费用
        return $fee->case
            && ($fee->case->principal_lawyer_id === $user->id
                || $fee->case->assistant_lawyer_id === $user->id
                || $fee->case->lawyers()->where('user_id', $user->id)->exists());
    }

    /**
     * 更新费用 - 仅管理员/合伙人/财务
     */
    public function update(User $user, Fee $fee): bool
    {
        return $user->hasRole('super_admin') || $user->hasRole('partner') || $user->hasRole('finance');
    }

    /**
     * 审批费用 - 仅管理员/合伙人
     */
    public function approve(User $user, Fee $fee): bool
    {
        return $user->hasRole('super_admin') || $user->hasRole('partner');
    }

    /**
     * 记录收款 - 仅管理员/财务
     */
    public function recordPayment(User $user, Fee $fee): bool
    {
        return $user->hasRole('super_admin') || $user->hasRole('finance');
    }
}
