// MainWindow.swift
// MCPProxy

import SwiftUI

enum SidebarItem: String, CaseIterable, Identifiable {
    case dashboard = "Dashboard"
    case servers = "Servers"
    case registries = "Registries"
    case activity = "Activity Log"
    case secrets = "Secrets"

    var id: String { rawValue }

    var icon: String {
        switch self {
        case .dashboard: return "rectangle.3.group"
        case .servers: return "server.rack"
        case .registries: return "books.vertical"
        case .activity: return "clock.arrow.circlepath"
        case .secrets: return "key.fill"
        }
    }

}

struct MainWindow: View {
    @ObservedObject var appState: AppState
    @State private var selectedItem: SidebarItem? = .dashboard

    var body: some View {
        NavigationSplitView {
            List(selection: $selectedItem) {
                ForEach(SidebarItem.allCases) { item in
                    Label(item.rawValue, systemImage: item.icon)
                        .tag(item)
                        .accessibilityIdentifier("sidebar-\(item.rawValue)")
                }
            }
            // Cap the sidebar width so SwiftUI cannot expand it past a
            // sensible upper bound. Without `max:`, the sidebar can grow
            // unbounded after certain layout transitions (e.g. exiting a
            // detail view back to the list), leaving the detail pane
            // squeezed into a sliver on the right. 280pt keeps long
            // labels like "Activity Log" fully readable while leaving
            // the main content area generous space.
            .navigationSplitViewColumnWidth(min: 180, ideal: 220, max: 280)
            .listStyle(.sidebar)
            .accessibilityIdentifier("sidebar-list")
        } detail: {
            VStack(spacing: 0) {
                // Core status banner — shown when not connected
                if appState.coreState != .connected {
                    coreStatusBanner
                }

                // Regular content
                Group {
                    switch selectedItem ?? .dashboard {
                    case .dashboard:
                        DashboardView(appState: appState)
                    case .servers:
                        ServersView(appState: appState)
                    case .registries:
                        RegistriesView(appState: appState)
                    case .activity:
                        ActivityView(appState: appState)
                    case .secrets:
                        SecretsView(appState: appState)
                    }
                }
                .frame(maxWidth: .infinity, maxHeight: .infinity)
            }
            .environment(\.fontScale, appState.fontScale)
            .accessibilityIdentifier("detail-view")
        }
        .frame(minWidth: 800, minHeight: 500)
        .background(sidebarShortcuts)
        .onReceive(NotificationCenter.default.publisher(for: .switchToActivity)) { _ in
            selectedItem = .activity
        }
        .onReceive(NotificationCenter.default.publisher(for: .switchToServers)) { _ in
            selectedItem = .servers
        }
    }

    /// Hidden ⌘1…⌘5 shortcuts to jump straight to each sidebar section. Keeps
    /// keyboard navigation fast for users and lets UI-test automation reach a
    /// section (the sidebar List rows aren't directly clickable via the
    /// accessibility menu API).
    @ViewBuilder
    private var sidebarShortcuts: some View {
        VStack {
            ForEach(Array(SidebarItem.allCases.enumerated()), id: \.element) { index, item in
                Button("") { selectedItem = item }
                    .keyboardShortcut(KeyEquivalent(Character(String(index + 1))), modifiers: .command)
                    .accessibilityIdentifier("sidebar-shortcut-\(item.rawValue)")
            }
        }
        .opacity(0)
        .frame(width: 0, height: 0)
        .accessibilityHidden(true)
    }

    // MARK: - Core Status Banner

    @ViewBuilder
    private var coreStatusBanner: some View {
        let isStopped = appState.isStopped
        let bannerColor: Color = isStopped ? .orange : .red
        let bannerIcon: String = isStopped ? "stop.circle.fill" : "exclamationmark.triangle.fill"
        let bannerText: String = {
            if isStopped { return "MCPProxy Core is stopped" }
            if case .idle = appState.coreState { return "MCPProxy Core is not running" }
            if case .error(let err) = appState.coreState { return "MCPProxy Core error: \(err.userMessage)" }
            return "MCPProxy Core: \(appState.coreState.displayName)"
        }()
        let fontScale = appState.fontScale

        HStack(spacing: 10) {
            Image(systemName: bannerIcon)
                .font(.scaled(.title3, scale: fontScale))
                .foregroundStyle(bannerColor)

            Text(bannerText)
                .font(.scaled(.subheadline, scale: fontScale).weight(.medium))

            Spacer()

            if isStopped {
                Button("Start") {
                    NotificationCenter.default.post(name: .startCore, object: nil)
                }
                .buttonStyle(.borderedProminent)
                .tint(.orange)
                .controlSize(.small)
            } else if appState.coreState == .idle || appState.coreState.canLaunch {
                Button("Start") {
                    NotificationCenter.default.post(name: .startCore, object: nil)
                }
                .buttonStyle(.borderedProminent)
                .controlSize(.small)
            }
        }
        .padding(.horizontal, 16)
        .padding(.vertical, 10)
        .background(bannerColor.opacity(0.15))
    }

}
