<?php

namespace Database\Seeders;

use App\Models\MatchPrediction;
use App\Models\VipPackage;
use Carbon\Carbon;
use Illuminate\Database\Seeder;

class MatchPredictionSeeder extends Seeder
{
    /**
     * Run the database seeds.
     */
    public function run(): void
    {
        // Resolve package IDs by title
        $pkgFree     = VipPackage::where('title', 'Free')->first();
        $pkgBetOfDay = VipPackage::where('title', 'Bet of the Day')->first();
        $pkgSuper    = VipPackage::where('title', 'Super Single')->first();
        $pkgRollover = VipPackage::where('title', '1.4 Rollover')->first();
        $pkgHalfTime = VipPackage::where('title', 'Half Time Draw')->first();

        $today = Carbon::today();

        // ── Past 5 days free predictions ────────────────────────────────
        $history = [
            // 5 days ago — WON
            [
                'date'    => $today->copy()->subDays(5),
                'package' => $pkgFree,
                'league'  => 'Premier League',
                'team1'   => 'Manchester United',
                'team2'   => 'Chelsea',
                'selection' => 'Over 2.5 Goals (Result: 2-2)',
                'status'  => 'won',
            ],
            // 4 days ago — WON
            [
                'date'    => $today->copy()->subDays(4),
                'package' => $pkgFree,
                'league'  => 'Serie A',
                'team1'   => 'AC Milan',
                'team2'   => 'Juventus',
                'selection' => 'AC Milan Win (Result: 2-1)',
                'status'  => 'won',
            ],
            // 3 days ago — LOST
            [
                'date'    => $today->copy()->subDays(3),
                'package' => $pkgFree,
                'league'  => 'Champions League',
                'team1'   => 'Real Madrid',
                'team2'   => 'AC Milan',
                'selection' => 'Real Madrid Win (Result: 1-3)',
                'status'  => 'lost',
            ],
            // 2 days ago — WON
            [
                'date'    => $today->copy()->subDays(2),
                'package' => $pkgFree,
                'league'  => 'Bundesliga',
                'team1'   => 'Bayern Munich',
                'team2'   => 'Leverkusen',
                'selection' => 'Both Teams To Score (Result: 2-2)',
                'status'  => 'won',
            ],
            // 1 day ago — WON
            [
                'date'    => $today->copy()->subDays(1),
                'package' => $pkgFree,
                'league'  => 'Premier League',
                'team1'   => 'Man City',
                'team2'   => 'Arsenal',
                'selection' => 'Over 1.5 Goals (Result: 2-2)',
                'status'  => 'won',
            ],
        ];

        foreach ($history as $item) {
            if (!$item['package']) continue;
            MatchPrediction::updateOrCreate(
                [
                    'vip_package_id'  => $item['package']->id,
                    'prediction_date' => $item['date']->toDateString(),
                    'team1'           => $item['team1'],
                    'team2'           => $item['team2'],
                ],
                [
                    'league'    => $item['league'],
                    'selection' => $item['selection'],
                    'status'    => $item['status'],
                ]
            );
        }

        // ── Today's free selections ──────────────────────────────────────
        if ($pkgFree) {
            $todayFree = [
                [
                    'league'    => 'Premier League',
                    'team1'     => 'Arsenal',
                    'team2'     => 'Brighton',
                    'selection' => 'Over 2.5 Goals',
                    'status'    => 'pending',
                ],
                [
                    'league'    => 'La Liga',
                    'team1'     => 'Getafe',
                    'team2'     => 'Sevilla',
                    'selection' => 'Draw X',
                    'status'    => 'pending',
                ],
            ];

            foreach ($todayFree as $item) {
                MatchPrediction::updateOrCreate(
                    [
                        'vip_package_id'  => $pkgFree->id,
                        'prediction_date' => $today->toDateString(),
                        'team1'           => $item['team1'],
                        'team2'           => $item['team2'],
                    ],
                    [
                        'league'    => $item['league'],
                        'selection' => $item['selection'],
                        'status'    => $item['status'],
                    ]
                );
            }
        }

        // ── Today's VIP (locked) predictions ─────────────────────────────
        $todayVip = [
            [
                'package'   => $pkgBetOfDay,
                'league'    => 'Premier League',
                'team1'     => 'Man City',
                'team2'     => 'Liverpool',
                'selection' => 'Man City Win',
                'status'    => 'pending',
            ],
            [
                'package'   => $pkgSuper,
                'league'    => 'La Liga',
                'team1'     => 'Real Madrid',
                'team2'     => 'Barcelona',
                'selection' => 'Over 1.5 Goals',
                'status'    => 'pending',
            ],
            [
                'package'   => $pkgRollover,
                'league'    => 'Champions League',
                'team1'     => 'Bayern Munich',
                'team2'     => 'PSG',
                'selection' => 'Both Teams To Score',
                'status'    => 'pending',
            ],
            [
                'package'   => $pkgHalfTime,
                'league'    => 'Serie A',
                'team1'     => 'Inter Milan',
                'team2'     => 'Napoli',
                'selection' => 'Half Time Draw',
                'status'    => 'pending',
            ],
        ];

        foreach ($todayVip as $item) {
            if (!$item['package']) continue;
            MatchPrediction::updateOrCreate(
                [
                    'vip_package_id'  => $item['package']->id,
                    'prediction_date' => $today->toDateString(),
                    'team1'           => $item['team1'],
                    'team2'           => $item['team2'],
                ],
                [
                    'league'    => $item['league'],
                    'selection' => $item['selection'],
                    'status'    => $item['status'],
                ]
            );
        }
    }
}
