<?php

namespace App\Models\Quote;

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

/**
 * 수식 품목 출력 모델
 *
 * @property int $id
 * @property int $formula_id
 * @property string $item_code
 * @property string $item_name
 * @property string|null $specification
 * @property string $unit
 * @property string $quantity_formula
 * @property string|null $unit_price_formula
 * @property int $sort_order
 */
class QuoteFormulaItem extends Model
{
    protected $table = 'quote_formula_items';

    protected $fillable = [
        'formula_id',
        'item_code',
        'item_name',
        'specification',
        'unit',
        'quantity_formula',
        'unit_price_formula',
        'sort_order',
    ];

    protected $casts = [
        'sort_order' => 'integer',
    ];

    protected $attributes = [
        'sort_order' => 0,
    ];

    // =========================================================================
    // Relationships
    // =========================================================================

    public function formula(): BelongsTo
    {
        return $this->belongsTo(QuoteFormula::class, 'formula_id');
    }

    // =========================================================================
    // Helper Methods
    // =========================================================================

    /**
     * 품목 표시 문자열
     */
    public function getDisplayNameAttribute(): string
    {
        $name = "[{$this->item_code}] {$this->item_name}";

        if ($this->specification) {
            $name .= " ({$this->specification})";
        }

        return $name;
    }
}
