//
//  CharacterListCard.swift
//  lichunWebsocket
//
//  Card component for displaying character in lists (matches, relationships)
//

import SwiftUI
import SDWebImageSwiftUI

struct CharacterListCard: View {
    var person: Person
    var showAffinity: Bool = true
    var showRelationships: Bool = true

    var body: some View {
        HStack(spacing: AppSpacing.md) {
            // Avatar
            WebImage(url: URL(string: person.image))
                .resizable()
                .aspectRatio(contentMode: .fill)
                .frame(width: 60, height: 60)
                .clipped()
                .cornerRadius(AppSpacing.cornerRadius)
                .shadow(radius: 3)

            // Character info
            VStack(alignment: .leading, spacing: AppSpacing.xs) {
                HStack(spacing: AppSpacing.xs) {
                    Text(person.firstname + " " + person.lastname)
                        .font(.appHeadline)
                        .foregroundColor(AppColors.primaryText)

                    if showRelationships {
                        ForEach(person.relationships, id: \.self) { relationship in
                            Text(relationship)
                                .font(.appCaption)
                                .foregroundColor(AppColors.secondaryText)
                        }
                    }
                }

                if showAffinity {
                    Text("\(person.affinity)")
                        .font(.appBodyBold)
                        .foregroundColor(getAffinityColor(for: person.affinity))
                }
            }

            Spacer()
        }
        .padding(AppSpacing.md)
        .background(Color(hex: 0xC46E72))
        .cornerRadius(AppSpacing.cornerRadius)
        .shadow(color: Color.black.opacity(0.3), radius: 3, x: 3, y: 3)
    }
}

// Backward compatibility alias
typealias CharacterView = CharacterListCard

// MARK: - Preview
#if DEBUG
struct CharacterListCard_Previews: PreviewProvider {
    static var previews: some View {
        let mockPerson = Person()
        mockPerson.firstname = "Jane"
        mockPerson.lastname = "Doe"
        mockPerson.affinity = 75
        mockPerson.relationships = ["friend", "classmate"]
        mockPerson.image = "https://example.com/avatar.svg"

        return VStack(spacing: AppSpacing.md) {
            CharacterListCard(person: mockPerson)
            CharacterListCard(person: mockPerson, showAffinity: false)
            CharacterListCard(person: mockPerson, showRelationships: false)
        }
        .padding()
        .background(Color.black)
    }
}
#endif
