<?php

namespace Database\Seeders;

use App\Models\FootballMatch;
use App\Models\Player;
use App\Models\Sponsor;
use App\Models\Team;
use App\Models\User;
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\Hash;

class DatabaseSeeder extends Seeder
{
    public function run(): void
    {
        // Admin user
        User::firstOrCreate(['email' => 'admin@svishtov.bg'], [
            'name'     => 'Администратор',
            'password' => Hash::make('changeme123'),
            'role'     => 'admin',
            'active'   => true,
        ]);

        User::firstOrCreate(['email' => 'operator@svishtov.bg'], [
            'name'     => 'Оператор',
            'password' => Hash::make('changeme123'),
            'role'     => 'operator',
            'active'   => true,
        ]);

        // Home team — Академик Свищов
        $akademik = Team::firstOrCreate(['name' => 'АКАДЕМИК СВИЩОВ'], [
            'short'        => 'АКА',
            'letter'       => 'A',
            'color'        => '#1e5ee8',
            'city'         => 'Свищов',
            'founded'      => 1922,
            'coach'        => 'Стоян Йорданов',
            'stadium'      => 'Академик',
            'is_home_team' => true,
        ]);

        // Away teams
        $levski = Team::firstOrCreate(['name' => 'ФК ЛЕВСКИ'], [
            'short'   => 'ЛЕВ',
            'letter'  => 'Л',
            'color'   => '#1565c0',
            'city'    => 'София',
            'founded' => 1914,
            'coach'   => 'Хулио Веласкес',
            'stadium' => 'Георги Аспарухов',
        ]);

        $cska = Team::firstOrCreate(['name' => 'ЦСКА СОФИЯ'], [
            'short'   => 'ЦСКА',
            'letter'  => 'Ц',
            'color'   => '#dc2626',
            'city'    => 'София',
            'founded' => 1948,
            'coach'   => 'Ален Пардю',
            'stadium' => 'Българска армия',
        ]);

        // Full squad — Академик Свищов (18 players)
        $akademikPlayers = [
            ['number' => 1,  'name' => 'Георги Георгиев',   'position' => 'GK',  'age' => 28, 'matches_played' => 22, 'goals' => 0,  'assists' => 0, 'yellow_cards' => 1, 'red_cards' => 0],
            ['number' => 2,  'name' => 'Пламен Иванов',     'position' => 'DEF', 'age' => 25, 'matches_played' => 20, 'goals' => 1,  'assists' => 1, 'yellow_cards' => 3, 'red_cards' => 0],
            ['number' => 3,  'name' => 'Стефан Димитров',   'position' => 'DEF', 'age' => 27, 'matches_played' => 18, 'goals' => 0,  'assists' => 2, 'yellow_cards' => 4, 'red_cards' => 1],
            ['number' => 4,  'name' => 'Красимир Петров',   'position' => 'DEF', 'age' => 24, 'matches_played' => 21, 'goals' => 2,  'assists' => 0, 'yellow_cards' => 2, 'red_cards' => 0],
            ['number' => 5,  'name' => 'Виктор Стоянов',    'position' => 'DEF', 'age' => 23, 'matches_played' => 19, 'goals' => 0,  'assists' => 1, 'yellow_cards' => 5, 'red_cards' => 0],
            ['number' => 6,  'name' => 'Йордан Тодоров',    'position' => 'MID', 'age' => 26, 'matches_played' => 22, 'goals' => 3,  'assists' => 4, 'yellow_cards' => 2, 'red_cards' => 0],
            ['number' => 7,  'name' => 'Николай Николов',   'position' => 'MID', 'age' => 22, 'matches_played' => 20, 'goals' => 5,  'assists' => 3, 'yellow_cards' => 1, 'red_cards' => 0],
            ['number' => 8,  'name' => 'Ивайло Маринов',    'position' => 'MID', 'age' => 29, 'matches_played' => 17, 'goals' => 2,  'assists' => 6, 'yellow_cards' => 3, 'red_cards' => 0],
            ['number' => 9,  'name' => 'Димитър Костов',    'position' => 'FWD', 'age' => 24, 'matches_played' => 22, 'goals' => 11, 'assists' => 4, 'yellow_cards' => 2, 'red_cards' => 0],
            ['number' => 10, 'name' => 'Александър Атанасов','position' => 'MID', 'age' => 26, 'matches_played' => 21, 'goals' => 7,  'assists' => 8, 'yellow_cards' => 1, 'red_cards' => 0],
            ['number' => 11, 'name' => 'Борислав Василев',  'position' => 'FWD', 'age' => 21, 'matches_played' => 18, 'goals' => 6,  'assists' => 2, 'yellow_cards' => 0, 'red_cards' => 0],
            ['number' => 14, 'name' => 'Радослав Радев',    'position' => 'MID', 'age' => 25, 'matches_played' => 15, 'goals' => 1,  'assists' => 2, 'yellow_cards' => 2, 'red_cards' => 0],
            ['number' => 17, 'name' => 'Христо Христов',    'position' => 'FWD', 'age' => 23, 'matches_played' => 12, 'goals' => 3,  'assists' => 1, 'yellow_cards' => 1, 'red_cards' => 0],
            ['number' => 20, 'name' => 'Добри Добрев',      'position' => 'GK',  'age' => 22, 'matches_played' => 2,  'goals' => 0,  'assists' => 0, 'yellow_cards' => 0, 'red_cards' => 0],
            ['number' => 21, 'name' => 'Светослав Иванов',  'position' => 'DEF', 'age' => 20, 'matches_played' => 10, 'goals' => 0,  'assists' => 1, 'yellow_cards' => 1, 'red_cards' => 0],
            ['number' => 23, 'name' => 'Мирослав Миков',    'position' => 'MID', 'age' => 24, 'matches_played' => 14, 'goals' => 2,  'assists' => 3, 'yellow_cards' => 2, 'red_cards' => 0],
            ['number' => 25, 'name' => 'Валентин Стефанов', 'position' => 'FWD', 'age' => 22, 'matches_played' => 9,  'goals' => 2,  'assists' => 0, 'yellow_cards' => 0, 'red_cards' => 0],
            ['number' => 30, 'name' => 'Тихомир Тодоров',   'position' => 'DEF', 'age' => 19, 'matches_played' => 6,  'goals' => 0,  'assists' => 0, 'yellow_cards' => 1, 'red_cards' => 0],
        ];

        foreach ($akademikPlayers as $p) {
            Player::firstOrCreate(
                ['team_id' => $akademik->id, 'number' => $p['number']],
                array_merge($p, [
                    'team_id'   => $akademik->id,
                    'photo_url' => '/assets/player-placeholder.png',
                ])
            );
        }

        // Squad — ФК Левски (11 players)
        $levskiPlayers = [
            ['number' => 1,  'name' => 'Николай Попов',     'position' => 'GK',  'age' => 30, 'matches_played' => 22, 'goals' => 0,  'assists' => 0, 'yellow_cards' => 0, 'red_cards' => 0],
            ['number' => 2,  'name' => 'Антон Антонов',     'position' => 'DEF', 'age' => 24, 'matches_played' => 20, 'goals' => 1,  'assists' => 0, 'yellow_cards' => 3, 'red_cards' => 0],
            ['number' => 3,  'name' => 'Тодор Тодоров',     'position' => 'DEF', 'age' => 26, 'matches_played' => 19, 'goals' => 0,  'assists' => 1, 'yellow_cards' => 2, 'red_cards' => 1],
            ['number' => 5,  'name' => 'Свилен Свиленов',   'position' => 'DEF', 'age' => 27, 'matches_played' => 18, 'goals' => 0,  'assists' => 0, 'yellow_cards' => 4, 'red_cards' => 0],
            ['number' => 6,  'name' => 'Борис Борисов',     'position' => 'MID', 'age' => 25, 'matches_played' => 21, 'goals' => 2,  'assists' => 3, 'yellow_cards' => 2, 'red_cards' => 0],
            ['number' => 7,  'name' => 'Кирил Александров', 'position' => 'MID', 'age' => 23, 'matches_played' => 22, 'goals' => 6,  'assists' => 4, 'yellow_cards' => 1, 'red_cards' => 0],
            ['number' => 8,  'name' => 'Венцислав Петков',  'position' => 'MID', 'age' => 28, 'matches_played' => 20, 'goals' => 3,  'assists' => 5, 'yellow_cards' => 3, 'red_cards' => 0],
            ['number' => 9,  'name' => 'Виктор Радев',      'position' => 'FWD', 'age' => 24, 'matches_played' => 22, 'goals' => 13, 'assists' => 2, 'yellow_cards' => 1, 'red_cards' => 0],
            ['number' => 10, 'name' => 'Лъчезар Илиев',    'position' => 'MID', 'age' => 26, 'matches_played' => 21, 'goals' => 8,  'assists' => 9, 'yellow_cards' => 2, 'red_cards' => 0],
            ['number' => 11, 'name' => 'Деян Симеонов',     'position' => 'FWD', 'age' => 22, 'matches_played' => 18, 'goals' => 7,  'assists' => 3, 'yellow_cards' => 0, 'red_cards' => 0],
            ['number' => 20, 'name' => 'Любомир Любенов',   'position' => 'GK',  'age' => 21, 'matches_played' => 3,  'goals' => 0,  'assists' => 0, 'yellow_cards' => 0, 'red_cards' => 0],
        ];

        foreach ($levskiPlayers as $p) {
            Player::firstOrCreate(
                ['team_id' => $levski->id, 'number' => $p['number']],
                array_merge($p, [
                    'team_id'   => $levski->id,
                    'photo_url' => '/assets/player-placeholder.png',
                ])
            );
        }

        // Squad — ЦСКА СОФИЯ (11 players)
        $cskaPlayers = [
            ['number' => 1,  'name' => 'Мартин Мартинов',   'position' => 'GK',  'age' => 29, 'matches_played' => 22, 'goals' => 0,  'assists' => 0, 'yellow_cards' => 0, 'red_cards' => 0],
            ['number' => 4,  'name' => 'Росен Росенов',     'position' => 'DEF', 'age' => 26, 'matches_played' => 20, 'goals' => 1,  'assists' => 0, 'yellow_cards' => 5, 'red_cards' => 1],
            ['number' => 5,  'name' => 'Галин Галинов',     'position' => 'DEF', 'age' => 25, 'matches_played' => 21, 'goals' => 0,  'assists' => 1, 'yellow_cards' => 3, 'red_cards' => 0],
            ['number' => 7,  'name' => 'Симон Симонов',     'position' => 'MID', 'age' => 24, 'matches_played' => 22, 'goals' => 5,  'assists' => 6, 'yellow_cards' => 2, 'red_cards' => 0],
            ['number' => 9,  'name' => 'Красен Красенов',   'position' => 'FWD', 'age' => 27, 'matches_played' => 22, 'goals' => 15, 'assists' => 3, 'yellow_cards' => 2, 'red_cards' => 0],
            ['number' => 10, 'name' => 'Тодор Тодоров',     'position' => 'MID', 'age' => 25, 'matches_played' => 20, 'goals' => 9,  'assists' => 7, 'yellow_cards' => 1, 'red_cards' => 0],
            ['number' => 11, 'name' => 'Емил Емилов',       'position' => 'FWD', 'age' => 23, 'matches_played' => 19, 'goals' => 8,  'assists' => 4, 'yellow_cards' => 1, 'red_cards' => 0],
        ];

        foreach ($cskaPlayers as $p) {
            Player::firstOrCreate(
                ['team_id' => $cska->id, 'number' => $p['number']],
                array_merge($p, [
                    'team_id'   => $cska->id,
                    'photo_url' => '/assets/player-placeholder.png',
                ])
            );
        }

        // Demo matches
        FootballMatch::firstOrCreate(
            ['home_team_id' => $akademik->id, 'away_team_id' => $levski->id],
            [
                'date'          => now()->setTime(20, 0),
                'venue'         => 'Стадион Академик, Свищов',
                'competition'   => 'Трета лига — Група Изток',
                'status'        => 'scheduled',
                'score_home'    => 0,
                'score_away'    => 0,
                'period'        => 'pre',
                'show_overlay'  => true,
                'show_sponsors' => true,
            ]
        );

        FootballMatch::firstOrCreate(
            ['home_team_id' => $levski->id, 'away_team_id' => $akademik->id],
            [
                'date'          => now()->subWeeks(1)->setTime(19, 0),
                'venue'         => 'Стадион Георги Аспарухов, София',
                'competition'   => 'Трета лига — Група Изток',
                'status'        => 'finished',
                'score_home'    => 1,
                'score_away'    => 2,
                'period'        => 'full',
                'show_overlay'  => true,
                'show_sponsors' => true,
            ]
        );

        FootballMatch::firstOrCreate(
            ['home_team_id' => $akademik->id, 'away_team_id' => $cska->id],
            [
                'date'          => now()->addWeeks(1)->setTime(19, 30),
                'venue'         => 'Стадион Академик, Свищов',
                'competition'   => 'Купа на България',
                'status'        => 'scheduled',
                'score_home'    => 0,
                'score_away'    => 0,
                'period'        => 'pre',
                'show_overlay'  => true,
                'show_sponsors' => true,
            ]
        );

        // Sponsors
        $sponsors = [
            ['name' => 'ОБЩИНА СВИЩОВ', 'subtitle' => 'официален партньор', 'tier' => 'main', 'sort_order' => 0],
            ['name' => 'WINBET',        'tier' => 'main',    'sort_order' => 1],
            ['name' => 'inbet',         'tier' => 'partner', 'sort_order' => 2],
            ['name' => 'JAKO',          'tier' => 'partner', 'sort_order' => 3, 'accent' => '⚽'],
        ];

        foreach ($sponsors as $s) {
            Sponsor::firstOrCreate(
                ['name' => $s['name']],
                array_merge($s, ['visible' => true, 'active' => true])
            );
        }
    }
}
