import SwiftUI
import NucleicCore

/// Shown when an agent's `check_conflict` call detects its task overlaps another active
/// agent's work. The asking agent is blocked until the user picks one of three actions;
/// the choice flows back to it as the tool result (internal project management).
struct ConflictSheet: View {
    @Environment(AppStore.self) private var store
    @Environment(\.dismiss) private var dismiss

    let prompt: ConflictPrompt

    var body: some View {
        VStack(alignment: .leading, spacing: 16) {
            HStack(spacing: 10) {
                Image(systemName: "exclamationmark.triangle.fill")
                    .foregroundStyle(.orange)
                    .font(.title2)
                VStack(alignment: .leading, spacing: 2) {
                    Text("Possible conflict").font(.title2).bold()
                    Text("“\(prompt.sessionTitle)” wants to start a task that overlaps work "
                        + "already in progress.")
                        .font(.callout).foregroundStyle(.secondary)
                }
            }

            VStack(alignment: .leading, spacing: 6) {
                Text("Task").font(.caption).foregroundStyle(.secondary)
                Text(prompt.task).font(.callout)
            }
            .frame(maxWidth: .infinity, alignment: .leading)
            .padding(12)
            .background(AppTheme.surface, in: .rect(cornerRadius: 10))
            .overlay(RoundedRectangle(cornerRadius: 10).strokeBorder(AppTheme.hairline, lineWidth: 1))

            VStack(alignment: .leading, spacing: 8) {
                Text("Conflicts with").font(.caption).foregroundStyle(.secondary)
                ForEach(prompt.matches) { match in
                    VStack(alignment: .leading, spacing: 2) {
                        Text(match.sessionTitle).font(.callout).bold()
                        Text(match.reason).font(.caption).foregroundStyle(.secondary)
                    }
                    .frame(maxWidth: .infinity, alignment: .leading)
                }
            }

            Text("“Wait for Access” keeps this agent queued until the conflicting work "
                + "merges, then lets it continue automatically.")
                .font(.caption).foregroundStyle(.secondary)
                .frame(maxWidth: .infinity, alignment: .leading)

            Divider()

            HStack(spacing: 10) {
                Button("Cancel Task", role: .destructive) { resolve(.cancel) }
                Spacer()
                Button("Proceed Anyway") { resolve(.proceed) }
                Button("Wait for Access") { resolve(.waitForAccess) }
                Button("Add to To-Do") { resolve(.defer) }
                    .keyboardShortcut(.defaultAction)
                    .buttonStyle(.borderedProminent)
            }
        }
        .padding(20)
        .frame(width: 480)
        .interactiveDismissDisabled(true)
    }

    private func resolve(_ choice: ConflictChoice) {
        Task { await store.resolveConflict(choice) }
    }
}
