<?php

namespace App\Models;

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

class InventoryAdjustment extends Model
{
    protected $fillable = [
        'product_id',
        'branch_id',
        'user_id',
        'type',
        'quantity',
        'quantity_before',
        'quantity_after',
        'reason',
        'notes',
    ];

    protected $casts = [
        'quantity' => 'integer',
        'quantity_before' => 'integer',
        'quantity_after' => 'integer',
    ];

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

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

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

    public function scopeIncreases($query)
    {
        return $query->where('type', 'increase');
    }

    public function scopeDecreases($query)
    {
        return $query->where('type', 'decrease');
    }

    /**
     * Get the list of common reasons
     */
    public static function reasons(): array
    {
        return [
            'damaged' => __('Damaged'),
            'stolen' => __('Stolen'),
            'expired' => __('Expired'),
            'correction' => __('Stock Correction'),
            'stocktake' => __('Stock Take'),
            'returned_defective' => __('Returned Defective'),
            'promotion' => __('Promotional Use'),
            'other' => __('Other'),
        ];
    }
}
