<?php

declare(strict_types=1);

namespace App\Models;

use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\SoftDeletes;

class AcademyChallenge extends Model
{
    use SoftDeletes;

    public const STATUS_DRAFT = 'draft';
    public const STATUS_SCHEDULED = 'scheduled';
    public const STATUS_ACTIVE = 'active';
    public const STATUS_VOTING = 'voting';
    public const STATUS_COMPLETED = 'completed';
    public const STATUS_ARCHIVED = 'archived';

    protected $fillable = [
        'title',
        'slug',
        'excerpt',
        'description',
        'brief',
        'rules',
        'access_level',
        'status',
        'starts_at',
        'ends_at',
        'voting_starts_at',
        'voting_ends_at',
        'cover_image',
        'prize_text',
        'required_tags',
        'allowed_categories',
        'featured',
        'active',
    ];

    protected $casts = [
        'starts_at' => 'datetime',
        'ends_at' => 'datetime',
        'voting_starts_at' => 'datetime',
        'voting_ends_at' => 'datetime',
        'required_tags' => 'array',
        'allowed_categories' => 'array',
        'featured' => 'boolean',
        'active' => 'boolean',
    ];

    public function scopeActive(Builder $query): Builder
    {
        return $query->where('active', true);
    }

    public function scopePubliclyVisible(Builder $query): Builder
    {
        return $query->active()->whereIn('status', [
            self::STATUS_ACTIVE,
            self::STATUS_VOTING,
            self::STATUS_COMPLETED,
            self::STATUS_ARCHIVED,
            self::STATUS_SCHEDULED,
        ]);
    }

    public function submissions(): HasMany
    {
        return $this->hasMany(AcademyChallengeSubmission::class, 'challenge_id');
    }
}