import SwiftUI
import Bedrock

struct ContactRowView: View {
    let contact: Contact
    let relativeDate: String

    var body: some View {
        HStack(spacing: Design.Spacing.medium) {
            ContactAvatarView(contact: contact)

            VStack(alignment: .leading, spacing: Design.Spacing.xxSmall) {
                HStack(spacing: Design.Spacing.xSmall) {
                    Text(contact.name)
                        .typography(.heading)
                        .foregroundStyle(Color.Text.primary)

                    if contact.isReceivedCard {
                        Image(systemName: "arrow.down.circle.fill")
                            .typography(.caption)
                            .foregroundStyle(Color.Accent.mint)
                    }

                    if contact.hasFollowUp {
                        Image(systemName: contact.isFollowUpOverdue ? "exclamationmark.circle.fill" : "clock.fill")
                            .typography(.caption)
                            .foregroundStyle(contact.isFollowUpOverdue ? Color.Accent.red : Color.Accent.gold)
                    }
                }

                if !contact.role.isEmpty || !contact.company.isEmpty {
                    Text("\(contact.role)\(contact.role.isEmpty || contact.company.isEmpty ? "" : " · ")\(contact.company)")
                        .typography(.subheading)
                        .foregroundStyle(Color.Text.secondary)
                        .lineLimit(1)
                }

                if !contact.tagList.isEmpty {
                    HStack(spacing: Design.Spacing.xSmall) {
                        ForEach(contact.tagList.prefix(2), id: \.self) { tag in
                            Text(tag)
                                .typography(.caption2)
                                .padding(.horizontal, Design.Spacing.xSmall)
                                .padding(.vertical, Design.Spacing.xxSmall)
                                .background(Color.AppBackground.accent)
                                .clipShape(.rect(cornerRadius: Design.CornerRadius.small))
                        }
                    }
                }
            }

            Spacer()

            VStack(alignment: .trailing, spacing: Design.Spacing.xxSmall) {
                Text(relativeDate)
                    .typography(.caption)
                    .foregroundStyle(Color.Text.secondary)
                Text(String.localized(contact.cardLabel))
                    .typography(.caption)
                    .padding(.horizontal, Design.Spacing.small)
                    .padding(.vertical, Design.Spacing.xxSmall)
                    .background(Color.AppBackground.base)
                    .clipShape(.rect(cornerRadius: Design.CornerRadius.medium))
            }
        }
        .accessibilityElement(children: .ignore)
        .accessibilityLabel(contact.name)
        .accessibilityValue("\(contact.role), \(contact.company)")
    }
}
