<?php

namespace App\Models;

use App\Casts\TrimmedDecimal;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;

class SaleItem extends Model
{
    /**
     * @var list<string>
     */
    protected $fillable = [
        'sale_id',
        'factory_product_id',
        'customer_order_item_id',
        'quantity_mode',
        'quantity',
        'total_weight',
        'weight_price',
        'return_cotton',
        'cotton_price',
    ];

    /**
     * @return array<string, string>
     */
    protected function casts(): array
    {
        return [
            'sale_id' => 'integer',
            'factory_product_id' => 'integer',
            'customer_order_item_id' => 'integer',
            'quantity' => TrimmedDecimal::class.':3',
            'total_weight' => TrimmedDecimal::class.':3',
            'weight_price' => TrimmedDecimal::class.':2',
            'return_cotton' => TrimmedDecimal::class.':3',
            'cotton_price' => TrimmedDecimal::class.':2',
        ];
    }

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

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

    /**
     * @return BelongsTo<CustomerOrderItem, $this>
     */
    public function orderItem(): BelongsTo
    {
        return $this->belongsTo(CustomerOrderItem::class, 'customer_order_item_id');
    }

    /**
     * @return HasMany<SalePack, $this>
     */
    public function packs(): HasMany
    {
        return $this->hasMany(SalePack::class);
    }
}
