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) {
    final user = context.read<AuthCubit>().currentUser;

    return Scaffold(
      appBar: AppBar(title: const Text('Settings')),
      body: ListView(
        padding: AppSpacing.screenAll,
        children: [
          // ── Account ──
          _SectionHeader('Account'),
          AppSpacing.vSm,
          Container(
            padding: AppSpacing.cardInner,
            decoration: BoxDecoration(
              color: AppColors.panel,
              border: Border.all(color: AppColors.border),
              borderRadius: AppRadius.mdAll,
            ),
            child: Column(
              children: [
                _infoRow('Username', user?.username ?? '-'),
                const Divider(height: 20),
                _infoRow('Email', user?.email ?? '-'),
                const Divider(height: 20),
                _infoRow('Role', user?.role.toUpperCase() ?? 'USER'),
              ],
            ),
          ),
          AppSpacing.vLg,

          // ── Server ──
          _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(
              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)'),
                const Divider(height: 20),
                _infoRow('Data', 'yfinance + SerpAPI + ChromaDB'),
              ],
            ),
          ),
          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),
        Flexible(
          child: Text(value,
              style: AppTextStyles.labelMedium
                  .copyWith(color: AppColors.textSecondary),
              textAlign: TextAlign.end),
        ),
      ],
    );
  }
}

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,
      ),
    );
  }
}