//
// OpenRocky — Voice-first AI Agent
// https://github.com/openrocky
//
// Developed by everettjf with the assistance of Claude Code and Codex.
// Copyright (c) 2026 everettjf. All rights reserved.
//

import SwiftUI

/// Kind of permission moment the user is in.
enum OpenRockyPermissionPromptKind: String, Equatable, Identifiable {
    /// First-time primer shown before we call the system permission dialog.
    case micPriming
    /// User already denied the permission; we can only deep-link to Settings.
    case micDenied

    var id: String { rawValue }
}

/// Reusable permission prompt sheet used by the voice entry points.
struct OpenRockyPermissionPromptView: View {
    let kind: OpenRockyPermissionPromptKind
    let onPrimaryAction: () -> Void
    let onDismiss: () -> Void

    var body: some View {
        VStack(spacing: 24) {
            Spacer(minLength: 24)

            ZStack {
                Circle()
                    .fill(iconTint.opacity(0.18))
                    .frame(width: 96, height: 96)
                Image(systemName: iconName)
                    .font(.system(size: 40, weight: .semibold))
                    .foregroundStyle(iconTint)
            }

            VStack(spacing: 10) {
                Text(title)
                    .font(.system(size: 22, weight: .bold, design: .rounded))
                    .multilineTextAlignment(.center)
                Text(message)
                    .font(.system(size: 15, weight: .medium))
                    .foregroundStyle(.secondary)
                    .multilineTextAlignment(.center)
                    .padding(.horizontal, 24)
            }

            Spacer(minLength: 12)

            VStack(spacing: 10) {
                Button(action: onPrimaryAction) {
                    Text(primaryActionLabel)
                        .font(.system(size: 16, weight: .semibold, design: .rounded))
                        .frame(maxWidth: .infinity)
                        .padding(.vertical, 14)
                        .background(
                            RoundedRectangle(cornerRadius: 14, style: .continuous)
                                .fill(Color.accentColor)
                        )
                        .foregroundStyle(.white)
                }

                Button(action: onDismiss) {
                    Text("Not Now")
                        .font(.system(size: 15, weight: .medium, design: .rounded))
                        .foregroundStyle(.secondary)
                        .frame(maxWidth: .infinity)
                        .padding(.vertical, 10)
                }
            }
            .padding(.horizontal, 20)
            .padding(.bottom, 24)
        }
        .padding(.top, 12)
        .presentationDetents([.medium])
        .presentationDragIndicator(.visible)
    }

    private var iconName: String {
        switch kind {
        case .micPriming, .micDenied: "mic.fill"
        }
    }

    private var iconTint: Color {
        switch kind {
        case .micPriming: .blue
        case .micDenied: .orange
        }
    }

    private var title: String {
        switch kind {
        case .micPriming: "Let Rocky Listen"
        case .micDenied: "Microphone Access Needed"
        }
    }

    private var message: String {
        switch kind {
        case .micPriming:
            "Rocky is voice-first. Talking is faster than typing — ask it to plan your day, check your health, or run a task, all hands-free. Your voice stays on your device except when sent to the AI provider you chose."
        case .micDenied:
            "Rocky can't hear you right now. Open Settings and enable Microphone access for Rocky to start a voice conversation."
        }
    }

    private var primaryActionLabel: String {
        switch kind {
        case .micPriming: "Continue"
        case .micDenied: "Open Settings"
        }
    }
}
