//
//  HomeContent_Brutalist.swift
//  SportsTime
//
//  BRUTALIST: Raw, unpolished, anti-design rebellion.
//  Monospace typography, harsh borders, no rounded corners.
//  Ticket stub perforations, stadium concrete vibes.
//

import SwiftUI
import SwiftData

struct HomeContent_Brutalist: View {
    @Environment(\.colorScheme) private var colorScheme
    @Binding var showNewTrip: Bool
    @Binding var selectedTab: Int
    @Binding var selectedSuggestedTrip: SuggestedTrip?

    let savedTrips: [SavedTrip]
    let suggestedTripsGenerator: SuggestedTripsGenerator
    let displayedTips: [PlanningTip]

    private var bgColor: Color {
        colorScheme == .dark ? .black : Color(white: 0.95)
    }

    private var textColor: Color {
        colorScheme == .dark ? .white : .black
    }

    var body: some View {
        ScrollView {
            VStack(alignment: .leading, spacing: 0) {
                // HEADER - Raw, bold, commanding
                brutalistHeader

                // PERFORATED DIVIDER
                perforatedDivider

                // ACTION BLOCK
                actionBlock
                    .padding(.vertical, 24)

                perforatedDivider

                // TRIPS SECTION
                if !suggestedTripsGenerator.suggestedTrips.isEmpty {
                    tripsSection
                        .padding(.vertical, 24)

                    perforatedDivider
                }

                // SAVED TRIPS
                if !savedTrips.isEmpty {
                    savedSection
                        .padding(.vertical, 24)
                }

                // FOOTER STAMP
                footerStamp
                    .padding(.vertical, 32)
            }
            .padding(.horizontal, 16)
        }
        .background(bgColor)
    }

    // MARK: - Brutalist Header

    private var brutalistHeader: some View {
        VStack(alignment: .leading, spacing: 8) {
            // Date stamp - like a ticket
            Text(Date.now.formatted(.dateTime.year().month().day()).uppercased())
                .font(.system(.caption, design: .monospaced))
                .foregroundStyle(textColor.opacity(0.5))
                .padding(.top, 24)

            // Main title - LOUD
            Text("SPORTS")
                .font(.system(size: 64, weight: .black, design: .default))
                .foregroundStyle(textColor)
                .tracking(-2)

            Text("TIME")
                .font(.system(size: 64, weight: .black, design: .default))
                .foregroundStyle(.red)
                .tracking(-2)
                .offset(y: -16)

            // Subtitle
            Text("PLAN YOUR ROAD TRIP")
                .font(.system(.subheadline, design: .monospaced))
                .foregroundStyle(textColor.opacity(0.7))
                .tracking(4)
        }
    }

    // MARK: - Perforated Divider (Ticket Stub Style)

    private var perforatedDivider: some View {
        HStack(spacing: 8) {
            ForEach(0..<20, id: \.self) { _ in
                Circle()
                    .fill(textColor.opacity(0.3))
                    .frame(width: 6, height: 6)
            }
        }
        .frame(maxWidth: .infinity)
    }

    // MARK: - Action Block

    private var actionBlock: some View {
        VStack(alignment: .leading, spacing: 16) {
            Text("[ NEW TRIP ]")
                .font(.system(.caption, design: .monospaced))
                .foregroundStyle(textColor.opacity(0.5))

            Button {
                showNewTrip = true
            } label: {
                HStack {
                    Text("START PLANNING →")
                        .font(.system(.title3, design: .monospaced).bold())

                    Spacer()
                }
                .foregroundStyle(colorScheme == .dark ? .black : .white)
                .padding(20)
                .background(.red)
            }

            Text("CREATE YOUR ULTIMATE SPORTS ROAD TRIP. NO FRILLS. JUST GAMES.")
                .font(.system(.caption, design: .monospaced))
                .foregroundStyle(textColor.opacity(0.6))
                .lineSpacing(4)
        }
    }

    // MARK: - Trips Section

