<?php

namespace App\Http\Controllers\Preview;

use App\Http\Controllers\Controller;
use App\Services\MockDataService;
use App\ViewModels\DashboardPresenter;
use Illuminate\Http\Request;

class PreviewController extends Controller
{
    // ----------------------------------------------------------------
    // Public pages
    // ----------------------------------------------------------------
    public function landing()
    {
        return view('pages.landing.index', [
            'stats'       => MockDataService::platformStats(),
            'games'       => MockDataService::games(),
            'tournaments' => MockDataService::featuredTournaments(),
            'feed'        => array_slice(MockDataService::feed(), 0, 4),
            'subscriptions' => MockDataService::subscriptions(),
            'topPlayers'  => array_slice(MockDataService::rankings(), 0, 5),
        ]);
    }

    // ----------------------------------------------------------------
    // Auth pages
    // ----------------------------------------------------------------
    public function login()    { return view('pages.auth.login'); }
    public function register() { return view('pages.auth.register'); }
    public function forgotPassword() { return view('pages.auth.forgot-password'); }

    // ----------------------------------------------------------------
    // App pages
    // ----------------------------------------------------------------
    public function dashboard(DashboardPresenter $presenter)
    {
        $dashboard = $presenter->present(useMock: true);

        return view('pages.dashboard.index', [
            'dashboard' => $dashboard,
            'featuredTournament' => $dashboard['featuredTournament'],
        ]);
    }

    public function discover()
    {
        return view('pages.discover.index', [
            'players'     => MockDataService::players(),
            'teams'       => MockDataService::teams(),
            'tournaments' => MockDataService::tournaments(),
            'games'       => MockDataService::games(),
        ]);
    }

    // ----------------------------------------------------------------
    // Players
    // ----------------------------------------------------------------
    public function players(Request $request)
    {
        $filters = $request->only(['game', 'country', 'looking_for_team', 'rank']);
        return view('pages.players.index', [
            'players' => MockDataService::players($filters),
            'games'   => MockDataService::games(),
            'filters' => $filters,
        ]);
    }

    public function playerProfile(string $slug)
    {
        $player = MockDataService::player($slug) ?? MockDataService::players()[0];
        return view('pages.players.show', [
            'player'   => $player,
            'feed'     => array_slice(MockDataService::feed(), 0, 3),
        ]);
    }

    // ----------------------------------------------------------------
    // Teams
    // ----------------------------------------------------------------
    public function teams(Request $request)
    {
        $filters = $request->only(['game', 'region', 'looking_for_players']);
        return view('pages.teams.index', [
            'teams'   => MockDataService::teams($filters),
            'games'   => MockDataService::games(),
            'filters' => $filters,
        ]);
    }

    public function teamProfile(string $slug)
    {
        $team = MockDataService::team($slug) ?? MockDataService::teams()[0];
        return view('pages.teams.show', [
            'team'  => $team,
            'feed'  => array_slice(MockDataService::feed(), 0, 3),
        ]);
    }

    // ----------------------------------------------------------------
    // Games
    // ----------------------------------------------------------------
    public function games()
    {
        return view('pages.games.index', [
            'games' => MockDataService::games(),
        ]);
    }

    public function gameDetail(string $slug)
    {
        $game = MockDataService::game($slug) ?? MockDataService::games()[0];
        return view('pages.games.show', [
            'game'        => $game,
            'players'     => MockDataService::players(['game' => $slug]),
            'teams'       => MockDataService::teams(['game' => $slug]),
            'tournaments' => MockDataService::tournaments(['game' => $slug]),
        ]);
    }

    // ----------------------------------------------------------------
    // Tournaments
    // ----------------------------------------------------------------
    public function tournaments(Request $request)
    {
        $filters = $request->only(['game', 'status', 'region']);
        return view('pages.tournaments.index', [
            'tournaments' => MockDataService::tournaments($filters),
            'featured'    => MockDataService::featuredTournaments(),
            'games'       => MockDataService::games(),
            'filters'     => $filters,
        ]);
    }

    public function tournamentDetail(string $slug)
    {
        $tournament = MockDataService::tournament($slug) ?? MockDataService::tournaments()[0];
        return view('pages.tournaments.show', [
            'tournament' => $tournament,
            'teams'      => array_slice(MockDataService::teams(), 0, 8),
        ]);
    }

    // ----------------------------------------------------------------
    // Rankings
    // ----------------------------------------------------------------
    public function rankings(Request $request)
    {
        return view('pages.rankings.index', [
            'players'  => MockDataService::rankings(),
            'teams'    => MockDataService::teams(),
            'games'    => MockDataService::games(),
            'activeGame' => $request->get('game', 'valorstrike'),
        ]);
    }

    // ----------------------------------------------------------------
    // Recruitment
    // ----------------------------------------------------------------
    public function recruitment()
    {
        return view('pages.recruitment.index', [
            'posts' => MockDataService::recruitment(),
            'games' => MockDataService::games(),
        ]);
    }

    public function recruitmentDetail(int $id)
    {
        $posts = MockDataService::recruitment();
        $post  = collect($posts)->firstWhere('id', $id) ?? $posts[0];
        return view('pages.recruitment.show', ['post' => $post]);
    }

