<?php

namespace App\Models;

use App\Models\Concerns\BelongsToCompany;
use Illuminate\Database\Eloquent\Relations\BelongsTo;

class LeaveTransaction extends Model
{
    use BelongsToCompany;

    public const TYPE_OPENING = 'opening';

    public const TYPE_ANNUAL_ALLOCATION = 'annual_allocation';

    public const TYPE_ACCRUAL = 'accrual';

    public const TYPE_CARRY_FORWARD = 'carry_forward';

    public const TYPE_DEBIT = 'debit';

    public const TYPE_CREDIT = 'credit';

    public const TYPE_PENDING_HOLD = 'pending_hold';

    public const TYPE_PENDING_RELEASE = 'pending_release';

    public const TYPE_ADJUSTMENT = 'adjustment';

    public const TYPE_ENCASHMENT = 'encashment';

    protected $fillable = [
        'company_id',
        'user_id',
        'leave_type_id',
        'leave_balance_id',
        'leave_id',
        'transaction_type',
        'amount',
        'balance_before',
        'balance_after',
        'year',
        'effective_date',
        'remarks',
        'created_by',
        'reference_key',
        'meta_json',
    ];

    protected $casts = [
        'year' => 'integer',
        'amount' => 'decimal:2',
        'balance_before' => 'decimal:2',
        'balance_after' => 'decimal:2',
        'effective_date' => 'date',
        'meta_json' => 'array',
    ];

    public function leaveBalance(): BelongsTo
    {
        return $this->belongsTo(LeaveBalance::class, 'leave_balance_id');
    }

    public function leave(): BelongsTo
    {
        return $this->belongsTo(Leave::class);
    }

    public function user(): BelongsTo
    {
        return $this->belongsTo(User::class);
    }

    public function leaveType(): BelongsTo
    {
        return $this->belongsTo(LeaveType::class, 'leave_type_id');
    }
}
