//
//  RelationshipSnapshotCard.swift
//  lichunWebsocket
//
//  Quick overview card showing key relationship metrics
//

import SwiftUI

struct RelationshipSnapshotCard: View {
    let relationship: Relationship
    let sharedInterestsCount: Int

    private var durationMonths: Int {
        calculateMonths(from: relationship.startDate)
    }

    private var durationLabel: String {
        if durationMonths < 12 {
            return durationMonths == 1 ? "Month" : "Months"
        } else {
            let years = durationMonths / 12
            return years == 1 ? "Year" : "Years"
        }
    }

    private var durationValue: Int {
        if durationMonths < 12 {
            return durationMonths
        } else {
            return durationMonths / 12
        }
    }

    var body: some View {
        BaseCard {
            ZStack {
                // Warm romantic background
                LinearGradient(
                    colors: [
                        AppColors.primary.opacity(0.08),
                        AppColors.accent.opacity(0.05)
                    ],
                    startPoint: .topLeading,
                    endPoint: .bottomTrailing
                )
                .cornerRadius(AppSpacing.cornerRadius)

                VStack(spacing: AppSpacing.sm) {
                    HStack {
                        Image(systemName: "heart.text.square.fill")
                            .font(.appBody)
                            .foregroundColor(AppColors.primary)

                        Text("Relationship Overview")
                            .font(.appHeadline)
                            .foregroundColor(AppColors.primaryText)
                    }

                    Divider()
                        .background(AppColors.secondaryText.opacity(0.2))

                HStack(spacing: AppSpacing.md) {
                    StatBadge(
                        icon: "star.fill",
                        label: "Score",
                        value: relationship.relationshipScore,
                        color: AppColors.accent
                    )

                    StatBadge(
                        icon: "calendar.badge.clock",
                        label: durationLabel,
                        value: durationValue,
                        color: AppColors.primary
                    )

                    StatBadge(
                        icon: "heart.circle.fill",
                        label: "Shared",
                        value: sharedInterestsCount,
                        color: AppColors.success
                    )
                }
                .padding(.bottom, AppSpacing.xs)
                }
            }
        }
    }

    // MARK: - Helper Methods

    private func calculateMonths(from dateString: String) -> Int {
        // Simple calculation based on events log count as proxy
        // In production, would parse actual date string
        let eventCount = relationship.eventsLog.count

        // Estimate ~1 event per month
        return max(1, eventCount)
    }
}

// MARK: - Preview

#if DEBUG
struct RelationshipSnapshotCard_Previews: PreviewProvider {
    static var previews: some View {
        VStack(spacing: AppSpacing.lg) {
            // Long-term relationship
            RelationshipSnapshotCard(
                relationship: mockRelationship(months: 24, score: 95),
                sharedInterestsCount: 8
            )

            // Medium-term relationship
            RelationshipSnapshotCard(
                relationship: mockRelationship(months: 10, score: 87),
                sharedInterestsCount: 5
            )

            // New relationship
            RelationshipSnapshotCard(
                relationship: mockRelationship(months: 2, score: 65),
                sharedInterestsCount: 3
            )
        }
        .padding()
        .background(AppColors.primaryBackground)
    }

    static func mockRelationship(months: Int, score: Int) -> Relationship {
        var events: [String] = []
        for i in 0..<months {
            events.append("Event \(i + 1)")
        }

        return Relationship(
            id: "rel123",
            person1: "player123",
            person2: "partner123",
            startDate: "Jan 15, 2024",
            relationshipStatus: "Dating",
            relationshipNotes: "Met at a coffee shop",
            eventsLog: events,
            relationshipScore: score,
            commonInterests: ["music", "hiking", "cooking"],
            challenges: [],
            futurePlans: []
        )
    }
}
#endif
