import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';

import '../core/config/app_config.dart';
import '../core/theme/app_colors.dart';
import '../core/theme/app_text_styles.dart';
import '../core/theme/app_spacing.dart';
import '../core/theme/app_radius.dart';
import '../core/widgets/fs_button.dart';
import '../features/auth/presentation/bloc/auth_cubit.dart';

class SettingsScreen extends StatelessWidget {
  const SettingsScreen({super.key});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: const Text('Settings')),
      body: ListView(
        padding: AppSpacing.screenAll,
        children: [
          // ── Server Info ──
          _SectionHeader('Server Configuration'),
          AppSpacing.vSm,
          Container(
            padding: AppSpacing.cardInner,
            decoration: BoxDecoration(
              color: AppColors.panel,
              border: Border.all(color: AppColors.border),
              borderRadius: AppRadius.mdAll,
            ),
            child: Row(
              children: [
                const Icon(Icons.dns_outlined,
                    color: AppColors.textTertiary, size: 20),
                const SizedBox(width: 12),
                Expanded(
                  child: Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      Text('Base URL', style: AppTextStyles.labelMedium),
                      Text(
                        AppConfig.baseUrl,
                        style: AppTextStyles.monoSmall
                            .copyWith(color: AppColors.accentLight),
                      ),
                    ],
                  ),
                ),
              ],
            ),
          ),
          AppSpacing.vLg,

          // ── About ──
          _SectionHeader('About'),
          AppSpacing.vSm,
          Container(
            padding: AppSpacing.cardInner,
            decoration: BoxDecoration(
              color: AppColors.panel,
              border: Border.all(color: AppColors.border),
              borderRadius: AppRadius.mdAll,
            ),
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                _infoRow('App', '${AppConfig.appName} v${AppConfig.appVersion}'),
                const Divider(height: 20),
                _infoRow('Platform', 'Flutter + FastAPI + LangGraph'),
                const Divider(height: 20),
                _infoRow('AI Model', 'LLaMA 3.3 70B (Groq)'),
              ],
            ),
          ),
          AppSpacing.vXl,

          // ── Logout ──
          FSButton(
            label: 'Sign Out',
            variant: FSButtonVariant.danger,
            icon: Icons.logout,
            onPressed: () => context.read<AuthCubit>().logout(),
          ),
          AppSpacing.vXl,
        ],
      ),
    );
  }

  Widget _infoRow(String label, String value) {
    return Row(
      mainAxisAlignment: MainAxisAlignment.spaceBetween,
      children: [
        Text(label, style: AppTextStyles.bodySmall),
        Text(value,
            style: AppTextStyles.labelMedium
                .copyWith(color: AppColors.textSecondary)),
      ],
    );
  }
}

class _SectionHeader extends StatelessWidget {
  final String title;
  const _SectionHeader(this.title);

  @override
  Widget build(BuildContext context) {
    return Text(
      title.toUpperCase(),
      style: AppTextStyles.labelSmall.copyWith(
        color: AppColors.textTertiary,
        letterSpacing: 1.5,
      ),
    );
  }
}