<?php

namespace Kylin\LawCase\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Kylin\LawCase\Enums\FeeType;
use Kylin\LawCase\Enums\ChargeMethod;

/**
 * 费用模型
 *
 * 管理案件费用，包括律师费、差旅费、鉴定费等。
 *
 * @property string $fee_no        费用编号
 * @property string $fee_type      费用类型
 * @property string $charge_method 计费方式
 * @property float  $amount        金额
 */
class Fee extends Model
{
    use SoftDeletes;

    protected $table = 'kylin_law_fees';

    protected $fillable = [
        'fee_no',
        'fee_type',
        'charge_method',
        'case_id',
        'client_id',
        'amount',
        'paid_amount',
        'approval_status',
        'approved_by',
        'approved_at',
        'approval_remark',
        'description',
    ];

    protected $casts = [
        'fee_type' => FeeType::class,
        'charge_method' => ChargeMethod::class,
        'amount' => 'decimal:2',
        'paid_amount' => 'decimal:2',
        'approved_at' => 'datetime',
    ];

    public function case()
    {
        return $this->belongsTo(CaseModel::class, 'case_id');
    }

    public function client()
    {
        return $this->belongsTo(Client::class);
    }

    public function approver()
    {
        return $this->belongsTo(\App\Models\User::class, 'approved_by');
    }

    public function invoices()
    {
        return $this->hasMany(Invoice::class);
    }

    public function timeEntries()
    {
        return $this->hasMany(TimeEntry::class, 'fee_id');
    }

    public function scopePendingApproval($query)
    {
        return $query->where('approval_status', 'pending');
    }
}
