<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Spatie\Activitylog\Traits\LogsActivity;
use Spatie\Activitylog\LogOptions;

class StockTransferItem extends Model
{
    use LogsActivity;

    protected $table = 'stock_transfer_items';

    protected $fillable = [
        'stock_transfer_id',
        'item_id',
        'lot_no',
        'qty',
        'uom_id',
    ];

    protected $casts = [
        'qty' => 'decimal:6',
    ];

    // Relationships
    public function stockTransfer(): BelongsTo
    {
        return $this->belongsTo(StockTransfer::class);
    }

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

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

    // Scopes
    public function scopeForItem($query, $itemId)
    {
        return $query->where('item_id', $itemId);
    }

    public function scopeForLot($query, $lotNo)
    {
        return $query->where('lot_no', $lotNo);
    }

    // Helper methods
    public function getFormattedQtyAttribute()
    {
        return number_format($this->qty, 6);
    }

    public function getActivitylogOptions(): LogOptions
    {
        return LogOptions::defaults()
            ->useLogName('stock_transfer_item')
            ->logFillable()
            ->logOnlyDirty()
            ->dontSubmitEmptyLogs();
    }
}
