import SwiftUI

enum SettingsSection: String, CaseIterable, Identifiable {
    case agents, appearance, changes, chat, code, general, remote, shortcuts, spaces, terminal, worktrees, debug
    var id: String { rawValue }
    var label: String {
        switch self {
        case .debug:      return "Debug"
        case .agents:     return "Agents"
        case .appearance: return "Appearance"
        case .changes:    return "Changes"
        case .chat:       return "Chat"
        case .code:       return "Code"
        case .general:    return "General"
        case .remote:     return "Remote"
        case .shortcuts:  return "Shortcuts"
        case .spaces:     return "Spaces"
        case .terminal:   return "Terminal"
        case .worktrees:  return "Worktrees"
        }
    }
    var icon: String {
        switch self {
        case .debug:      return "wrench.and.screwdriver"
        case .agents:     return "sparkle"
        case .appearance: return "palette"
        case .changes:    return "diff"
        case .chat:       return "message"
        case .code:       return "code"
        case .general:    return "gear"
        case .remote:     return "iphone.gen3"
        case .shortcuts:  return "keyboard"
        case .spaces:     return "rectangle.3.group"
        case .terminal:   return "terminal"
        case .worktrees:  return "branch"
        }
    }

    static func visibleSections(showsDebug: Bool) -> [SettingsSection] {
        SettingsSection.allCases
            .filter { showsDebug || $0 != .debug }
            .sorted(by: {
                if $0 == .general { return true }
                if $1 == .general { return false }
                if $0 == .debug { return false }
                if $1 == .debug { return true }
                return $0.label < $1.label
            })
    }
}

struct SettingsNavView: View {
    @Binding var selection: SettingsSection
    var showsDebug: Bool
    @Environment(\.theme) var theme

    var body: some View {
        VStack(spacing: 1) {
            ForEach(sections) { section in
                Button { selection = section } label: {
                    HStack(spacing: 9) {
                        Icon(name: section.icon, size: 14,
                             color: selection == section ? theme.color("accent") : theme.color("fg-dim"))
                            .frame(width: 16, height: 16)
                        Text(section.label).font(.system(size: 12.5))
                            .foregroundColor(selection == section ? theme.color("fg") : theme.color("fg-muted"))
                        Spacer()
                    }
                    .padding(.horizontal, 10).padding(.vertical, 7)
                    .frame(maxWidth: .infinity, alignment: .leading)
                    .background(selection == section ? theme.color("accent-soft") : .clear)
                    .clipShape(RoundedRectangle(cornerRadius: 6))
                    .contentShape(Rectangle())
                }
                .buttonStyle(.plain)
            }
            Spacer()
            Text("Alas \(Bundle.main.infoDictionary?["CFBundleShortVersionString"] as? String ?? "—")")
                .font(.system(size: 10.5, design: .monospaced))
                .foregroundColor(theme.color("fg-faint"))
                .padding(.horizontal, 10).padding(.vertical, 8)
        }
        .padding(.horizontal, 8).padding(.vertical, 12)
        .frame(width: 200)
        .background(theme.color("bg-2"))
        .overlay(Divider().opacity(0.5), alignment: .trailing)
    }

    private var sections: [SettingsSection] {
        SettingsSection.visibleSections(showsDebug: showsDebug)
    }
}