    private var tripsSection: some View {
        VStack(alignment: .leading, spacing: 16) {
            HStack {
                Text("[ FEATURED ]")
                    .font(.system(.caption, design: .monospaced))
                    .foregroundStyle(textColor.opacity(0.5))

                Spacer()

                Text("\(suggestedTripsGenerator.suggestedTrips.count) TRIPS")
                    .font(.system(.caption, design: .monospaced))
                    .foregroundStyle(.red)
            }

            ForEach(suggestedTripsGenerator.suggestedTrips.prefix(4)) { suggestedTrip in
                Button {
                    selectedSuggestedTrip = suggestedTrip
                } label: {
                    brutalistTripCard(suggestedTrip.trip)
                }
                .buttonStyle(.plain)
            }
        }
    }

    private func brutalistTripCard(_ trip: Trip) -> some View {
        VStack(alignment: .leading, spacing: 8) {
            HStack(alignment: .top) {
                VStack(alignment: .leading, spacing: 4) {
                    Text(trip.displayName.uppercased())
                        .font(.system(.headline, design: .monospaced).bold())
                        .foregroundStyle(textColor)

                    Text("\(trip.stops.count) STOPS • \(trip.totalGames) GAMES")
                        .font(.system(.caption2, design: .monospaced))
                        .foregroundStyle(textColor.opacity(0.5))
                }

                Spacer()

                // Sport icons as harsh symbols
                HStack(spacing: 4) {
                    ForEach(Array(trip.uniqueSports.prefix(3)), id: \.self) { sport in
                        Text("●")
                            .font(.caption)
                            .foregroundStyle(sport.themeColor)
                    }
                }
            }

            // Route as raw text
            Text(trip.stops.map { $0.city.uppercased() }.joined(separator: " → "))
                .font(.system(.caption2, design: .monospaced))
                .foregroundStyle(textColor.opacity(0.4))
                .lineLimit(1)
        }
        .padding(16)
        .background(textColor.opacity(0.05))
        .border(textColor.opacity(0.2), width: 1)
    }

    // MARK: - Saved Section

    private var savedSection: some View {
        VStack(alignment: .leading, spacing: 16) {
            HStack {
                Text("[ YOUR TRIPS ]")
                    .font(.system(.caption, design: .monospaced))
                    .foregroundStyle(textColor.opacity(0.5))

                Spacer()

                Button {
                    selectedTab = 2
                } label: {
                    Text("VIEW ALL →")
                        .font(.system(.caption, design: .monospaced))
                        .foregroundStyle(.red)
                }
            }

            ForEach(savedTrips.prefix(3)) { savedTrip in
                if let trip = savedTrip.trip {
                    NavigationLink {
                        TripDetailView(trip: trip)
                    } label: {
                        HStack {
                            Text(trip.displayName.uppercased())
                                .font(.system(.subheadline, design: .monospaced))
                                .foregroundStyle(textColor)

                            Spacer()

                            Text("\(trip.stops.count)")
                                .font(.system(.caption, design: .monospaced))
                                .foregroundStyle(textColor.opacity(0.5))
                        }
                        .padding(.vertical, 12)
                        .overlay(alignment: .bottom) {
                            Rectangle()
                                .fill(textColor.opacity(0.1))
                                .frame(height: 1)
                        }
                    }
                    .buttonStyle(.plain)
                }
            }
        }
    }

    // MARK: - Footer Stamp

    private var footerStamp: some View {
        VStack(spacing: 8) {
            Rectangle()
                .fill(.red)
                .frame(width: 60, height: 4)

            Text("SPORTSTIME")
                .font(.system(.caption2, design: .monospaced))
                .foregroundStyle(textColor.opacity(0.3))
                .tracking(6)

            Text("EST. 2024")
                .font(.system(.caption2, design: .monospaced))
                .foregroundStyle(textColor.opacity(0.2))
        }
        .frame(maxWidth: .infinity)
    }
}
