<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class CourierLedgerEntry extends Model
{
    use HasFactory;

    public const EVENT_OPENING_BALANCE = 'opening_balance';
    public const EVENT_HANDOVER = 'parcel_handed_over';
    public const EVENT_COD_COLLECTION = 'cod_collected';
    public const EVENT_DELIVERY_CHARGE = 'courier_charge_deducted';
    public const EVENT_COD_CHARGE = 'cod_charge_deducted';
    public const EVENT_RETURN_CHARGE = 'return_charge_applied';
    public const EVENT_SETTLEMENT = 'settlement_received';
    public const EVENT_ADJUSTMENT = 'manual_adjustment';
    public const EVENT_REVERSAL = 'reversal';

    protected $guarded = [];

    protected $casts = [
        'entry_date' => 'date',
        'debit' => 'float',
        'credit' => 'float',
        'balance' => 'float',
        'payload' => 'array',
        'reversed_at' => 'datetime',
        'created_at' => 'datetime',
        'updated_at' => 'datetime',
    ];

    public function courier()
    {
        return $this->belongsTo(Courier::class);
    }

    public function shipment()
    {
        return $this->belongsTo(OrderCourierShipment::class, 'order_courier_shipment_id');
    }

    public function order()
    {
        return $this->belongsTo(Order::class);
    }

    public function settlement()
    {
        return $this->belongsTo(CourierSettlement::class, 'courier_settlement_id');
    }

    public function reversedEntry()
    {
        return $this->belongsTo(self::class, 'reversed_entry_id');
    }
}
