//
//  PrestigeView.swift
//  lichunWebsocket
//
//  Wave 2: surfaces the player's prestige (this life) and family prestige
//  (cross-generational), explains what prestige unlocks (senior/top roles at
//  100/300), and lists prestige-gated shop items as locked with their threshold.
//
//  Prestige gates (mirrors server):
//   - Jobs: senior roles at 100 prestige, top roles at 300 prestige.
//   - Shop: exclusive lifestyle items gated at 100 / 250 / 500 (server-defined
//     via each item's minPrestige; we display whatever the catalog reports).
//

import SwiftUI

struct PrestigeView: View {
    @EnvironmentObject var webSocketService: WebSocketService

    private var prestige: Int { webSocketService.person.prestige }
    private var familyPrestige: Int { webSocketService.player.familyPrestige }

    // Job prestige gates (mirrors PRESTIGE_GATE_SENIOR / PRESTIGE_GATE_TOP).
    private let seniorGate = 100
    private let topGate = 300

    private var gatedItems: [StoreItem] {
        webSocketService.player.storeItems
            .filter { $0.minPrestige > 0 }
            .sorted { $0.minPrestige < $1.minPrestige }
    }

    var body: some View {
        ScrollView {
            VStack(spacing: AppSpacing.lg) {
                prestigeHeader

                VStack(alignment: .leading, spacing: AppSpacing.sm) {
                    SectionHeader(title: "What Prestige Unlocks")
                    UnlockRow(
                        icon: "arrow.up.circle.fill",
                        title: "Senior Roles",
                        detail: "Unlock senior career levels at \(seniorGate) prestige.",
                        met: prestige >= seniorGate,
                        color: AppColors.info
                    )
                    UnlockRow(
                        icon: "crown.fill",
                        title: "Top Roles",
                        detail: "Unlock the top career tier at \(topGate) prestige.",
                        met: prestige >= topGate,
                        color: AppColors.prestige
                    )
                }
                .padding(.horizontal, AppSpacing.md)

                if !gatedItems.isEmpty {
                    VStack(alignment: .leading, spacing: AppSpacing.sm) {
                        SectionHeader(title: "Exclusive Shop Items")
                        ForEach(gatedItems, id: \.self) { item in
                            GatedItemRow(item: item, prestige: prestige)
                        }
                    }
                    .padding(.horizontal, AppSpacing.md)
                }

                Spacer(minLength: AppSpacing.xl)
            }
            .padding(.bottom, AppSpacing.xl)
        }
        .background(AppColors.background)
        .navigationTitle("Prestige")
        .navigationBarTitleDisplayMode(.inline)
        .onAppear {
            AnalyticsManager.shared.trackScreenView("prestige", screenClass: "PrestigeView")
        }
    }

    private var prestigeHeader: some View {
        HStack(spacing: AppSpacing.md) {
            prestigeStat(
                icon: "star.circle.fill",
                value: prestige,
                label: "Prestige",
                sublabel: "This life",
                color: AppColors.prestige
            )
            prestigeStat(
                icon: "house.fill",
                value: familyPrestige,
                label: "Family Prestige",
                sublabel: "Carries across lives",
                color: AppColors.family
            )
        }
        .padding(.horizontal, AppSpacing.md)
        .padding(.top, AppSpacing.md)
    }

    private func prestigeStat(icon: String, value: Int, label: String, sublabel: String, color: Color) -> some View {
        BaseCard(backgroundColor: AppColors.surfaceElevated, showShadow: true) {
            VStack(spacing: AppSpacing.xs) {
                Image(systemName: icon)
                    .font(.system(size: 28))
                    .foregroundColor(color)
                Text("\(value)")
                    .font(.system(size: 28, weight: .bold, design: .rounded))
                    .foregroundColor(AppColors.primaryText)
                Text(label)
                    .font(.appBodyBold)
                    .foregroundColor(AppColors.primaryText)
                    .multilineTextAlignment(.center)
                Text(sublabel)
                    .font(.appCaption)
                    .foregroundColor(AppColors.secondaryText)
                    .multilineTextAlignment(.center)
            }
            .frame(maxWidth: .infinity)
            .padding(.vertical, AppSpacing.xs)
        }
    }
}

// MARK: - Unlock Row

private struct UnlockRow: View {
    let icon: String
    let title: String
    let detail: String
    let met: Bool
    let color: Color

    var body: some View {
        BaseCard(backgroundColor: AppColors.surfaceElevated, showShadow: true) {
            HStack(spacing: AppSpacing.md) {
                ZStack {
                    Circle()
                        .fill(color.opacity(0.15))
                        .frame(width: 44, height: 44)
                    Image(systemName: icon)
                        .font(.system(size: 20))
                        .foregroundColor(color)
                }
                VStack(alignment: .leading, spacing: 2) {
                    Text(title)
                        .font(.appBodyBold)
                        .foregroundColor(AppColors.primaryText)
                    Text(detail)
                        .font(.appCaption)
                        .foregroundColor(AppColors.secondaryText)
                        .lineLimit(2)
                }
                Spacer()
                Image(systemName: met ? "checkmark.seal.fill" : "lock.fill")
                    .font(.system(size: 18))
                    .foregroundColor(met ? AppColors.success : AppColors.secondaryText.opacity(0.6))
            }
        }
    }
}

// MARK: - Gated Shop Item Row

private struct GatedItemRow: View {
    let item: StoreItem
    let prestige: Int

    private var unlocked: Bool { prestige >= item.minPrestige }

    var body: some View {
        BaseCard(backgroundColor: AppColors.surfaceElevated, showShadow: true) {
            HStack(spacing: AppSpacing.md) {
                ZStack {
                    Circle()
                        .fill((unlocked ? AppColors.success : AppColors.disabledText).opacity(0.15))
                        .frame(width: 44, height: 44)
                    Image(systemName: unlocked ? "bag.fill" : "lock.fill")
                        .font(.system(size: 18))
                        .foregroundColor(unlocked ? AppColors.success : AppColors.disabledText.opacity(0.7))
                }
                VStack(alignment: .leading, spacing: 2) {
                    Text(item.name)
                        .font(.appBodyBold)
                        .foregroundColor(AppColors.primaryText)
                    Text(unlocked
                         ? "Unlocked - available in the shop"
                         : "Requires \(item.minPrestige) prestige (you have \(prestige))")
                        .font(.appCaption)
                        .foregroundColor(unlocked ? AppColors.secondaryText : AppColors.error)
                        .lineLimit(2)
                }
                Spacer()
                HStack(spacing: 4) {
                    Image(systemName: "star.fill")
                        .font(.system(size: 11))
                    Text("\(item.minPrestige)")
                        .font(.appCaptionBold)
                }
                .foregroundColor(unlocked ? AppColors.prestige : AppColors.secondaryText)
            }
        }
        .opacity(unlocked ? 1.0 : 0.85)
    }
}

// MARK: - Preview
// Preview removed - requires backend connection
