<?php

namespace App\Models\Inventory;

use App\Models\Catalog\Product;
use App\Models\Concerns\BelongsToTenant;
use App\Models\Concerns\HasDefaultUnitFromProduct;
use App\Models\Lookups\UnitOfMeasure;
use App\Support\SerialInput;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;

class ReturnGrnItem extends Model
{
    use BelongsToTenant, HasDefaultUnitFromProduct;

    protected $fillable = [
        'return_grn_id', 'product_id', 'unit_id',
        'source_grn_item_id', 'stock_batch_id', 'serial_numbers',
        'quantity', 'unit_cost', 'total_cost',
    ];

    protected function casts(): array
    {
        return [
            'quantity' => 'decimal:4',
            'unit_cost' => 'decimal:2',
            'total_cost' => 'decimal:2',
            'serial_numbers' => 'array',
        ];
    }

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

    /**
     * The return form submits serials as comma-separated text; the 'array'
     * cast would otherwise json_encode that as a single string and every
     * array-shaped read (count/foreach/implode) would break. Normalize to a
     * real array before the value reaches the column.
     */
    public function setSerialNumbersAttribute(mixed $value): void
    {
        $numbers = SerialInput::numbers($value);

        $this->attributes['serial_numbers'] = $numbers === [] ? null : json_encode($numbers);
    }

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

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

    public function sourceGrnItem(): BelongsTo
    {
        return $this->belongsTo(GrnItem::class, 'source_grn_item_id');
    }

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

    public function serials(): HasMany
    {
        return $this->hasMany(ProductSerial::class, 'stock_batch_id', 'stock_batch_id');
    }
}
