<?php

namespace App\Models\Vouchers;

use App\Models\Branch;
use App\Models\Concerns\Auditable;
use App\Models\Concerns\BelongsToTenant;
use App\Models\Concerns\NullableBranchScoped;
use App\Models\Customers\Customer;
use App\Models\Lookups\DiscountType;
use App\Models\Lookups\Status;
use App\Models\Lookups\VoucherType;
use App\Models\Sales\Invoice;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Support\Str;

class Voucher extends Model
{
    use Auditable, BelongsToTenant, NullableBranchScoped, SoftDeletes;

    protected $fillable = [
        'uuid', 'code', 'name', 'voucher_type_id', 'discount_type_id', 'discount_value',
        'initial_balance', 'remaining_balance', 'minimum_purchase', 'maximum_discount',
        'usage_limit', 'used_count', 'is_partial_redemption', 'is_stackable',
        'is_active', 'customer_id', 'branch_id', 'valid_from', 'valid_until', 'status_id',
        'issued_by_invoice_id', 'notes', 'created_by', 'updated_by', 'deleted_by',
    ];

    protected function casts(): array
    {
        return [
            'discount_value' => 'decimal:2',
            'initial_balance' => 'decimal:2',
            'remaining_balance' => 'decimal:2',
            'minimum_purchase' => 'decimal:2',
            'maximum_discount' => 'decimal:2',
            'usage_limit' => 'integer',
            'used_count' => 'integer',
            'is_partial_redemption' => 'boolean',
            'is_stackable' => 'boolean',
            'is_active' => 'boolean',
            'valid_from' => 'datetime',
            'valid_until' => 'datetime',
        ];
    }

    protected static function booted(): void
    {
        static::creating(function (self $voucher): void {
            if (! $voucher->uuid) {
                $voucher->uuid = (string) Str::ulid();
            }
        });
    }

    public function type(): BelongsTo
    {
        return $this->belongsTo(VoucherType::class, 'voucher_type_id');
    }

    public function discountType(): BelongsTo
    {
        return $this->belongsTo(DiscountType::class, 'discount_type_id');
    }

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

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

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

    public function issuedByInvoice(): BelongsTo
    {
        return $this->belongsTo(Invoice::class, 'issued_by_invoice_id');
    }

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

    public function isBalanceBased(): bool
    {
        return (bool) $this->type?->is_balance_based;
    }

    public function isActive(): bool
    {
        return $this->is_active && $this->status?->code === Status::VOUCHER_ACTIVE;
    }
}
