<?php

namespace App\Models\Sales;

use App\Models\Catalog\Product;
use App\Models\Catalog\ProductBundle;
use App\Models\Concerns\BelongsToTenant;
use App\Models\Inventory\StockBatch;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;

class HeldSaleItem extends Model
{
    use BelongsToTenant;

    protected $fillable = [
        'held_sale_id', 'product_id', 'product_bundle_id', 'product_name',
        'quantity', 'unit_price', 'vat_rate', 'discount_type', 'discount_value',
        'stock_batch_id', 'selected_serial_ids',
    ];

    protected function casts(): array
    {
        return [
            'quantity' => 'decimal:4',
            'unit_price' => 'decimal:2',
            'vat_rate' => 'decimal:2',
            'discount_value' => 'decimal:2',
            'selected_serial_ids' => 'array',
        ];
    }

    public function heldSale(): BelongsTo
    {
        return $this->belongsTo(HeldSale::class);
    }

    public function product(): BelongsTo
    {
        return $this->belongsTo(Product::class);
    }

    public function bundle(): BelongsTo
    {
        return $this->belongsTo(ProductBundle::class, 'product_bundle_id');
    }

    public function stockBatch(): BelongsTo
    {
        return $this->belongsTo(StockBatch::class, 'stock_batch_id');
    }
}
