<?php

namespace App\Models\Lookups;

use App\Models\Concerns\BelongsToTenant;
use Illuminate\Database\Eloquent\Model;

class MovementType extends Model
{
    use BelongsToTenant;

    public const PURCHASE = 'purchase';

    public const SALE = 'sale';

    public const RETURN_IN = 'return_in';

    public const RETURN_OUT = 'return_out';

    public const ADJUSTMENT = 'adjustment';

    public const TRANSFER_IN = 'transfer_in';

    public const TRANSFER_OUT = 'transfer_out';

    public const DAMAGE = 'damage';

    public const GRN_REVERSAL = 'grn_reversal';

    public const INVOICE_REVERSAL = 'invoice_reversal';

    public const RETURN_REVERSAL = 'return_reversal';

    public const RETURN_OUT_REVERSAL = 'return_out_reversal';

    public const ADJUSTMENT_REVERSAL = 'adjustment_reversal';

    public const TRANSFER_REVERSAL = 'transfer_reversal';

    public const DAMAGE_REVERSAL = 'damage_reversal';

    public const DIRECTION_IN = 'in';

    public const DIRECTION_OUT = 'out';

    protected $fillable = ['code', 'name', 'direction', 'sort_order', 'is_active'];

    protected function casts(): array
    {
        return [
            'is_active' => 'boolean',
            'sort_order' => 'integer',
        ];
    }

    public static function findByCode(string $code): ?self
    {
        return static::query()->where('code', $code)->first();
    }

    public static function idOf(string $code): ?int
    {
        return static::findByCode($code)?->id;
    }
}
