<?php

namespace App\Models;

use App\Casts\TrimmedDecimal;
use App\Concerns\RecordsCreator;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;
use OwenIt\Auditing\Auditable as AuditableTrait;
use OwenIt\Auditing\Contracts\Auditable;

class ProductionOrder extends Model implements Auditable
{
    use AuditableTrait, RecordsCreator;

    /**
     * @var list<string>
     */
    protected $fillable = [
        'factory_product_id',
        'customer_order_item_id',
        'quantity',
        'status',
        'started_at',
        'completed_at',
        'notes',
    ];

    /**
     * @return array<string, string>
     */
    protected function casts(): array
    {
        return [
            'factory_product_id' => 'integer',
            'customer_order_item_id' => 'integer',
            'quantity' => TrimmedDecimal::class.':3',
            'started_at' => 'datetime',
            'completed_at' => 'datetime',
        ];
    }

    /**
     * @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<ProductionOrderMaterial, $this>
     */
    public function materials(): HasMany
    {
        return $this->hasMany(ProductionOrderMaterial::class);
    }
}
