<?php

namespace App\Domain\Training\Models;

use App\Domain\Participant\Models\Participant;
use App\Domain\Shared\Traits\BelongsToAcademy;
use App\Domain\Training\Enums\WaitlistResponse;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;

class Waitlist extends Model
{
    use BelongsToAcademy;

    protected $fillable = [
        'academy_id', 'participant_id', 'training_group_id',
        'position', 'requested_at', 'notified_at', 'responded_at',
        'response', 'expires_at', 'enrolled_enrollment_id', 'notes',
    ];

    protected $casts = [
        'position' => 'integer',
        'requested_at' => 'datetime',
        'notified_at' => 'datetime',
        'responded_at' => 'datetime',
        'response' => WaitlistResponse::class,
        'expires_at' => 'datetime',
    ];

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

    public function group(): BelongsTo
    {
        return $this->belongsTo(TrainingGroup::class, 'training_group_id');
    }

    public function enrollment(): BelongsTo
    {
        return $this->belongsTo(Enrollment::class, 'enrolled_enrollment_id');
    }

    public function scopePending($query)
    {
        return $query->whereNull('response')->orderBy('position');
    }
}
