<?php

namespace App\Models;

use App\Traits\Multitenantable;
use DateTimeInterface;
use Illuminate\Database\Eloquent\Model;

class PurchaseItem extends Model
{
    use Multitenantable;
    protected $primaryKey = 'uuid';
    public $incrementing = false;
    protected $keyType = 'string';

    protected $fillable = [
        'uuid', 
        'tenant_id',
        'purchase_id', 
        'item_type', 
        'item_id', 
        'quantity',
        'item_image_url_snapshot', 
        'item_name_snapshot',
        'item_variant_name_snapshot', 
        'purchase_price_snapshot', 
        'subtotal', 
    ];

    protected $casts = [
        'created_at' => 'datetime',
        'updated_at' => 'datetime',
    ];

    protected function serializeDate(DateTimeInterface $date)
    {
        return $date->format('Y-m-d H:i:s');
    }

    public function purchase() {
        return $this->belongsTo(Purchase::class, 'purchase_id', 'uuid');
    }

    // Relasi dinamis tergantung item_type (Product/RawMaterial)
    public function item() {
        return $this->morphTo(null, 'item_type', 'item_id', 'uuid');
    }
}
