<?php

namespace App\Models\Inventory;

use App\Models\Branch;
use App\Models\Catalog\Product;
use App\Models\Concerns\Auditable;
use App\Models\Concerns\BelongsToTenant;
use App\Models\Concerns\DualBranchScoped;
use App\Models\Concerns\HasDefaultUnitFromProduct;
use App\Models\Lookups\StockTransferReason;
use App\Models\Lookups\UnitOfMeasure;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\SoftDeletes;

class StockTransfer extends Model
{
    use Auditable, BelongsToTenant, DualBranchScoped, HasDefaultUnitFromProduct, SoftDeletes;

    protected $fillable = [
        'from_branch_id', 'to_branch_id', 'product_id',
        'unit_id', 'stock_batch_id', 'serial_ids',
        'reason_id', 'quantity', 'note',
    ];

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

    public function fromBranch(): BelongsTo
    {
        return $this->belongsTo(Branch::class, 'from_branch_id');
    }

    public function toBranch(): BelongsTo
    {
        return $this->belongsTo(Branch::class, 'to_branch_id');
    }

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

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

    public function reason(): BelongsTo
    {
        return $this->belongsTo(StockTransferReason::class, 'reason_id');
    }

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