import SwiftUI
import ComposeApp

/// Task card that dynamically renders buttons based on the column's button types
struct DynamicTaskCard: View {
    let task: TaskResponse
    let buttonTypes: [String]
    let onEdit: () -> Void
    let onCancel: () -> Void
    let onUncancel: () -> Void
    let onMarkInProgress: () -> Void
    let onComplete: () -> Void
    let onArchive: () -> Void
    let onUnarchive: () -> Void

    @State private var showCompletionHistory = false

    var body: some View {
//        let _ = print("📋 DynamicTaskCard - Task: \(task.title), ButtonTypes: \(buttonTypes)")

        VStack(alignment: .leading, spacing: 12) {
            HStack {
                VStack(alignment: .leading, spacing: 4) {
                    Text(task.title)
                        .font(.title3)
                        .foregroundColor(.primary)

                    if task.inProgress {
                        StatusBadge(status: "in_progress")
                    }
                }

                Spacer()

                if let priorityName = task.priorityName, !priorityName.isEmpty {
                    PriorityBadge(priority: priorityName)
                }
            }

            if !task.description_.isEmpty {
                Text(task.description_)
                    .font(.subheadline)
                    .foregroundColor(Color.appTextSecondary)
                    .lineLimit(2)
            }

            HStack {
                if let frequency = task.frequencyDisplayName, !frequency.isEmpty {
                    Label(frequency, systemImage: "repeat")
                        .font(.caption)
                        .foregroundColor(Color.appTextSecondary)
                }

                Spacer()

                if let effectiveDate = task.effectiveDueDate {
                    Label(effectiveDate.toFormattedDate(), systemImage: "calendar")
                        .font(.caption)
                        .foregroundColor(Color.appTextSecondary)
                }
            }

            // Actions row with completion count button and actions menu
            if !buttonTypes.isEmpty || task.completionCount > 0 {
                Divider()

                HStack(spacing: 12) {
                    // Actions menu
                    if !buttonTypes.isEmpty {
                        Menu {
                            menuContent
                        } label: {
                            HStack {
                                Image(systemName: "ellipsis.circle.fill")
                                    .font(.title3)
                                Text("Actions")
                                    .fontWeight(.medium)
                            }
                            .frame(maxWidth: .infinity)
                            .padding(.vertical, 12)
                            .background(Color.appPrimary.opacity(0.1))
                            .foregroundColor(Color.appPrimary)
                            .cornerRadius(8)
                            .overlay(
                                RoundedRectangle(cornerRadius: 8)
                                    .stroke(Color.appPrimary, lineWidth: 2)
                            )
                        }
                        .accessibilityLabel("Task actions")
                        .zIndex(10)
                        .menuOrder(.fixed)
                    }
                    
                    // Completion count button - shows when count > 0
                    if task.completionCount > 0 {
                        Button(action: {
                            showCompletionHistory = true
                        }) {
                            HStack(spacing: 6) {
                                Image(systemName: "checkmark.circle.fill")
                                    .font(.title3)
                                Text("\(task.completionCount)")
                                    .fontWeight(.bold)
                            }
                            .padding(.horizontal, 16)
                            .padding(.vertical, 12)
                            .background(Color.appAccent.opacity(0.1))
                            .foregroundColor(Color.appAccent)
                            .cornerRadius(8)
                            .overlay(
                                RoundedRectangle(cornerRadius: 8)
                                    .stroke(Color.appAccent, lineWidth: 2)
                            )
                        }
                        .accessibilityLabel("View \(task.completionCount) completions")
                    }
                }
            }
        }
        .padding(16)
        .background(Color.appBackgroundSecondary)
        .cornerRadius(12)
        .shadow(color: Color.black.opacity(0.1), radius: 5, x: 0, y: 2)
        .simultaneousGesture(TapGesture(), including: .subviews)
        .sheet(isPresented: $showCompletionHistory) {
            CompletionHistorySheet(
                taskTitle: task.title,
                taskId: task.id,
                isPresented: $showCompletionHistory
            )
            .presentationDetents([.medium, .large])
            .presentationDragIndicator(.visible)
        }
    }

    // MARK: - Menu Content

    @ViewBuilder
    private var menuContent: some View {
        // Primary actions
        ForEach(Array(buttonTypes.enumerated()), id: \.offset) { index, buttonType in
            if isPrimaryAction(buttonType) {
                menuButton(for: buttonType)
            }
        }

        // Secondary actions (if any exist)
        if buttonTypes.contains(where: { isSecondaryAction($0) }) {
            Divider()

            ForEach(Array(buttonTypes.enumerated()), id: \.offset) { index, buttonType in
                if isSecondaryAction(buttonType) {
                    menuButton(for: buttonType)
                }
            }
        }

        // Destructive actions (if any exist)
        if buttonTypes.contains(where: { isDestructiveAction($0) }) {
            Divider()

            ForEach(Array(buttonTypes.enumerated()), id: \.offset) { index, buttonType in
                if isDestructiveAction(buttonType) {
                    menuButton(for: buttonType)
                }
            }
        }
    }

    private func isPrimaryAction(_ buttonType: String) -> Bool {
        ["mark_in_progress", "complete", "edit", "uncancel", "unarchive"].contains(buttonType)
    }

    private func isSecondaryAction(_ buttonType: String) -> Bool {
        ["archive"].contains(buttonType)
    }

    private func isDestructiveAction(_ buttonType: String) -> Bool {
        ["cancel"].contains(buttonType)
    }

    @ViewBuilder
    private func menuButton(for buttonType: String) -> some View {
        switch buttonType {
        case "mark_in_progress":
            Button {
                #if DEBUG
                print("Mark In Progress tapped for task: \(task.id)")
                #endif
                onMarkInProgress()
            } label: {
                Label("Mark Task In Progress", systemImage: "play.circle")
            }
            .accessibilityIdentifier(AccessibilityIdentifiers.Task.markInProgressButton)
        case "complete":
            Button {
                #if DEBUG
                print("Complete tapped for task: \(task.id)")
                #endif
                onComplete()
            } label: {
                Label("Complete Task", systemImage: "checkmark.circle")
            }
            .accessibilityIdentifier(AccessibilityIdentifiers.Task.completeButton)
        case "edit":
            Button {
                #if DEBUG
                print("Edit tapped for task: \(task.id)")
                #endif
                onEdit()
            } label: {
                Label("Edit Task", systemImage: "pencil")
            }
            .accessibilityIdentifier(AccessibilityIdentifiers.Task.editButton)
        case "cancel":
            Button(role: .destructive) {
                #if DEBUG
                print("Cancel tapped for task: \(task.id)")
                #endif
                onCancel()
            } label: {
                Label("Cancel Task", systemImage: "xmark.circle")
            }
            .accessibilityIdentifier(AccessibilityIdentifiers.Task.deleteButton)
        case "uncancel":
            Button {
                #if DEBUG
                print("Restore tapped for task: \(task.id)")
                #endif
                onUncancel()
            } label: {
                Label("Restore Task", systemImage: "arrow.uturn.backward.circle")
            }
        case "archive":
            Button {
                #if DEBUG
                print("Archive tapped for task: \(task.id)")
                #endif
                onArchive()
            } label: {
                Label("Archive Task", systemImage: "archivebox")
            }
        case "unarchive":
            Button {
                #if DEBUG
                print("Unarchive tapped for task: \(task.id)")
                #endif
                onUnarchive()
            } label: {
                Label("Unarchive Task", systemImage: "arrow.up.bin")
            }
        default:
            EmptyView()
        }
    }
}
