<?php

namespace App\Domains\Ticket\Models;

use App\Domains\Auth\Models\User;
use App\Domains\Catalog\Models\CatalogItem;
use App\Domains\Catalog\Models\Variant;
use App\Domains\Purchase\Models\Purchase;
use App\Domains\Tenant\Models\Tenant;
use App\Domains\Ticket\Services\ResolvedTicketValidity;
use App\Domains\Ticket\Services\ResolvedValidityGroup;
use App\Domains\Ticket\Services\TicketPresentationResolver;
use App\Domains\Ticket\Services\TicketValidityResolver;
use Carbon\CarbonInterface;
use Illuminate\Database\Eloquent\Attributes\Fillable;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Support\Collection;

#[Fillable([
    'tenant_code',
    'ticket',
    'source_purchase_id',
    'source_catalog_item_id',
    'source_variant_id',
    'used_at',
    'scanner_user_id',
    'user_id',
])]
class Ticket extends Model
{
    use HasFactory;

    private ?ResolvedTicketValidity $resolvedValidity = null;

    public const STATUS_ACTIVE = 'active';

    public const STATUS_EXPIRED = 'expired';

    public const STATUS_USED = 'used';

    public $timestamps = false;

    protected $appends = [
        'name',
        'description',
        'is_valid',
        'is_expired',
        'is_used',
        'status',
    ];

    protected function casts(): array
    {
        return [
            'source_catalog_item_id' => 'integer',
            'source_variant_id' => 'integer',
            'source_purchase_id' => 'integer',
            'used_at' => 'datetime',
            'scanner_user_id' => 'integer',
            'user_id' => 'integer',
        ];
    }

    /** @return BelongsTo<Tenant, $this> */
    public function tenant(): BelongsTo
    {
        return $this->belongsTo(Tenant::class, 'tenant_code', 'codigo');
    }

    /** @return BelongsTo<User, $this> */
    public function user(): BelongsTo
    {
        return $this->belongsTo(User::class);
    }

    /** @return BelongsTo<User, $this> */
    public function scannerUser(): BelongsTo
    {
        return $this->belongsTo(User::class, 'scanner_user_id');
    }

    /** @return BelongsTo<Purchase, $this> */
    public function sourcePurchase(): BelongsTo
    {
        return $this->belongsTo(Purchase::class, 'source_purchase_id');
    }

    /** @return BelongsTo<CatalogItem, $this> */
    public function sourceCatalogItem(): BelongsTo
    {
        return $this->belongsTo(CatalogItem::class, 'source_catalog_item_id')->withTrashed();
    }

    /** @return BelongsTo<Variant, $this> */
    public function sourceVariant(): BelongsTo
    {
        return $this->belongsTo(Variant::class, 'source_variant_id')->withTrashed();
    }

    public function isValid(): bool
    {
        if ($this->used_at !== null) {
            return false;
        }

        return $this->resolvedValidity()->isValid();
    }

    public function getIsValidAttribute(): bool
    {
        return $this->isValid();
    }

    public function getIsExpiredAttribute(): bool
    {
        return $this->used_at === null && $this->resolvedValidity()->isExpired();
    }

    public function getIsUsedAttribute(): bool
    {
        return $this->used_at !== null;
    }

    public function getStatusAttribute(): string
    {
        if ($this->is_used) {
            return self::STATUS_USED;
        }

        if ($this->is_expired) {
            return self::STATUS_EXPIRED;
        }

        return self::STATUS_ACTIVE;
    }

    public function getNameAttribute(): string
    {
        return app(TicketPresentationResolver::class)->name($this);
    }

    public function getDescriptionAttribute(): string
    {
        return app(TicketPresentationResolver::class)->description($this);
    }

    public function getEffectiveStartsAt(): ?CarbonInterface
    {
        return $this->resolvedValidity()->effectiveStartsAt();
    }

    public function getEffectiveExpiresAt(): ?CarbonInterface
    {
        return $this->resolvedValidity()->effectiveExpiresAt();
    }

    /** @return Collection<int, ResolvedValidityGroup> */
    public function resolvedValidityGroups(): Collection
    {
        return $this->resolvedValidity()->groups;
    }

    public function resolvedValidity(): ResolvedTicketValidity
    {
        return $this->resolvedValidity ??= app(TicketValidityResolver::class)->resolveTicket($this);
    }
}