    // ----------------------------------------------------------------
    // Transfer Market
    // ----------------------------------------------------------------
    public function transferMarket()
    {
        return view('pages.transfer-market.index', [
            'market' => MockDataService::transferMarket(),
            'games'  => MockDataService::games(),
        ]);
    }

    // ----------------------------------------------------------------
    // Scrims
    // ----------------------------------------------------------------
    public function scrims()
    {
        return view('pages.scrims.index', [
            'scrims' => MockDataService::scrims(),
            'games'  => MockDataService::games(),
        ]);
    }

    public function scrimDetail(int $id)
    {
        $scrims = MockDataService::scrims();
        $scrim  = collect($scrims)->firstWhere('id', $id) ?? $scrims[0];
        return view('pages.scrims.show', ['scrim' => $scrim]);
    }

    // ----------------------------------------------------------------
    // Matches
    // ----------------------------------------------------------------
    public function matches()
    {
        $teams = MockDataService::teams();
        $games = MockDataService::games();

        // Build mock recent matches list
        $matches = [];
        $statuses = ['completed', 'completed', 'live', 'upcoming', 'completed'];
        for ($i = 1; $i <= 8; $i++) {
            $home = $teams[($i - 1) % count($teams)];
            $away = $teams[$i % count($teams)];
            $game = $games[($i - 1) % count($games)];
            $status = $statuses[$i % count($statuses)];
            $matches[] = [
                'id'        => $i,
                'home'      => $home,
                'away'      => $away,
                'score'     => $status === 'upcoming' ? null : ['home' => rand(0, 2), 'away' => rand(0, 2)],
                'status'    => $status,
                'game'      => $game['name'],
                'format'    => ['BO1','BO3','BO5'][($i - 1) % 3],
                'played_at' => now()->subHours($i * 3)->toIso8601String(),
            ];
        }

        return view('pages.matches.index', [
            'matches' => $matches,
            'games'   => $games,
        ]);
    }

    public function matchDetail(int $id)
    {
        return view('pages.matches.show', [
            'match' => [
                'id' => $id,
                'home' => MockDataService::teams()[0],
                'away' => MockDataService::teams()[1],
                'score' => ['home' => 2, 'away' => 0],
                'status' => 'completed',
                'game' => 'ValoStrike',
                'format' => 'BO3',
                'played_at' => '2026-07-13T20:00:00Z',
            ],
        ]);
    }

    // ----------------------------------------------------------------
    // Community
    // ----------------------------------------------------------------
    public function community()
    {
        return view('pages.community.index', [
            'feed'  => MockDataService::feed(),
            'games' => MockDataService::games(),
        ]);
    }

    // ----------------------------------------------------------------
    // Chat
    // ----------------------------------------------------------------
    public function chat()
    {
        return view('pages.chat.index', [
            'conversations' => [
                ['id' => 1, 'name' => 'NightFalcon', 'avatar' => '/assets/images/generated/players/player-1.webp', 'last' => 'GG, good game tonight!', 'time' => '2m', 'unread' => 2, 'online' => true, 'type' => 'direct'],
                ['id' => 2, 'name' => 'Team Nexus', 'avatar' => '/assets/images/generated/teams/team-1.webp', 'last' => 'Scrim at 20:00 tonight', 'time' => '15m', 'unread' => 0, 'online' => true, 'type' => 'team'],
                ['id' => 3, 'name' => 'StellarX', 'avatar' => '/assets/images/generated/players/player-2.webp', 'last' => 'Check your DMs', 'time' => '1h', 'unread' => 0, 'online' => false, 'type' => 'direct'],
                ['id' => 4, 'name' => 'ALGA Arena Cup Chat', 'avatar' => null, 'last' => 'Registration closes tomorrow', 'time' => '2h', 'unread' => 5, 'online' => true, 'type' => 'tournament'],
            ],
        ]);
    }

    // ----------------------------------------------------------------
    // Profile
    // ----------------------------------------------------------------
    public function profile(string $slug)
    {
        $player = MockDataService::player($slug) ?? MockDataService::players()[0];
        return view('pages.profile.show', [
            'player' => $player,
            'feed'   => array_slice(MockDataService::feed(), 0, 3),
        ]);
    }

    public function profileEdit(string $slug)
    {
        $player = MockDataService::player($slug) ?? MockDataService::players()[0];
        return view('pages.profile.edit', ['player' => $player]);
    }

    // ----------------------------------------------------------------
    // Verification
    // ----------------------------------------------------------------
    public function verification()
    {
        return view('pages.verification.index', [
            'player' => MockDataService::player('nightfalcon'),
        ]);
    }

    // ----------------------------------------------------------------
    // Subscriptions
    // ----------------------------------------------------------------
    public function subscriptions()
    {
        return view('pages.subscriptions.index', [
            'plans' => MockDataService::subscriptions(),
        ]);
    }

    // ----------------------------------------------------------------
    // Settings
    // ----------------------------------------------------------------
    public function settings(Request $request, string $tab = 'account')
    {
        return view('pages.settings.index', [
            'tab'    => $tab,
            'player' => MockDataService::player('nightfalcon'),
        ]);
    }
}
