<?php

namespace App\Domain\POS\Models;

use App\Domain\Financial\Models\CashSession;
use App\Domain\Financial\Models\Invoice;
use App\Domain\Identity\Models\Branch;
use App\Domain\POS\Enums\POSPaymentMethod;
use App\Domain\POS\Enums\POSPaymentStatus;
use App\Domain\Participant\Models\Participant;
use App\Domain\Pricing\Models\Promotion;
use App\Domain\Shared\Traits\BelongsToBranch;
use App\Domain\Shared\Traits\BelongsToAcademy;
use App\Domain\Shared\Traits\HasUuid;
use App\Models\User;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;

class POSTransaction extends Model
{
    use BelongsToAcademy, BelongsToBranch, HasUuid;

    protected $table = 'pos_transactions';

    protected $fillable = [
        'academy_id',
        'branch_id',
        'cash_session_id',
        'participant_id',
        'receipt_number',
        'subtotal',
        'discount_amount',
        'tax_amount',
        'service_fee_amount',
        'total_amount',
        'payment_method',
        'payment_status',
        'invoice_id',
        'promotion_id',
        'coupon_code',
        'notes',
        'processed_by',
        'processed_at',
        'refunded_at',
        'metadata',
    ];

    protected $casts = [
        'subtotal' => 'integer',
        'discount_amount' => 'integer',
        'tax_amount' => 'integer',
        'service_fee_amount' => 'integer',
        'total_amount' => 'integer',
        'payment_method' => POSPaymentMethod::class,
        'payment_status' => POSPaymentStatus::class,
        'processed_at' => 'datetime',
        'refunded_at' => 'datetime',
        'metadata' => 'array',
    ];

    public function items(): HasMany
    {
        return $this->hasMany(POSTransactionItem::class, 'pos_transaction_id');
    }

    public function splitPayments(): HasMany
    {
        return $this->hasMany(POSSplitPayment::class, 'pos_transaction_id');
    }

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

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

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

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

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

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