<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;

class FootballMatch extends Model
{
    // Explicit table name — avoids any confusion with PHP's match keyword
    protected $table = 'football_matches';

    protected $fillable = [
        'home_team_id',
        'away_team_id',
        'match_datetime',
        'venue',
        'stage',
        'group_name',
        'status',
        'home_score',
        'away_score',
        'home_community_votes',
        'away_community_votes',
        'draw_community_votes',
        // AI prediction fields
        'home_win_probability',
        'draw_probability',
        'away_win_probability',
        'confidence_level',
        'ai_analysis',
        'key_insights',
        'predicted_score_home',
        'predicted_score_away',
    ];

    protected function casts(): array
    {
        return [
            'match_datetime'        => 'datetime',
            'home_win_probability'  => 'decimal:2',
            'draw_probability'      => 'decimal:2',
            'away_win_probability'  => 'decimal:2',
            'key_insights'          => 'array',
        ];
    }

    // ── Relationships ──────────────────────────────────────────────

    public function homeTeam(): BelongsTo
    {
        return $this->belongsTo(Team::class, 'home_team_id');
    }

    public function awayTeam(): BelongsTo
    {
        return $this->belongsTo(Team::class, 'away_team_id');
    }

    public function predictions(): HasMany
    {
        return $this->hasMany(Prediction::class, 'football_match_id');
    }

    public function polls(): HasMany
    {
        return $this->hasMany(Poll::class, 'football_match_id');
    }

    // ── Helpers ────────────────────────────────────────────────────

    public function isUpcoming(): bool
    {
        return $this->status === 'upcoming';
    }

    public function isLive(): bool
    {
        return $this->status === 'live';
    }

    public function isFinished(): bool
    {
        return $this->status === 'finished';
    }

    public function isPredictionLocked(): bool
    {
        return $this->status !== 'upcoming';
    }

    /**
     * Kickoff time converted to the admin panel's display timezone
     * (config('app.admin_timezone'), e.g. Asia/Dhaka GMT+6).
     *
     * The underlying `match_datetime` attribute remains UTC for storage
     * and for the API (which the Flutter app converts to each user's
     * own local timezone). This accessor exists purely so the admin
     * panel can display/edit times in the admin's own timezone.
     */
    public function getMatchDatetimeAdminAttribute(): \Illuminate\Support\Carbon
    {
        return $this->match_datetime->copy()->setTimezone(config('app.admin_timezone'));
    }

    /**
     * Whether the admin has set an AI prediction for this match.
     * True if probabilities sum to > 0.
     */
    public function hasAiPrediction(): bool
    {
        return ((float) $this->home_win_probability
              + (float) $this->draw_probability
              + (float) $this->away_win_probability) > 0;
    }
}
