<?php

namespace App\Models\Catalog;

use App\Models\Concerns\BelongsToTenant;
use App\Models\Concerns\HasDefaultUnitFromProduct;
use App\Models\Lookups\UnitOfMeasure;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Support\Str;

class BundleItem extends Model
{
    use BelongsToTenant, HasDefaultUnitFromProduct;

    public const UPDATED_AT = null;

    protected $fillable = ['uuid', 'bundle_id', 'product_id', 'quantity', 'unit_id', 'created_by'];

    protected function casts(): array
    {
        return ['quantity' => 'decimal:4'];
    }

    protected static function booted(): void
    {
        static::creating(function (self $item): void {
            if (! $item->uuid) {
                $item->uuid = (string) Str::ulid();
            }
        });
    }

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

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

    public function unit(): BelongsTo
    {
        return $this->belongsTo(UnitOfMeasure::class, 'unit_id');
    }
}
