//
//  HomeContent_NeoBrutalist.swift
//  SportsTime
//
//  NEO-BRUTALIST: Bold blocks, harsh shadows, high contrast.
//  Offset elements, thick borders, punchy colors.
//  Ticket stub aesthetic with hard edges.
//

import SwiftUI
import SwiftData

struct HomeContent_NeoBrutalist: 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]

    // Neo-brutalist palette
    private let primaryYellow = Color(red: 1.0, green: 0.9, blue: 0.0)
    private let hotPink = Color(red: 1.0, green: 0.2, blue: 0.6)
    private let electricBlue = Color(red: 0.2, green: 0.4, blue: 1.0)

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

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

    var body: some View {
        ScrollView {
            VStack(spacing: 24) {
                // HERO BLOCK with offset shadow
                heroBlock
                    .padding(.top, 16)
                    .padding(.horizontal, 16)

                // ACTION BUTTONS - Bold stacked blocks
                actionBlocks
                    .padding(.horizontal, 16)

                // FEATURED TRIPS
                if !suggestedTripsGenerator.suggestedTrips.isEmpty {
                    featuredSection
                        .padding(.horizontal, 16)
                }

                // SAVED TRIPS
                if !savedTrips.isEmpty {
                    savedSection
                        .padding(.horizontal, 16)
                }

                Spacer(minLength: 32)
            }
        }
        .background(bgColor)
    }

    // MARK: - Hero Block

    private var heroBlock: some View {
        ZStack {
            // Shadow block (offset)
            RoundedRectangle(cornerRadius: 0)
                .fill(textColor)
                .offset(x: 8, y: 8)

            // Main block
            VStack(alignment: .leading, spacing: 16) {
                // Badge
                Text("SPORTS TRIP PLANNER")
                    .font(.system(size: 10, weight: .black))
                    .tracking(2)
                    .foregroundStyle(bgColor)
                    .padding(.horizontal, 12)
                    .padding(.vertical, 6)
                    .background(primaryYellow)

                // Main title
                VStack(alignment: .leading, spacing: 4) {
                    Text("PLAN YOUR")
                        .font(.system(size: 32, weight: .black))
                        .foregroundStyle(textColor)

                    HStack(spacing: 0) {
                        Text("ROAD")
                            .font(.system(size: 32, weight: .black))
                            .foregroundStyle(hotPink)
                        Text(" TRIP")
                            .font(.system(size: 32, weight: .black))
                            .foregroundStyle(textColor)
                    }
                }

                // Description
                Text("Hit every stadium. Catch every game. Make memories that last.")
                    .font(.system(size: 14, weight: .medium))
                    .foregroundStyle(textColor.opacity(0.7))
            }
            .padding(24)
            .frame(maxWidth: .infinity, alignment: .leading)
            .background(bgColor)
            .border(textColor, width: 3)
        }
    }

    // MARK: - Action Blocks

    private var actionBlocks: some View {
        VStack(spacing: 12) {
            // Primary CTA
            Button {
                showNewTrip = true
            } label: {
                ZStack {
                    // Shadow
                    Rectangle()
                        .fill(textColor)
                        .offset(x: 6, y: 6)

                    HStack {
                        Text("START PLANNING")
                            .font(.system(size: 16, weight: .black))
                            .tracking(1)

                        Spacer()

                        Text("→")
                            .font(.system(size: 24, weight: .black))
                    }
                    .foregroundStyle(.black)
                    .padding(20)
                    .background(primaryYellow)
                    .border(.black, width: 3)
                }
            }
            .frame(height: 70)

            // Secondary actions
            HStack(spacing: 12) {
                Button {
                    selectedTab = 1
                } label: {
                    secondaryBlock(text: "SCHEDULE", color: electricBlue)
                }

                Button {
                    selectedTab = 2
                } label: {
                    secondaryBlock(text: "MY TRIPS", color: hotPink)
                }
            }
        }
    }

    private func secondaryBlock(text: String, color: Color) -> some View {
        ZStack {
            Rectangle()
                .fill(textColor)
                .offset(x: 4, y: 4)

            Text(text)
                .font(.system(size: 12, weight: .black))
                .tracking(1)
                .foregroundStyle(.white)
                .padding(16)
                .frame(maxWidth: .infinity)
                .background(color)
                .border(.black, width: 2)
        }
        .frame(height: 56)
    }

    // MARK: - Featured Section

    private var featuredSection: some View {
        VStack(alignment: .leading, spacing: 16) {
            // Section header
            HStack {
                Text("FEATURED")
                    .font(.system(size: 12, weight: .black))
                    .tracking(2)
                    .foregroundStyle(textColor)

                Rectangle()
                    .fill(primaryYellow)
                    .frame(height: 4)

                Button {
                    Task {
                        await suggestedTripsGenerator.refreshTrips()
                    }
                } label: {
                    Image(systemName: "arrow.clockwise")
                        .font(.system(size: 14, weight: .bold))
                        .foregroundStyle(textColor)
                }
            }

            // Trip cards - stacked blocks
            ForEach(suggestedTripsGenerator.suggestedTrips.prefix(4)) { suggestedTrip in
                Button {
                    selectedSuggestedTrip = suggestedTrip
                } label: {
                    neoBrutalistTripCard(suggestedTrip.trip)
                }
                .buttonStyle(.plain)
            }
        }
    }

    private func neoBrutalistTripCard(_ trip: Trip) -> some View {
        ZStack {
            // Shadow
            Rectangle()
                .fill(textColor)
                .offset(x: 5, y: 5)

            HStack(spacing: 16) {
                // Color block indicator
                Rectangle()
                    .fill(trip.uniqueSports.first?.themeColor ?? primaryYellow)
                    .frame(width: 8)

                VStack(alignment: .leading, spacing: 6) {
                    Text(trip.displayName.uppercased())
                        .font(.system(size: 14, weight: .black))
                        .foregroundStyle(textColor)
                        .lineLimit(1)

                    HStack(spacing: 12) {
                        Text("\(trip.stops.count) CITIES")
                            .font(.system(size: 10, weight: .bold))
                            .padding(.horizontal, 8)
                            .padding(.vertical, 4)
                            .background(primaryYellow)
                            .foregroundStyle(.black)

                        Text("\(trip.totalGames) GAMES")
                            .font(.system(size: 10, weight: .bold))
                            .padding(.horizontal, 8)
                            .padding(.vertical, 4)
                            .background(hotPink)
                            .foregroundStyle(.white)
                    }
                }

                Spacer()

                Text("→")
                    .font(.system(size: 18, weight: .black))
                    .foregroundStyle(textColor)
            }
            .padding(16)
            .background(bgColor)
            .border(textColor, width: 2)
        }
    }

    // MARK: - Saved Section

    private var savedSection: some View {
        VStack(alignment: .leading, spacing: 16) {
            HStack {
                Text("YOUR TRIPS")
                    .font(.system(size: 12, weight: .black))
                    .tracking(2)
                    .foregroundStyle(textColor)

                Rectangle()
                    .fill(hotPink)
                    .frame(height: 4)

                Button {
                    selectedTab = 2
                } label: {
                    Text("ALL →")
                        .font(.system(size: 12, weight: .black))
                        .foregroundStyle(hotPink)
                }
            }

            ForEach(savedTrips.prefix(3)) { savedTrip in
                if let trip = savedTrip.trip {
                    NavigationLink {
                        TripDetailView(trip: trip)
                    } label: {
                        HStack {
                            Rectangle()
                                .fill(electricBlue)
                                .frame(width: 4)

                            Text(trip.displayName.uppercased())
                                .font(.system(size: 13, weight: .bold))
                                .foregroundStyle(textColor)

                            Spacer()

                            Text("\(trip.stops.count)")
                                .font(.system(size: 12, weight: .black))
                                .foregroundStyle(.black)
                                .padding(.horizontal, 10)
                                .padding(.vertical, 6)
                                .background(primaryYellow)
                        }
                        .padding(12)
                        .border(textColor.opacity(0.3), width: 2)
                    }
                    .buttonStyle(.plain)
                }
            }
        }
    }
}
