import SwiftUI
import ComposeApp

// Action buttons accept a shared TaskViewModel from the parent view to avoid redundant instances.

// MARK: - Edit Task Button
struct EditTaskButton: View {
    let taskId: Int32
    let onCompletion: () -> Void
    let onError: (String) -> Void

    var body: some View {
        Button(action: {
            // Edit navigates to edit screen - handled by parent
            onCompletion()
        }) {
            Label("Edit", systemImage: "pencil")
                .font(.subheadline)
                .frame(maxWidth: .infinity)
        }
        .buttonStyle(.bordered)
        .accessibilityLabel("Edit task")
        .accessibilityHint("Double tap to edit this task")
    }
}

// MARK: - Cancel Task Button
struct CancelTaskButton: View {
    let taskId: Int32
    let onCompletion: () -> Void
    let onError: (String) -> Void

    let viewModel: TaskViewModel
    @State private var showConfirmation = false

    var body: some View {
        Button(action: {
            showConfirmation = true
        }) {
            Label("Cancel", systemImage: "xmark.circle")
                .font(.subheadline)
                .frame(maxWidth: .infinity)
        }
        .buttonStyle(.bordered)
        .tint(Color.appError)
        .accessibilityLabel("Cancel task")
        .accessibilityHint("Double tap to cancel this task")
        .alert("Cancel Task", isPresented: $showConfirmation) {
            Button("Cancel", role: .cancel) { }
            Button("Cancel Task", role: .destructive) {
                viewModel.cancelTask(id: taskId) { success in
                    if success {
                        onCompletion()
                    } else {
                        onError("Failed to cancel task")
                    }
                }
            }
        } message: {
            Text("Are you sure you want to cancel this task? You can undo this later.")
        }
    }
}

// MARK: - Uncancel (Restore) Task Button
struct UncancelTaskButton: View {
    let taskId: Int32
    let onCompletion: () -> Void
    let onError: (String) -> Void

    let viewModel: TaskViewModel

    var body: some View {
        Button(action: {
            viewModel.uncancelTask(id: taskId) { success in
                if success {
                    onCompletion()
                } else {
                    onError("Failed to restore task")
                }
            }
        }) {
            Label("Restore", systemImage: "arrow.uturn.backward")
                .font(.subheadline)
                .frame(maxWidth: .infinity)
        }
        .buttonStyle(.borderedProminent)
        .tint(Color.appPrimary)
        .accessibilityLabel("Restore task")
        .accessibilityHint("Double tap to restore this task")
    }
}

// MARK: - Mark In Progress Button
struct MarkInProgressButton: View {
    let taskId: Int32
    let onCompletion: () -> Void
    let onError: (String) -> Void

    let viewModel: TaskViewModel

    var body: some View {
        Button(action: {
            viewModel.markInProgress(id: taskId) { success in
                if success {
                    onCompletion()
                } else {
                    onError("Failed to mark task in progress")
                }
            }
        }) {
            HStack {
                Image(systemName: "play.circle.fill")
                    .resizable()
                    .frame(width: 18, height: 18)
                Text("In Progress")
                    .font(.subheadline.weight(.semibold))
            }
            .frame(maxWidth: .infinity)
        }
        .buttonStyle(.bordered)
        .tint(Color.appAccent)
        .accessibilityLabel("Mark as in progress")
        .accessibilityHint("Double tap to mark this task as in progress")
    }
}

// MARK: - Complete Task Button
struct CompleteTaskButton: View {
    let taskId: Int32
    let onCompletion: () -> Void
    let onError: (String) -> Void

    var body: some View {
        Button(action: {
            // Complete shows dialog - handled by parent
            onCompletion()
        }) {
            HStack {
                Image(systemName: "checkmark.circle.fill")
                    .resizable()
                    .frame(width: 18, height: 18)
                Text("Complete")
                    .font(.subheadline.weight(.semibold))
            }
            .frame(maxWidth: .infinity)
        }
        .buttonStyle(.borderedProminent)
        .accessibilityLabel("Complete task")
        .accessibilityHint("Double tap to complete this task")
    }
}

// MARK: - Archive Task Button
struct ArchiveTaskButton: View {
    let taskId: Int32
    let onCompletion: () -> Void
    let onError: (String) -> Void

    let viewModel: TaskViewModel
    @State private var showConfirmation = false

    var body: some View {
        Button(action: {
            showConfirmation = true
        }) {
            Label("Archive", systemImage: "archivebox")
                .font(.subheadline)
                .frame(maxWidth: .infinity)
        }
        .buttonStyle(.bordered)
        .tint(.gray)
        .accessibilityLabel("Archive task")
        .accessibilityHint("Double tap to archive this task")
        .alert("Archive Task", isPresented: $showConfirmation) {
            Button("Cancel", role: .cancel) { }
            Button("Archive", role: .destructive) {
                viewModel.archiveTask(id: taskId) { success in
                    if success {
                        onCompletion()
                    } else {
                        onError("Failed to archive task")
                    }
                }
            }
        } message: {
            Text("Are you sure you want to archive this task? You can unarchive it later from archived tasks.")
        }
    }
}

// MARK: - Unarchive Task Button
struct UnarchiveTaskButton: View {
    let taskId: Int32
    let onCompletion: () -> Void
    let onError: (String) -> Void

    let viewModel: TaskViewModel

    var body: some View {
        Button(action: {
            viewModel.unarchiveTask(id: taskId) { success in
                if success {
                    onCompletion()
                } else {
                    onError("Failed to unarchive task")
                }
            }
        }) {
            Label("Unarchive", systemImage: "tray.and.arrow.up")
                .font(.subheadline)
                .frame(maxWidth: .infinity)
        }
        .buttonStyle(.bordered)
        .tint(Color.appPrimary)
        .accessibilityLabel("Unarchive task")
        .accessibilityHint("Double tap to unarchive this task")
    }
}
