//
//  ProfileHeader.swift
//  lichunWebsocket
//
//  Quick glance header for NPC profile with avatar, name, affinity, and info pills
//

import SwiftUI

struct ProfileHeader: View {
    @ObservedObject var person: Person

    var body: some View {
        VStack(spacing: AppSpacing.lg) {
            // Avatar with mood overlay
            avatarSection

            // Name, relationship, age
            nameSection

            // Affinity bar
            affinitySection

            // Quick info pills
            infoPillsSection
        }
        .padding(.vertical, AppSpacing.lg)
    }

    // MARK: - Avatar Section

    private var avatarSection: some View {
        ZStack(alignment: .bottomTrailing) {
            CharacterAvatar(
                person: person,
                size: 120,
                showBorder: true,
                borderGradient: [AppColors.primary, AppColors.accent],
                showGlow: true
            )

            // Mood emoji overlay
            if !person.mood.isEmpty {
                Text(moodEmoji)
                    .font(.system(size: 28))
                    .padding(6)
                    .background(
                        Circle()
                            .fill(AppColors.cardBackground)
                            .shadow(color: Color.black.opacity(0.15), radius: 4, x: 0, y: 2)
                    )
                    .offset(x: 8, y: 8)
            }
        }
    }

    private var moodEmoji: String {
        switch person.mood.lowercased() {
        case "happy": return "😊"
        case "calm": return "😌"
        case "stressed": return "😰"
        case "exhausted": return "😩"
        case "fulfilled": return "🥰"
        case "depressed": return "😢"
        case "angry": return "😠"
        case "excited": return "🤩"
        case "anxious": return "😬"
        case "content": return "😊"
        default: return "😐"
        }
    }

    // MARK: - Name Section

    private var nameSection: some View {
        VStack(spacing: AppSpacing.xs) {
            // Full name
            Text("\(person.firstname) \(person.lastname)")
                .font(.system(size: 28, weight: .bold, design: .rounded))
                .foregroundStyle(
                    LinearGradient(
                        colors: [AppColors.primaryText, AppColors.primaryText.opacity(0.8)],
                        startPoint: .top,
                        endPoint: .bottom
                    )
                )

            // Relationship type and age
            HStack(spacing: AppSpacing.sm) {
                if !relationshipType.isEmpty {
                    Text(relationshipType)
                        .font(.appBody)
                        .foregroundColor(AppColors.primary)
                        .fontWeight(.medium)
                }

                Text("•")
                    .foregroundColor(AppColors.secondaryText.opacity(0.5))

                Text("\(person.ageYears) years old")
                    .font(.appBody)
                    .foregroundColor(AppColors.secondaryText)
            }
        }
    }

    private var relationshipType: String {
        // Determine primary relationship type from relationships array
        let relationships = person.relationships
        if relationships.contains("partner") || relationships.contains("spouse") {
            return "Partner"
        } else if relationships.contains("parent") {
            return "Parent"
        } else if relationships.contains("child") {
            return "Child"
        } else if relationships.contains("sibling") {
            return "Sibling"
        } else if relationships.contains("friend") {
            return "Friend"
        } else if relationships.contains("classmate") {
            return "Classmate"
        } else if relationships.contains("coworker") {
            return "Coworker"
        } else if relationships.contains("crush") {
            return "Crush"
        } else if let first = relationships.first {
            return first.capitalized
        }
        return "Acquaintance"
    }

    // MARK: - Affinity Section

    private var affinitySection: some View {
        VStack(alignment: .leading, spacing: AppSpacing.xs) {
            HStack {
                Image(systemName: "heart.fill")
                    .font(.system(size: 14))
                    .foregroundColor(AppColors.primary)

                Text("Affinity")
                    .font(.appCaptionBold)
                    .foregroundColor(AppColors.secondaryText)

                Spacer()

                Text("\(person.affinity)%")
                    .font(.system(size: 14, weight: .bold, design: .rounded))
                    .foregroundColor(affinityColor)
            }

            CozyStatBar(
                label: nil,
                value: person.affinity,
                color: affinityColor,
                showPercentage: false,
                height: 10
            )
        }
        .padding(.horizontal, AppSpacing.xl)
    }

    private var affinityColor: Color {
        if person.affinity >= 80 {
            return Color.green
        } else if person.affinity >= 50 {
            return AppColors.primary
        } else if person.affinity >= 25 {
            return Color.orange
        } else {
            return Color.red
        }
    }

    // MARK: - Info Pills Section

    private var infoPillsSection: some View {
        HStack(spacing: AppSpacing.sm) {
            InfoPill(
                icon: "person.fill",
                text: lifeStage
            )

            InfoPill(
                icon: "location.fill",
                text: person.location.isEmpty ? "Unknown" : person.location.capitalized
            )

            InfoPill(
                icon: "clock.fill",
                text: person.intraDayMessage.isEmpty ? "Idle" : person.intraDayMessage
            )
        }
        .padding(.horizontal, AppSpacing.md)
    }

    private var lifeStage: String {
        switch person.ageYears {
        case 0..<1: return "Baby"
        case 1..<4: return "Toddler"
        case 4..<13: return "Child"
        case 13..<18: return "Teen"
        case 18..<30: return "Young Adult"
        case 30..<60: return "Adult"
        case 60..<80: return "Senior"
        default: return "Elder"
        }
    }
}

// MARK: - Info Pill Component

private struct InfoPill: View {
    let icon: String
    let text: String

    var body: some View {
        HStack(spacing: AppSpacing.xs) {
            Image(systemName: icon)
                .font(.system(size: 12, weight: .semibold))
                .foregroundColor(AppColors.primary)

            Text(text)
                .font(.appCaption)
                .foregroundColor(AppColors.primaryText)
                .lineLimit(1)
        }
        .padding(.horizontal, AppSpacing.sm)
        .padding(.vertical, AppSpacing.xs)
        .background(
            Capsule()
                .fill(AppColors.surfaceElevated)
        )
        .overlay(
            Capsule()
                .strokeBorder(AppColors.primary.opacity(0.2), lineWidth: 1)
        )
    }
}

// MARK: - Preview

#if DEBUG
struct ProfileHeader_Previews: PreviewProvider {
    static var previews: some View {
        let samplePerson = Person()
        samplePerson.id = "123"
        samplePerson.firstname = "Emma"
        samplePerson.lastname = "Johnson"
        samplePerson.ageYears = 28
        samplePerson.sex = "Female"
        samplePerson.mood = "Happy"
        samplePerson.affinity = 75
        samplePerson.location = "Coffee Shop"
        samplePerson.intraDayMessage = "Having coffee"
        samplePerson.relationships = ["friend", "classmate"]
        samplePerson.image = "https://api.dicebear.com/7.x/avataaars/svg?seed=Emma"

        return ScrollView {
            ProfileHeader(person: samplePerson)
                .padding()
        }
        .background(AppColors.background)
    }
}
#endif
