<?php

namespace App\Models;

use App\Models\Catalog\Product;
use App\Models\Concerns\BelongsToTenant;
use App\Models\Concerns\BranchScoped;
use App\Models\Inventory\StockBatch;
use App\Models\Lookups\MovementType;
use App\Models\Lookups\UnitOfMeasure;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;

class StockMovement extends Model
{
    use BelongsToTenant, BranchScoped;

    public $timestamps = false;

    protected $fillable = [
        'branch_id',
        'product_id',
        'stock_batch_id',
        'movement_type_id',
        'reference_type',
        'reference_id',
        'reference_number',
        'quantity_before',
        'quantity_change',
        'quantity_after',
        'unit_id',
        'unit_cost',
        'note',
        'created_by',
        'created_at',
    ];

    protected function casts(): array
    {
        return [
            'quantity_before' => 'decimal:4',
            'quantity_change' => 'decimal:4',
            'quantity_after' => 'decimal:4',
            'unit_cost' => 'decimal:2',
            'created_at' => 'datetime',
        ];
    }

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

    public function movementType(): BelongsTo
    {
        return $this->belongsTo(MovementType::class, 'movement_type_id');
    }

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

    public function creator(): BelongsTo
    {
        return $this->belongsTo(User::class, 'created_by');
    }

    public function createdBy(): BelongsTo
    {
        return $this->belongsTo(User::class, 'created_by');
    }

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