<?php

namespace App\Models\Sales;

use App\Models\Branch;
use App\Models\Concerns\Auditable;
use App\Models\Concerns\BelongsToTenant;
use App\Models\Concerns\BranchScoped;
use App\Models\Customers\Customer;
use App\Models\Lookups\PaymentMethod;
use App\Models\Lookups\Status;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\SoftDeletes;

class Transaction extends Model
{
    use Auditable, BelongsToTenant, BranchScoped, SoftDeletes;

    protected $fillable = [
        'transaction_number', 'customer_id', 'branch_id',
        'payment_method_id', 'status_id', 'amount', 'used_amount',
        'usable_amount', 'usable_pending', 'reference', 'note',
        'taken_on', 'cheque_number', 'cheque_bank', 'cheque_branch_code',
        'cheque_date', 'cheque_reminder_sent_at', 'custom_fields',
    ];

    protected function casts(): array
    {
        return [
            'amount' => 'decimal:2',
            'used_amount' => 'decimal:2',
            'usable_amount' => 'decimal:2',
            'usable_pending' => 'decimal:2',
            'taken_on' => 'datetime',
            'cheque_date' => 'date',
            'cheque_reminder_sent_at' => 'datetime',
            'custom_fields' => 'array',
        ];
    }

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

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

    public function method(): BelongsTo
    {
        return $this->belongsTo(PaymentMethod::class, 'payment_method_id');
    }

    public function paymentMethod(): BelongsTo
    {
        return $this->belongsTo(PaymentMethod::class, 'payment_method_id');
    }

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

    public function paymentStatus(): BelongsTo
    {
        return $this->belongsTo(Status::class, 'status_id');
    }

    public function claims(): HasMany
    {
        return $this->hasMany(TransactionClaim::class);
    }
}
