<?php

namespace App\Models;

use Database\Factories\TicketPurchaseRequestFactory;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Support\Carbon;

/**
 * @property int $id
 * @property int $draw_id
 * @property int $user_id
 * @property int $quantity
 * @property array<array-key, mixed> $pricing_tier
 * @property string $status
 * @property Carbon|null $created_at
 * @property Carbon|null $updated_at
 * @property-read Draw $draw
 * @property-read User $user
 *
 * @method static \Database\Factories\TicketPurchaseRequestFactory factory($count = null, $state = [])
 * @method static \Illuminate\Database\Eloquent\Builder<static>|TicketPurchaseRequest fulfilled()
 * @method static \Illuminate\Database\Eloquent\Builder<static>|TicketPurchaseRequest newModelQuery()
 * @method static \Illuminate\Database\Eloquent\Builder<static>|TicketPurchaseRequest newQuery()
 * @method static \Illuminate\Database\Eloquent\Builder<static>|TicketPurchaseRequest pending()
 * @method static \Illuminate\Database\Eloquent\Builder<static>|TicketPurchaseRequest query()
 * @method static \Illuminate\Database\Eloquent\Builder<static>|TicketPurchaseRequest whereCreatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder<static>|TicketPurchaseRequest whereDrawId($value)
 * @method static \Illuminate\Database\Eloquent\Builder<static>|TicketPurchaseRequest whereId($value)
 * @method static \Illuminate\Database\Eloquent\Builder<static>|TicketPurchaseRequest wherePricingTier($value)
 * @method static \Illuminate\Database\Eloquent\Builder<static>|TicketPurchaseRequest whereQuantity($value)
 * @method static \Illuminate\Database\Eloquent\Builder<static>|TicketPurchaseRequest whereStatus($value)
 * @method static \Illuminate\Database\Eloquent\Builder<static>|TicketPurchaseRequest whereUpdatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder<static>|TicketPurchaseRequest whereUserId($value)
 *
 * @mixin \Eloquent
 */
class TicketPurchaseRequest extends Model
{
    /** @use HasFactory<TicketPurchaseRequestFactory> */
    use HasFactory;

    /**
     * The attributes that are mass assignable.
     *
     * @var list<string>
     */
    protected $fillable = [
        'draw_id',
        'user_id',
        'quantity',
        'pricing_tier',
        'status',
    ];

    /**
     * Get the attributes that should be cast.
     *
     * @return array<string, string>
     */
    protected function casts(): array
    {
        return [
            'pricing_tier' => 'array',
        ];
    }

    /**
     * Get the draw for this purchase request.
     *
     * @return BelongsTo<Draw, $this>
     */
    public function draw(): BelongsTo
    {
        return $this->belongsTo(Draw::class);
    }

    /**
     * Get the user who made this purchase request.
     *
     * @return BelongsTo<User, $this>
     */
    public function user(): BelongsTo
    {
        return $this->belongsTo(User::class);
    }

    /**
     * Scope a query to only include pending requests.
     *
     * @param  Builder<static>  $query
     * @return Builder<static>
     */
    public function scopePending(Builder $query): Builder
    {
        return $query->where('status', 'pending');
    }

    /**
     * Scope a query to only include fulfilled requests.
     *
     * @param  Builder<static>  $query
     * @return Builder<static>
     */
    public function scopeFulfilled(Builder $query): Builder
    {
        return $query->where('status', 'fulfilled');
    }
}
