//
//  CharacterCard.swift
//  lichunWebsocket
//
//  Reusable card component for displaying character information with cozy design
//

import SwiftUI

struct CharacterCard: View {
    let person: Person
    var showRelationship: Bool = true
    var showStats: Bool = false
    var onTap: (() -> Void)? = nil

    @State private var isHovered = false

    var body: some View {
        BaseCard(
            borderColor: onTap != nil && isHovered ? AppColors.primary.opacity(0.5) : nil,
            enableAnimation: true
        ) {
            HStack(spacing: AppSpacing.md) {
                // Avatar with gradient border
                CharacterAvatar(
                    person: person,
                    size: AppSpacing.avatarSizeMedium,
                    showBorder: true,
                    showGlow: true
                )

                // Character Info
                VStack(alignment: .leading, spacing: AppSpacing.xs) {
                    // Name with gradient on tap
                    Text(person.firstname + " " + person.lastname)
                        .font(.appHeadline)
                        .foregroundStyle(
                            isHovered && onTap != nil ?
                            LinearGradient(
                                colors: [AppColors.primary, AppColors.accent],
                                startPoint: .leading,
                                endPoint: .trailing
                            ) :
                            LinearGradient(
                                colors: [AppColors.primaryText, AppColors.primaryText],
                                startPoint: .leading,
                                endPoint: .trailing
                            )
                        )
                        .animation(.easeInOut(duration: 0.2), value: isHovered)

                    // Age with icon
                    HStack(spacing: 4) {
                        Image(systemName: "calendar")
                            .font(.system(size: 10))
                            .foregroundColor(AppColors.secondaryText)
                        Text("\(person.ageYears) years")
                            .font(.appCaption)
                            .foregroundColor(AppColors.secondaryText)
                    }

                    // Relationship Status
                    if showRelationship && !person.relationship.isEmpty {
                        HStack(spacing: 4) {
                            Image(systemName: "heart.fill")
                                .font(.system(size: 10))
                                .foregroundColor(AppColors.primary)
                            Text(person.relationship)
                                .font(.appCaption)
                                .foregroundColor(AppColors.primaryText)
                        }
                    }

                    // Stats (if enabled)
                    if showStats {
                        HStack(spacing: AppSpacing.sm) {
                            StatPill(icon: "❤️", value: Int(person.health), color: AppColors.health)
                            StatPill(icon: "😊", value: Int(person.happiness), color: AppColors.happiness)
                            StatPill(icon: "🧠", value: Int(person.intelligence), color: AppColors.intelligence)
                        }
                        .padding(.top, 2)
                    }
                }

                Spacer()

                // Chevron if tappable with animation
                if onTap != nil {
                    Image(systemName: "chevron.right")
                        .font(.system(size: 14, weight: .semibold))
                        .foregroundColor(isHovered ? AppColors.primary : AppColors.secondaryText)
                        .offset(x: isHovered ? 4 : 0)
                        .animation(.spring(response: 0.3, dampingFraction: 0.6), value: isHovered)
                }
            }
        }
        .scaleEffect(isHovered && onTap != nil ? 1.02 : 1.0)
        .animation(.spring(response: 0.3, dampingFraction: 0.7), value: isHovered)
        .onTapGesture {
            if let action = onTap {
                hapticFeedback(style: .light)
                withAnimation(.spring(response: 0.3, dampingFraction: 0.6)) {
                    isHovered = true
                }
                DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) {
                    withAnimation {
                        isHovered = false
                    }
                }
                action()
            }
        }
        .simultaneousGesture(
            DragGesture(minimumDistance: 0)
                .onChanged { _ in
                    if onTap != nil {
                        isHovered = true
                    }
                }
                .onEnded { _ in
                    isHovered = false
                }
        )
    }
}

// MARK: - Supporting Views

private struct StatPill: View {
    let icon: String
    let value: Int
    let color: Color

    var body: some View {
        HStack(spacing: 3) {
            Text(icon)
                .font(.system(size: 10))
            Text("\(value)")
                .font(.system(size: 11, weight: .bold, design: .rounded))
                .foregroundColor(AppColors.primaryText)
        }
        .padding(.horizontal, AppSpacing.xs)
        .padding(.vertical, 3)
        .background(
            LinearGradient(
                colors: [color.opacity(0.3), color.opacity(0.2)],
                startPoint: .leading,
                endPoint: .trailing
            )
        )
        .cornerRadius(AppSpacing.cornerRadius / 2)
        .overlay(
            RoundedRectangle(cornerRadius: AppSpacing.cornerRadius / 2)
                .strokeBorder(color.opacity(0.4), lineWidth: 1)
        )
    }
}

// MARK: - Preview
#if DEBUG
struct CharacterCard_Previews: PreviewProvider {
    static var previews: some View {
        let mockPerson = Person()
        mockPerson.firstname = "Jane"
        mockPerson.lastname = "Doe"
        mockPerson.ageYears = 28
        mockPerson.relationship = "Friend"
        mockPerson.health = 85
        mockPerson.happiness = 70
        mockPerson.intelligence = 90
        mockPerson.image = "https://example.com/avatar.svg"

        return VStack(spacing: AppSpacing.md) {
            CharacterCard(person: mockPerson)

            CharacterCard(person: mockPerson, showStats: true)

            CharacterCard(person: mockPerson, showStats: true, onTap: {
                print("Card tapped")
            })
        }
        .padding()
        .background(Color.black)
    }
}
#endif
