import SwiftUI
import ComposeApp

struct DocumentCard: View {
    let document: Document

    var typeColor: Color {
        switch document.documentType {
        case "warranty": return Color.appPrimary
        case "manual": return Color.appSecondary
        case "receipt": return Color.appAccent
        case "inspection": return Color.appAccent
        default: return Color.appTextSecondary
        }
    }

    var typeIcon: String {
        switch document.documentType {
        case "photo": return "photo"
        case "warranty", "insurance": return "checkmark.shield"
        case "manual": return "book"
        case "receipt": return "receipt"
        default: return "doc.text"
        }
    }

    var body: some View {
        HStack(spacing: AppSpacing.md) {
            // Document Icon
            VStack {
                Image(systemName: typeIcon)
                    .font(.system(size: 24))
                    .foregroundColor(typeColor)
                    .background(content: {
                        RoundedRectangle(cornerRadius: 8)
                            .fill(typeColor.opacity(0.1))
                            .frame(width: 56, height: 56)
                    })
                    .padding(AppSpacing.md)
                    .accessibilityHidden(true)

                Spacer()
            }

            VStack(alignment: .leading, spacing: 4) {
                Text(document.title)
                    .font(.title3.weight(.semibold))
                    .fontWeight(.bold)
                    .foregroundColor(Color.appTextPrimary)
//                    .lineLimit(1)

                if let description = document.description_, !description.isEmpty {
                    Text(description)
                        .font(.callout)
                        .foregroundColor(Color.appTextSecondary)
                        .lineLimit(2)
                }

                HStack(spacing: 8) {
                    Text(getDocTypeDisplayName(document.documentType))
                        .font(.caption.weight(.medium))
                        .foregroundColor(typeColor)
                        .padding(.horizontal, 6)
                        .padding(.vertical, 2)
                        .background(typeColor.opacity(0.2))
                        .cornerRadius(4)

                    if let fileSize = document.fileSize {
                        Text(formatFileSize(Int(fileSize)))
                            .font(.caption.weight(.medium))
                            .foregroundColor(Color.appTextSecondary)
                    }
                }
            }

            Spacer()

            Image(systemName: "chevron.right")
                .foregroundColor(Color.appTextSecondary)
                .font(.system(size: 14))
                .accessibilityHidden(true)
        }
        .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 getDocTypeDisplayName(_ type: String) -> String {
        return DocumentType.companion.fromValue(value: type).displayName
    }

    private func formatFileSize(_ bytes: Int) -> String {
        var size = Double(bytes)
        let units = ["B", "KB", "MB", "GB"]
        var unitIndex = 0

        while size >= 1024 && unitIndex < units.count - 1 {
            size /= 1024
            unitIndex += 1
        }

        return String(format: "%.1f %@", size, units[unitIndex])
    }
}
