import { createFileRoute } from '@tanstack/react-router'
import { z } from 'zod'
import { AsciiPanel } from '#/components/ascii/AsciiPanel'
import { QuadrantFilterBar } from '#/components/panels/QuadrantFilterBar'
import type { MatchData } from '#/components/panels/MatchCard'
import { MatchCard } from '#/components/panels/MatchCard'
import { LiveTicker } from '#/components/panels/LiveTicker'

// Define schema for deterministic URL state filtering (AGENTS.md Rule A compliance)
const dashboardSearchSchema = z.object({
  quadrant: z.enum(['alpha', 'beta', 'gamma', 'delta', 'all']).default('all'),
  currency: z.enum(['Latinum', 'Credits']).default('Latinum'),
})

export const Route = createFileRoute('/')({
  validateSearch: dashboardSearchSchema,
  component: DashboardView,
})

function DashboardView() {
  const { quadrant } = Route.useSearch()

  // Full active matches pool matching taxonomy.yaml configuration
  const matches: MatchData[] = [
    {
      id: 'sector-001',
      name: 'Sector 001 — Sol Array',
      quadrant: 'alpha',
      status: 'LIVE',
      tick: 42,
      maxTicks: 90,
      kineticYield: 14.7,
      homeFaction: {
        abbreviation: 'SFC',
        name: 'Starfleet Command',
        glyph: '◆',
        colorCode: '#34d399',
        shieldCapacity: 62,
        oddsPrice: 2.85,
      },
      awayFaction: {
        abbreviation: 'BRG',
        name: 'Borg Collective',
        glyph: '█',
        colorCode: '#38bdf8',
        shieldCapacity: 71,
        oddsPrice: 1.48,
      },
      marketsOpenCount: 6,
    },
    {
      id: 'bajor-wormhole',
      name: 'Bajor Sector — DS9',
      quadrant: 'alpha',
      status: 'LIVE',
      tick: 31,
      maxTicks: 75,
      kineticYield: 8.2,
      homeFaction: {
        abbreviation: 'BAJ',
        name: 'Bajoran Militia',
        glyph: '☼',
        colorCode: '#fb923c',
        shieldCapacity: 48,
        oddsPrice: 2.1,
      },
      awayFaction: {
        abbreviation: 'DOM',
        name: 'The Dominion',
        glyph: '◉',
        colorCode: '#fb7185',
        shieldCapacity: 55,
        oddsPrice: 1.72,
      },
      marketsOpenCount: 8,
    },
    {
      id: 'wolf-359',
      name: 'Wolf 359 Outpost',
      quadrant: 'alpha',
      status: 'PRE',
      tick: 0,
      maxTicks: 90,
      kineticYield: 0.0,
      homeFaction: {
        abbreviation: 'SFC',
        name: 'Starfleet Command',
        glyph: '◆',
        colorCode: '#34d399',
        shieldCapacity: 100,
        oddsPrice: 2.4,
      },
      awayFaction: {
        abbreviation: 'BRG',
        name: 'Borg Collective',
        glyph: '█',
        colorCode: '#38bdf8',
        shieldCapacity: 100,
        oddsPrice: 1.62,
      },
      marketsOpenCount: 4,
      commencingIn: '00:47',
    },
    {
      id: 'qonos-arena',
      name: "Qo'noS Sector — Warrior's Ring",
      quadrant: 'beta',
      status: 'LIVE',
      tick: 58,
      maxTicks: 120,
      kineticYield: 12.4,
      homeFaction: {
        abbreviation: 'KLG',
        name: 'Klingon Empire',
        glyph: '⚔',
        colorCode: '#f87171',
        shieldCapacity: 88,
        oddsPrice: 1.75,
      },
      awayFaction: {
        abbreviation: 'ROM',
        name: 'Romulan Star Empire',
        glyph: '◈',
        colorCode: '#a78bfa',
        shieldCapacity: 72,
        oddsPrice: 2.15,
      },
      marketsOpenCount: 6,
    },
    {
      id: 'neutral-zone',
      name: 'The Neutral Zone',
      quadrant: 'beta',
      status: 'LIVE',
      tick: 15,
      maxTicks: 80,
      kineticYield: 4.2,
      homeFaction: {
        abbreviation: 'ROM',
        name: 'Romulan Star Empire',
        glyph: '◈',
        colorCode: '#a78bfa',
        shieldCapacity: 90,
        oddsPrice: 1.95,
      },
      awayFaction: {
        abbreviation: 'SFC',
        name: 'Starfleet Command',
        glyph: '◆',
        colorCode: '#34d399',
        shieldCapacity: 95,
        oddsPrice: 1.85,
      },
      marketsOpenCount: 8,
    },
    {
      id: 'karemma-trade-corridor',
      name: 'Karemma Trade Corridor',
      quadrant: 'gamma',
      status: 'LIVE',
      tick: 73,
      maxTicks: 100,
      kineticYield: 18.1,
      homeFaction: {
        abbreviation: 'KRM',
        name: 'Karemma Trade Alliance',
        glyph: '◊',
        colorCode: '#fcd34d',
        shieldCapacity: 65,
        oddsPrice: 2.3,
      },
      awayFaction: {
        abbreviation: 'DOS',
        name: 'Dominion Outpost 6',
        glyph: '▬',
        colorCode: '#c084fc',
        shieldCapacity: 80,
        oddsPrice: 1.6,
      },
      marketsOpenCount: 6,
    },
    {
      id: 'grid-325',
      name: 'Grid 325 — Borg Corridor',
      quadrant: 'delta',
      status: 'LIVE',
      tick: 82,
      maxTicks: 90,
      kineticYield: 24.5,
      homeFaction: {
        abbreviation: 'BRG',
        name: 'Borg Collective',
        glyph: '█',
        colorCode: '#38bdf8',
        shieldCapacity: 15,
        oddsPrice: 9.5,
      },
      awayFaction: {
        abbreviation: 'S84',
        name: 'Species 8472',
        glyph: '∞',
        colorCode: '#ec4899',
        shieldCapacity: 99,
        oddsPrice: 1.08,
      },
      marketsOpenCount: 8,
    },
    {
      id: 'hirogen-relay',
      name: 'Hirogen Relay Nexus',
      quadrant: 'delta',
      status: 'LIVE',
      tick: 49,
      maxTicks: 100,
      kineticYield: 9.3,
      homeFaction: {
        abbreviation: 'HRG',
        name: 'Hirogen Hunt Pack',
        glyph: '⊗',
        colorCode: '#fbbf24',
        shieldCapacity: 75,
        oddsPrice: 1.55,
      },
      awayFaction: {
        abbreviation: 'KZN',
        name: 'Kazon Sects',
        glyph: '╳',
        colorCode: '#f97316',
        shieldCapacity: 40,
        oddsPrice: 2.45,
      },
      marketsOpenCount: 6,
    },
  ]

  // Filter matches based on the URL query param state
  const filteredMatches =
    quadrant === 'all'
      ? matches
      : matches.filter((m) => m.quadrant === quadrant)

  return (
    <AsciiPanel
      title="Console Terminal"
      subtitle="Active Feeds"
      variant="emerald"
    >
      <div className="space-y-4 font-mono select-none h-full flex flex-col justify-between">
        {/* Broadcast Banner */}
        <div>
          <div className="text-emerald-400 font-bold text-xs uppercase tracking-wider mb-2">
            &gt; SUBSPACE BROADCAST — LIVE ENGAGEMENTS ACROSS ALL QUADRANTS
          </div>
          <QuadrantFilterBar currentQuadrant={quadrant} />
        </div>

        {/* Matches Grid Area */}
        <div className="flex-1 overflow-y-auto min-h-0 my-3 pr-1">
          {filteredMatches.length === 0 ? (
            <div className="p-8 border border-dashed border-slate-900 text-center italic text-slate-600">
              No active engagements detected in this quadrant division array.
            </div>
          ) : (
            <div className="grid grid-cols-1 md:grid-cols-2 gap-3">
              {filteredMatches.map((match) => (
                <MatchCard key={match.id} match={match} />
              ))}
            </div>
          )}
        </div>

        {/* Live Feed Event Ticker */}
        <div>
          <div className="text-slate-600 text-[10px] font-bold tracking-wider mb-1.5 px-0.5">
            &gt; LIVE GLOBAL TELEMETRY FEED TICKER
          </div>
          <LiveTicker />
        </div>
      </div>
    </AsciiPanel>
  )
}
