<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;

class StockAdjustment extends Model
{
    protected $fillable = [
        'batch_id',
        'product_id',
        'branch_id',
        'location_id',
        'user_id',
        'type',
        'quantity',
        'reason',
        'reference',
        'stock_before',
        'stock_after',
    ];

    protected $casts = [
        'quantity' => 'decimal:2',
        'stock_before' => 'decimal:2',
        'stock_after' => 'decimal:2',
    ];

    public function batch(): BelongsTo
    {
        return $this->belongsTo(InventoryAdjustmentBatch::class, 'batch_id');
    }

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

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

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

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

    public function getTypeLabelAttribute(): string
    {
        return $this->type === 'in' ? __('Stock In') : __('Stock Out');
    }

    public function getTypeColorAttribute(): string
    {
        return $this->type === 'in' ? 'success' : 'danger';
    }
}
