<?php

namespace App\Models;

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

class MatchContest extends LegacyModel
{
    public const TYPE_PAID = 'paid';
    public const TYPE_FREE = 'free';

    protected $table = 'matchcobntests';

    protected function casts(): array
    {
        return [
            'contest_type' => 'string',
            'totalamount' => 'float',
            'contestseat' => 'integer',
            'winpertange' => 'float',
            'userentery' => 'integer',
            'joinamt' => 'float',
            'createdtime' => 'datetime',
            'updatetime' => 'datetime',
        ];
    }

    public function scopeOfType(Builder $query, string $contestType): Builder
    {
        return $query->where('contest_type', $contestType);
    }

    public function scopePaid(Builder $query): Builder
    {
        return $query->ofType(self::TYPE_PAID);
    }

    public function scopeFree(Builder $query): Builder
    {
        return $query->ofType(self::TYPE_FREE);
    }

    public function isFree(): bool
    {
        return $this->contest_type === self::TYPE_FREE;
    }

    public function isPaid(): bool
    {
        return $this->contest_type !== self::TYPE_FREE;
    }

    public function getContestTypeLabelAttribute(): string
    {
        return $this->isFree() ? 'Free Exam' : 'Paid Competition';
    }

    public function assignments(): HasMany
    {
        return $this->hasMany(CompetitionAssignment::class, 'compitetionid');
    }

    public function gameplayEntries(): HasMany
    {
        return $this->hasMany(GameplayHistory::class, 'contestid');
    }
}
