import SwiftUI
import ComposeApp

struct CompletionCardView: View {
    let completion: TaskCompletionResponse
    @State private var showPhotoSheet = false

    var body: some View {
        VStack(alignment: .leading, spacing: 8) {
            HStack {
                Text(completion.completionDate.toFormattedDate())
                    .font(.caption)
                    .fontWeight(.semibold)
                    .foregroundColor(Color.appPrimary)

                Spacer()

                if let rating = completion.rating {
                    HStack(spacing: 2) {
                        Image(systemName: "star.fill")
                            .font(.caption2)
                        Text("\(rating)")
                            .font(.caption)
                            .fontWeight(.bold)
                    }
                    .foregroundColor(Color.appAccent)
                    .padding(.horizontal, 8)
                    .padding(.vertical, 4)
                    .background(Color.appAccent.opacity(0.1))
                    .clipShape(RoundedRectangle(cornerRadius: 8, style: .continuous))
                    .accessibilityLabel("Rated \(rating) out of 5")
                }
            }

            // Display contractor or manual entry
            if let contractorDetails = completion.contractorDetails {
                HStack(alignment: .top, spacing: 6) {
                    Image(systemName: "wrench.and.screwdriver")
                        .font(.caption2)
                        .foregroundColor(Color.appPrimary)

                    VStack(alignment: .leading, spacing: 2) {
                        Text("By: \(contractorDetails.name)")
                            .font(.caption2)
                            .fontWeight(.medium)
                            .foregroundColor(Color.appTextPrimary)

                        if let company = contractorDetails.company {
                            Text(company)
                                .font(.caption2)
                                .foregroundColor(Color.appTextSecondary)
                        }
                    }
                }
            } else if let completedBy = completion.completedByName {
                Text("By: \(completedBy)")
                    .font(.caption2)
                    .foregroundColor(Color.appTextSecondary)
            }

            if let cost = completion.actualCost {
                Text("Cost: \(cost.toCurrency())")
                    .font(.caption2)
                    .foregroundColor(Color.appPrimary)
                    .fontWeight(.medium)
            }

            if !completion.notes.isEmpty {
                Text(completion.notes)
                    .font(.caption2)
                    .foregroundColor(Color.appTextSecondary)
                    .lineLimit(2)
            }

            // Show button to view photos if images exist
            if !completion.images.isEmpty {
                let images = completion.images
                Button(action: {
                    showPhotoSheet = true
                }) {
                    HStack {
                        Image(systemName: "photo.on.rectangle")
                            .font(.caption)
                        Text("View Photos (\(images.count))")
                            .font(.caption)
                            .fontWeight(.semibold)
                    }
                    .frame(maxWidth: .infinity)
                    .padding(.vertical, 8)
                    .background(Color.appPrimary.opacity(0.1))
                    .foregroundColor(Color.appPrimary)
                    .clipShape(RoundedRectangle(cornerRadius: 10, style: .continuous))
                }
                .accessibilityLabel("View \(images.count) photos")
            }
        }
        .padding(14)
        .background(Color.appBackgroundSecondary.opacity(0.5))
        .clipShape(RoundedRectangle(cornerRadius: 14, style: .continuous))
        .sheet(isPresented: $showPhotoSheet) {
            PhotoViewerSheet(images: completion.images)
        }
    }
}
