<?php

namespace App\Models;

use App\Enums\ChargeBasis;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;

class QuotationItem extends Model
{
    /**
     * @var list<string>
     */
    protected $fillable = [
        'quotation_id',
        'description',
        'quantity',
        'unit_price',
        'charge_basis',
        'guest_count',
        'billable_days',
        'billable_hours',
        'discount_amount',
        'tax_rate',
        'tax_amount',
        'line_total',
        'product_id',
        'package_id',
        'product_snapshot',
        'package_snapshot',
        'sort_order',
    ];

    /**
     * @return array<string, string>
     */
    protected function casts(): array
    {
        return [
            'quantity' => 'decimal:2',
            'unit_price' => 'decimal:2',
            'charge_basis' => ChargeBasis::class,
            'guest_count' => 'integer',
            'billable_days' => 'integer',
            'billable_hours' => 'decimal:2',
            'discount_amount' => 'decimal:2',
            'tax_rate' => 'decimal:2',
            'tax_amount' => 'decimal:2',
            'line_total' => 'decimal:2',
            'sort_order' => 'integer',
        ];
    }

    /**
     * @return BelongsTo<Quotation, $this>
     */
    public function quotation(): BelongsTo
    {
        return $this->belongsTo(Quotation::class);
    }

    /**
     * @return BelongsTo<Product, $this>
     */
    public function product(): BelongsTo
    {
        return $this->belongsTo(Product::class);
    }

    /**
     * @return BelongsTo<Package, $this>
     */
    public function package(): BelongsTo
    {
        return $this->belongsTo(Package::class);
    }

    /**
     * @return array<string, mixed>
     */
    public function toPricingLine(): array
    {
        return [
            'unit_price' => $this->unit_price,
            'quantity' => $this->quantity,
            'guest_count' => $this->guest_count ?? 1,
            'billable_days' => $this->billable_days ?? 1,
            'billable_hours' => $this->billable_hours ?? 1,
            'discount_amount' => $this->discount_amount,
            'tax_rate' => $this->tax_rate,
            'charge_basis' => $this->charge_basis,
        ];
    }
}
