<?php

namespace App\Models\Sales;

use App\Models\Concerns\BelongsToTenant;
use App\Models\Lookups\CashMovementReason;
use App\Models\User;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;

class CashMovement extends Model
{
    use BelongsToTenant;

    public const IN = 'in';

    public const OUT = 'out';

    public const CASH_REFUND = 'cash_refund';

    /** Automatic take from a completed sale. */
    public const SOURCE_SALE = 'sale';

    /** Automatic pay-out for a refunded return. */
    public const SOURCE_REFUND = 'refund';

    /** Cashier-initiated pay-in or pay-out (petty cash). */
    public const SOURCE_MANUAL = 'manual';

    public $timestamps = false;

    protected $fillable = ['pos_session_id', 'type', 'source', 'amount', 'reason_id', 'note', 'created_by', 'created_at'];

    protected function casts(): array
    {
        return [
            'amount' => 'decimal:2',
            'created_at' => 'datetime',
        ];
    }

    public function session(): BelongsTo
    {
        return $this->belongsTo(PosSession::class, 'pos_session_id');
    }

    public function reason(): BelongsTo
    {
        return $this->belongsTo(CashMovementReason::class, 'reason_id');
    }

    public function creator(): BelongsTo
    {
        return $this->belongsTo(User::class, 'created_by');
    }
}
