import SwiftUI
import ComponentLibrary

// Action menu sheet with options for Model, Upload, Record, Library, and Persona
struct ActionMenuSheet: View {
    @Environment(\.dismiss) private var dismiss

    let currentModel: String
    let onModelSelect: (String) -> Void
    let onUploadTap: () -> Void
    let onRecordTap: () -> Void
    let onLibraryTap: () -> Void

    var body: some View {
        VStack(spacing: 0) {
            // Grabber and action buttons
            VStack(spacing: 12) {
                RoundedRectangle(cornerRadius: 100)
                    .fill(Color.white.opacity(0.1))
                    .frame(width: 32, height: 4)

                // Three action buttons in a row
                HStack(spacing: 16) {
                    ActionSheetButton(icon: Image.FigmaMCP.upload, title: "Upload", onTap: {
                        dismiss()
                        onUploadTap()
                    })

                    ActionSheetButton(icon: Image.FigmaMCP.microphone, title: "Record", onTap: {
                        dismiss()
                        onRecordTap()
                    })

                    ActionSheetButton(icon: Image.FigmaMCP.library, title: "Library", onTap: {
                        dismiss()
                        onLibraryTap()
                    })
                }
            }
            .padding(16)
            .frame(maxWidth: .infinity)

            // Menu cells container
            VStack(spacing: 0) {
                VStack(spacing: 0) {
                    // Model - with menu
                    Menu {
                        Button {
                            onModelSelect("v5")
                        } label: {
                            HStack {
                                Text("v5")
                                if currentModel == "v5" {
                                    Image(systemName: "checkmark")
                                }
                            }
                        }

                        Button {
                            onModelSelect("v4.5")
                        } label: {
                            HStack {
                                Text("v4.5")
                                if currentModel == "v4.5" {
                                    Image(systemName: "checkmark")
                                }
                            }
                        }

                        Button {
                            onModelSelect("v4")
                        } label: {
                            HStack {
                                Text("v4")
                                if currentModel == "v4" {
                                    Image(systemName: "checkmark")
                                }
                            }
                        }

                        Divider()

                        Menu {
                            Button {
                                onModelSelect("v3")
                            } label: {
                                HStack {
                                    Text("v3")
                                    if currentModel == "v3" {
                                        Image(systemName: "checkmark")
                                    }
                                }
                            }

                            Button {
                                onModelSelect("v2")
                            } label: {
                                HStack {
                                    Text("v2")
                                    if currentModel == "v2" {
                                        Image(systemName: "checkmark")
                                    }
                                }
                            }
                        } label: {
                            HStack {
                                Text("Legacy Models")
                                if currentModel == "v3" || currentModel == "v2" {
                                    Image(systemName: "checkmark")
                                }
                            }
                        }
                    } label: {
                        ActionMenuCellContent(
                            icon: Image.FigmaMCP.sparkles,
                            title: "Model",
                            subtitle: currentModel
                        )
                    }
                    .buttonStyle(PlainButtonStyle())
                    .menuOrder(.fixed)
                }
                .background(Color.white.opacity(0.04))
                .cornerRadius(16)
            }
            .padding(.horizontal, 16)

            Spacer()
        }
        .frame(maxWidth: .infinity, maxHeight: .infinity)
        .selfSizingPresentation()
        .presentationDragIndicator(.hidden)
        .presentationCornerRadius(24)
        .modify {
            /// Use default styling on iOS 26+
            if #available(iOS 26.0, *) {
                $0
            } else {
                $0.presentationBackground {
                    Color.FigmaMCP.Semantic.smokeDense
                        .background(.ultraThinMaterial)
                }
            }
        }
        .preferredColorScheme(.dark)
    }
}

// Cell content that can be used in both buttons and menus
private struct ActionMenuCellContent: View {
    let icon: Image
    let title: String
    let subtitle: String

    var body: some View {
        HStack(spacing: 10) {
            // Icon
            icon
                .figmaMCPIconStyle(
                    size: Image.FigmaMCPSize.medium,
                    semanticColor: ChatConstants.Colors.Foreground.primary
                )

            // Title and subtitle
            VStack(alignment: .leading, spacing: 0) {
                Text(title)
                    .figmaMCPTypography(TypographyV1.FigmaMCP.mediumTitle)
                    .foregroundColor(ChatConstants.Colors.Foreground.primary)
                    .tracking(0.32)

                Text(subtitle)
                    .figmaMCPTypography(TypographyV1.FigmaMCP.xSmallRegular)
                    .foregroundColor(ChatConstants.Colors.Foreground.tertiary)
                    .tracking(0.24)
            }
            .frame(maxWidth: .infinity, alignment: .leading)

            // Chevron
            Image.FigmaMCP.chevronRight
                .figmaMCPIconStyle(
                    size: Image.FigmaMCPSize.small,
                    semanticColor: ChatConstants.Colors.Foreground.tertiary
                )
        }
        .padding(16)
    }
}

private struct ActionMenuCell: View {
    let icon: Image
    let title: String
    let subtitle: String
    let onTap: () -> Void

    var body: some View {
        Button(action: onTap) {
            ActionMenuCellContent(
                icon: icon,
                title: title,
                subtitle: subtitle
            )
        }
        .buttonStyle(PlainButtonStyle())
    }
}

// Action button component for Upload, Record, Library
private struct ActionSheetButton: View {
    let icon: Image
    let title: String
    let onTap: () -> Void

    var body: some View {
        Button(action: onTap) {
            VStack(spacing: 8) {
                icon
                    .figmaMCPIconStyle(
                        size: Image.FigmaMCPSize.medium,
                        semanticColor: ChatConstants.Colors.Foreground.primary
                    )

                Text(title)
                    .figmaMCPTypography(TypographyV1.FigmaMCP.mediumTitle)
                    .foregroundColor(.white)
                    .tracking(0.32)
            }
            .frame(maxWidth: .infinity)
            .padding(.vertical, 24)
            .background(Color.white.opacity(0.04))
            .cornerRadius(16)
        }
        .buttonStyle(PlainButtonStyle())
    }
}

#Preview {
    ActionMenuSheet(
        currentModel: "v4.5",
        onModelSelect: { model in print("Model selected: \(model)") },
        onUploadTap: { print("Upload tapped") },
        onRecordTap: { print("Record tapped") },
        onLibraryTap: { print("Library tapped") }
    )
}

