<?php

namespace App\Models\Sales;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;

/**
 * 영업수수료 상세 모델 (상품별 수당 내역)
 *
 * @property int $id
 * @property int $commission_id
 * @property int $contract_product_id
 * @property float $registration_fee
 * @property float $base_amount
 * @property float $partner_rate
 * @property float $manager_rate
 * @property float $partner_commission
 * @property float $manager_commission
 */
class SalesCommissionDetail extends Model
{
    protected $table = 'sales_commission_details';

    protected $fillable = [
        'commission_id',
        'contract_product_id',
        'registration_fee',
        'base_amount',
        'partner_rate',
        'manager_rate',
        'partner_commission',
        'manager_commission',
    ];

    protected $casts = [
        'registration_fee' => 'decimal:2',
        'base_amount' => 'decimal:2',
        'partner_rate' => 'decimal:2',
        'manager_rate' => 'decimal:2',
        'partner_commission' => 'decimal:2',
        'manager_commission' => 'decimal:2',
    ];

    /**
     * 수수료 정산 관계
     */
    public function commission(): BelongsTo
    {
        return $this->belongsTo(SalesCommission::class, 'commission_id');
    }

    /**
     * 계약 상품 관계
     */
    public function contractProduct(): BelongsTo
    {
        return $this->belongsTo(SalesContractProduct::class, 'contract_product_id');
    }

    /**
     * 총 수당액 Accessor
     */
    public function getTotalCommissionAttribute(): float
    {
        return $this->partner_commission + $this->manager_commission;
    }
}
