import SwiftUI
import SharedModels
import SwiftData

struct ChatLibraryView: View {
    @Environment(\.cloudModelContextProvider) private var cloudModelContextProvider
    @State private var conversations: [Conversation] = []
    @State private var showingScenarioPicker = false

    private var cloudContext: ModelContext { cloudModelContextProvider() }

    var body: some View {
        Group {
            if conversations.isEmpty {
                ContentUnavailableView(
                    "No Conversations Yet",
                    systemImage: "bubble.left.and.bubble.right",
                    description: Text("Tap + to start a Spanish conversation.")
                )
            } else {
                List {
                    ForEach(conversations) { conv in
                        NavigationLink {
                            ChatView(conversation: conv)
                        } label: {
                            VStack(alignment: .leading, spacing: 4) {
                                Text(conv.scenario)
                                    .font(.subheadline.weight(.semibold))
                                HStack(spacing: 8) {
                                    Text(conv.level.capitalized)
                                        .font(.caption2.weight(.medium))
                                        .foregroundStyle(.green)
                                        .padding(.horizontal, 6)
                                        .padding(.vertical, 2)
                                        .background(.green.opacity(0.12), in: Capsule())
                                    let msgCount = conv.decodedMessages.count
                                    Text("\(msgCount) message\(msgCount == 1 ? "" : "s")")
                                        .font(.caption)
                                        .foregroundStyle(.secondary)
                                }
                            }
                            .padding(.vertical, 2)
                        }
                    }
                    .onDelete(perform: deleteConversations)
                }
            }
        }
        .navigationTitle("Conversations")
        .navigationBarTitleDisplayMode(.large)
        .toolbar {
            ToolbarItem(placement: .topBarTrailing) {
                Button {
                    showingScenarioPicker = true
                } label: {
                    Image(systemName: "plus")
                }
                .disabled(!ConversationService.isAvailable)
            }
        }
        .sheet(isPresented: $showingScenarioPicker) {
            ScenarioPickerView { scenario in
                showingScenarioPicker = false
                createConversation(scenario: scenario)
            }
        }
        .onAppear(perform: loadConversations)
    }

    private func loadConversations() {
        let descriptor = FetchDescriptor<Conversation>(
            sortBy: [SortDescriptor(\Conversation.createdDate, order: .reverse)]
        )
        conversations = (try? cloudContext.fetch(descriptor)) ?? []
    }

    private func createConversation(scenario: String) {
        let progress = ReviewStore.fetchOrCreateUserProgress(context: cloudContext)
        let conv = Conversation(scenario: scenario, level: progress.selectedLevel)
        cloudContext.insert(conv)
        try? cloudContext.save()
        loadConversations()
    }

    private func deleteConversations(at offsets: IndexSet) {
        for index in offsets { cloudContext.delete(conversations[index]) }
        try? cloudContext.save()
        loadConversations()
    }
}

// MARK: - Scenario Picker

struct ScenarioPickerView: View {
    let onPick: (String) -> Void
    @Environment(\.dismiss) private var dismiss

    var body: some View {
        NavigationStack {
            List {
                ForEach(ConversationService.scenarios, id: \.self) { scenario in
                    Button {
                        onPick(scenario)
                    } label: {
                        HStack {
                            Text(scenario)
                                .font(.body)
                            Spacer()
                            Image(systemName: "chevron.right")
                                .font(.caption)
                                .foregroundStyle(.tertiary)
                        }
                    }
                    .tint(.primary)
                }
            }
            .navigationTitle("Choose a Scenario")
            .navigationBarTitleDisplayMode(.inline)
            .toolbar {
                ToolbarItem(placement: .cancellationAction) {
                    Button("Cancel") { dismiss() }
                }
            }
        }
    }
}
