import SwiftUI
import ComposeApp

struct WarrantyCard: View {
    let document: Document

    var hasEndDate: Bool {
        document.daysUntilExpiration != nil
    }

    var daysUntilExpiration: Int {
        Int(document.daysUntilExpiration ?? 0)
    }

    var statusColor: Color {
        // Use backend-calculated status color if available
        if let warrantyStatus = document.warrantyStatus {
            return Color(hex: warrantyStatus.statusColor) ?? Color.appPrimary
        }

        // Fallback to client-side calculation (shouldn't happen with updated backend)
        if !document.isActive { return Color.appTextSecondary }
        if daysUntilExpiration < 0 { return Color.appError }
        if daysUntilExpiration < 30 { return Color.appAccent }
        if daysUntilExpiration < 90 { return Color.appAccent.opacity(0.7) }
        return Color.appPrimary
    }

    var statusText: String {
        // Use backend-calculated status text if available
        if let warrantyStatus = document.warrantyStatus {
            return warrantyStatus.statusText
        }

        // Fallback to client-side calculation (shouldn't happen with updated backend)
        if !document.isActive { return L10n.Documents.inactive }
        if daysUntilExpiration < 0 { return L10n.Documents.expired }
        if daysUntilExpiration < 30 { return L10n.Documents.expiringSoon }
        return L10n.Documents.activeStatus
    }

    var body: some View {
        VStack(alignment: .leading, spacing: AppSpacing.sm) {
            // Header
            HStack(alignment: .top) {
                VStack(alignment: .leading, spacing: 4) {
                    Text(document.title)
                        .font(.title3.weight(.semibold))
                        .fontWeight(.bold)
                        .foregroundColor(Color.appTextPrimary)

                    Text(document.itemName ?? "")
                        .font(.body)
                        .foregroundColor(Color.appTextSecondary)
                }

                Spacer()

                // Status Badge
                Text(statusText)
                    .font(.caption.weight(.medium))
                    .fontWeight(.bold)
                    .foregroundColor(statusColor)
                    .padding(.horizontal, 8)
                    .padding(.vertical, 4)
                    .background(statusColor.opacity(0.2))
                    .cornerRadius(6)
            }

            Divider()

            // Details
            HStack {
                VStack(alignment: .leading, spacing: 2) {
                    Text(L10n.Documents.provider)
                        .font(.caption.weight(.medium))
                        .foregroundColor(Color.appTextSecondary)
                    Text(document.provider ?? L10n.Documents.na)
                        .font(.body)
                        .fontWeight(.medium)
                        .foregroundColor(Color.appTextPrimary)
                }

                Spacer()

                VStack(alignment: .trailing, spacing: 2) {
                    Text(L10n.Documents.expires)
                        .font(.caption.weight(.medium))
                        .foregroundColor(Color.appTextSecondary)
                    Text(DateUtils.formatDateMedium(document.endDate) ?? L10n.Documents.na)
                        .font(.body)
                        .fontWeight(.medium)
                        .foregroundColor(Color.appTextPrimary)
                }
            }

            if document.isActive && hasEndDate && daysUntilExpiration >= 0 {
                Text(String(format: L10n.Documents.daysRemainingCount, daysUntilExpiration))
                    .font(.footnote.weight(.medium))
                    .foregroundColor(statusColor)
            }

            // Category Badge
            if let category = document.category {
                Text(getCategoryDisplayName(category))
                    .font(.caption.weight(.medium))
                    .foregroundColor(Color.appTextPrimary)
                    .padding(.horizontal, 8)
                    .padding(.vertical, 4)
                    .background(Color.appBackgroundSecondary.opacity(0.8))
                    .cornerRadius(4)
            }
        }
        .padding(AppSpacing.md)
        .background(Color.appBackgroundSecondary)
        .cornerRadius(AppRadius.md)
        .shadow(color: Color.black.opacity(0.05), radius: 2, x: 0, y: 1)
        .accessibilityElement(children: .combine)
    }

    private func getCategoryDisplayName(_ category: String) -> String {
        return DocumentCategory.companion.fromValue(value: category).displayName
    }
}
