content: import 'package:flutter/material.dart';
  import 'package:flutter_riverpod/flutter_riverpod.dart';
  import 'package:slash_flutter/features/prompt/prompt_widgets.dart';
  import 'package:slash_flutter/ui/components/option_selection.dart';
  import 'package:slash_flutter/ui/components/slash_dropdown.dart';
  import 'package:slash_flutter/ui/components/slash_text.dart';
  import 'package:slash_flutter/ui/components/slash_toast.dart';
  import 'package:slash_flutter/ui/theme/app_theme_builder.dart';
  import '../../ui/components/slash_text_field.dart';
  import '../../ui/components/slash_button.dart';
  import '../repo/repo_controller.dart';
  import '../file_browser/file_browser_controller.dart';
  import 'prompt_controller.dart';
  import '../auth/auth_controller.dart';
  
  class PromptPage extends ConsumerStatefulWidget {
    const PromptPage({super.key});
  
    @override
    ConsumerState<PromptPage> createState() => _PromptPageState();
  }
  
  class _PromptPageState extends ConsumerState<PromptPage> {
    late TextEditingController _promptTextController;
    final TextEditingController _searchController = TextEditingController();
  
    // Chat scroll behavior
    final ScrollController _chatController = ScrollController();
    bool _userScrolling = false;
  
    void _autoScrollToBottom() {
      if (!_chatController.hasClients) return;
      // Only auto-scroll if user is already near the bottom to avoid hijacking when reading history.
      final pos = _chatController.position;
      final nearBottom = (pos.maxScrollExtent - pos.pixels) <= 80; // threshold
      if (!nearBottom) return;
  
      WidgetsBinding.instance.addPostFrameCallback((_) {
        if (!_chatController.hasClients) return;
        _chatController.animateTo(
          _chatController.position.maxScrollExtent,
          duration: const Duration(milliseconds: 180),
          curve: Curves.easeOut,
        );
      });
    }
  
    @override
    void initState() {
      super.initState();
      _promptTextController = TextEditingController();
      WidgetsBinding.instance.addPostFrameCallback((_) {
        _autoScrollToBottom();
      });
    }
  
    @override
    void dispose() {
      _promptTextController.dispose();
      _searchController.dispose();
      _chatController.dispose();
      super.dispose();
    }
  
    Future<void> _handlePromptSubmit() async {
      final prompt = _promptTextController.text.trim();
      if (prompt.isEmpty) return;
  
      // Agentic: allow send for follow-ups without forcing repo/context here.
      _promptTextController.clear();
      await ref.read(promptControllerProvider.notifier).submitPrompt(prompt);
      // Only snap if user is already near bottom (handled inside).
      _autoScrollToBottom();
    }
  
    void _showFilePickerModal(BuildContext context) async {
      final repoState = ref.read(repoControllerProvider);
      final promptState = ref.read(promptControllerProvider);
      final repo = promptState.selectedRepo ?? repoState.selectedRepo;
  
      if (repo == null) {
        SlashToast.showError(context, 'Please select a repository first.');
        return;
      }
  
      final owner = repo['owner']['login'];
      final repoName = repo['name'];
      final params = RepoParams(owner: owner, repo: repoName);
  
      showModalBottomSheet(
        context: context,
        isScrollControlled: true,
        builder: (ctx) {
          return LazyFilePickerModal(
            params: params,
            initiallySelected: promptState.repoContextFiles,
            onSelected: (List<FileItem> selected) {
              ref
                  .read(promptControllerProvider.notifier)
                  .setRepoContextFiles(selected);
            },
          );
        },
      );
    }
  
    @override
    Widget build(BuildContext context) {
      final repoState = ref.watch(repoControllerProvider);
      final promptState = ref.watch(promptControllerProvider);
      final repoController = ref.read(repoControllerProvider.notifier);
      final promptController = ref.read(promptControllerProvider.notifier);
  
      final repos = repoState.repos;
      final selectedRepo =
          promptState.selectedRepo ??
          repoState.selectedRepo ??
          (repos.isNotEmpty ? repos[0] : null);
  
      if (repoState.isLoading) {
        return const Center(child: CircularProgressIndicator());
      }
  
      if (repos.isEmpty) {
        return const Center(child: SlashText('No repositories found.'));
      }
  
      return ThemeBuilder(
        builder: (context, colors, ref) {
          return Scaffold(
            appBar: AppBar(
              backgroundColor: colors.always8B5CF6.withValues(alpha: 0.1),
              title: Image.asset('assets/slash2.png', height: 100),
              centerTitle: false,
              toolbarHeight: 80,
              actions: [
                Container(
                  margin: const EdgeInsets.symmetric(horizontal: 16),
                  decoration: BoxDecoration(
                    color: colors.alwaysEDEDED.withValues(alpha: 0.1),
                    borderRadius: BorderRadius.circular(20),
                  ),
                  child: OptionSelection(
                    options: ["Gemini", "OpenRouter"],
                    margin: 0,
                    padding: 8,
                    unselectedColor: Colors.transparent,
                    // Force default OpenRouter on first mount if nothing persisted yet:
                    selectedValue: (promptState.selectedModel.isEmpty
                        ? 'openrouter'
                        : promptState.selectedModel),
                    onChanged: (val) {
                      // Persist into auth so next reload uses the saved choice
                      promptController.setSelectedModel(val);
                      // Mirror to auth storage to survive reloads
                      ref.read(authControllerProvider.notifier).saveModel(val.toLowerCase());
                    },
                  ),
                ),
              ],
            ),
            body: SafeArea(
              child: Column(
                children: [
                  // Repository and branch selection
                  Padding(
                    padding: const EdgeInsets.symmetric(
                      horizontal: 16,
                      vertical: 8,
                    ),
                    child: Row(
                      children: [
                        Expanded(
                          child: SlashDropDown(
                            hintText: 'Select Repository',
                            items:
                                repos.map<DropdownMenuItem<dynamic>>((repo) {
                                  return DropdownMenuItem<dynamic>(
                                    value: repo,
                                    child: SlashText(
                                      repo['full_name'] ?? repo['name'],
                                      fontSize: 12,
                                    ),
                                  );
                                }).toList(),
                            onChanged: (repo) {
                              promptController.setSelectedRepo(repo);
                              repoController.selectRepo(repo);
                            },
                          ),
                        ),
                        const SizedBox(width: 12),
                        if (promptState.branches.isNotEmpty)
                          SlashDropDown(
                            width: 80,
                            color: colors.always8B5CF6,
                            value: promptState.selectedBranch,
                            items:
                                promptState.branches
                                    .map<DropdownMenuItem<String>>((branch) {
                                      return DropdownMenuItem<String>(
                                        value: branch,
                                        child: SlashText(
                                          branch,
                                          fontSize: 10,
                                          fontWeight: FontWeight.bold,
                                        ),
                                      );
                                    })
                                    .toList(),
                            onChanged: (branch) {
                              promptController.setSelectedBranch(branch);
                            },
                          ),
                      ],
                    ),
                  ),
  
                  const SizedBox(height: 10),
  
                  const Divider(height: 0.5),
  
                  // Chat messages: always keep view pinned to the latest message
                  Expanded(
                    child: NotificationListener<ScrollNotification>(
                      onNotification: (_) {
                        // Let user scroll freely; we only snap if already near bottom (handled in _autoScrollToBottom).
                        return false;
                      },
                      child: NotificationListener<SizeChangedLayoutNotification>(
                        onNotification: (_) {
                          _autoScrollToBottom();
                          return false;
                        },
                        child: SizeChangedLayoutNotifier(
                          child: ListView.builder(
                            controller: _chatController,
                            padding: const EdgeInsets.all(16),
                            itemCount: promptState.messages.length,
                            itemBuilder: (context, idx) {
                              final msg = promptState.messages[idx];
                              final isLast = idx == promptState.messages.length - 1;
  
                              if (msg.review != null) {
                                if (isLast) {
                                  WidgetsBinding.instance.addPostFrameCallback((_) {
                                    _autoScrollToBottom();
                                  });
                                }
                                return ReviewBubble(
                                  review: msg.review!,
                                  summary: msg.text,
                                  isLast: isLast,
                                );
                              }
  
                              final isLastAgent =
                                  !msg.isUser &&
                                  idx ==
                                      promptState.messages.lastIndexWhere(
                                        (m) => !m.isUser,
                                      );
  
                              if (isLast) {
                                WidgetsBinding.instance.addPostFrameCallback((_) {
                                  _autoScrollToBottom();
                                });
                              }
  
                              return Column(
                                crossAxisAlignment: CrossAxisAlignment.stretch,
                                children: [
                                  if (isLastAgent)
                                    IntentTag(intent: promptState.lastIntent),
                                  ChatMessageBubble(message: msg),
                                ],
                              );
                            },
                          ),
                        ),
                      ),
                    ),
                  ),
  
                  // Error message
                  if (promptState.error != null)
                    Padding(
                      padding: const EdgeInsets.all(12),
                      child: SlashText(promptState.error!, color: Colors.red),
                    ),
  
                  // Input field and send button
                  Padding(
                    padding: const EdgeInsets.all(16),
                    child: Row(
                      crossAxisAlignment: CrossAxisAlignment.end,
                      children: [
                        slashIconButton(
                          asset: 'assets/icons/attach.svg',
                          hasContainer: false,
                          color: colors.always909090.withValues(alpha: 0.2),
                          onPressed:
                              promptState.isLoading
                                  ? () {}
                                  : () => _showFilePickerModal(context),
                        ),
                        const SizedBox(width: 12),
                        Expanded(
                          child: SlashTextField(
                            controller: _promptTextController,
                            hint: 'Type a prompt…',
                            minLines: 4,
                            maxLines: 8,
                            suffix: Container(
                              margin: const EdgeInsets.only(bottom: 5.0),
                              child: slashIconButton(
                                icon: Icons.arrow_upward,
                                onPressed:
                                    promptState.isLoading
                                        ? () {}
                                        : _handlePromptSubmit,
                              ),
                            ),
                          ),
                        ),
                        SizedBox(width: 12),
                      ],
                    ),
                  ),
  
                  // Repo context files display
                  if (promptState.repoContextFiles.isNotEmpty)
                    ContextFilesDisplay(
                      contextFiles: promptState.repoContextFiles,
                      onRemoveFile:
                          (file) => promptController.removeContextFile(file),
                    ),
                ],
              ),
            ),
          );
        },
      );
    }
  }
  