import SwiftUI

/// Fetches available themes from the server and lets the user import + apply them.
struct ThemeImportView: View {
    @Environment(\.apiClient) private var apiClient
    @Environment(ThemeStore.self) private var themeStore

    @State private var remoteThemes: [RemoteThemeSummary] = []
    @State private var isLoading = true
    @State private var error: String?
    @State private var importingName: String?

    var body: some View {
        Group {
            if isLoading {
                ProgressView("Fetching themes…")
                    .frame(maxWidth: .infinity, maxHeight: .infinity)
            } else if let error {
                ContentUnavailableView(
                    "Unable to Load Themes",
                    systemImage: "exclamationmark.triangle",
                    description: Text(error)
                )
            } else if remoteThemes.isEmpty {
                ContentUnavailableView(
                    "No Custom Themes",
                    systemImage: "paintbrush",
                    description: Text("Ask the agent to create a theme, then try again.")
                )
            } else {
                themeList
            }
        }
        .themedListSurface()
        .navigationTitle("Import Theme")
        .navigationBarTitleDisplayMode(.inline)
        .task { await loadThemes() }
    }

    private var themeList: some View {
        List {
            ForEach(remoteThemes) { summary in
                HStack {
                    VStack(alignment: .leading, spacing: 2) {
                        Text(summary.name)
                            .font(.body.weight(.medium))
                        Text(summary.colorScheme)
                            .font(.caption)
                            .foregroundStyle(.themeComment)
                    }

                    Spacer()

                    if importingName == summary.filename {
                        ProgressView()
                            .controlSize(.small)
                    } else if CustomThemeStore.load(name: summary.name) != nil {
                        Image(systemName: "checkmark.circle.fill")
                            .foregroundStyle(.themeGreen)
                    }
                }
                .contentShape(Rectangle())
                .onTapGesture {
                    Task { await importTheme(summary) }
                }
            }
        }
    }

    private func loadThemes() async {
        guard let api = apiClient else {
            error = "Not connected"
            isLoading = false
            return
        }
        do {
            remoteThemes = try await api.listThemes()
            isLoading = false
        } catch {
            self.error = error.localizedDescription
            isLoading = false
        }
    }

    private func importTheme(_ summary: RemoteThemeSummary) async {
        guard let api = apiClient else { return }
        importingName = summary.filename
        do {
            let theme = try await api.getTheme(name: summary.filename)
            guard theme.toPalette() != nil else {
                importingName = nil
                return
            }
            CustomThemeStore.save(theme)
            themeStore.selectedThemeID = .custom(theme.name)
            AppHaptics.success()
            importingName = nil
        } catch {
            importingName = nil
        }
    }
}
