import 'package:flutter/material.dart';
import 'package:go_router/go_router.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/theme/app_gradients.dart';
import '../../../../core/widgets/fs_button.dart';
import '../../data/datasources/generator_remote_datasource.dart';

class GeneratorResultsScreen extends StatelessWidget {
  final GenerateResultModel result;

  const GeneratorResultsScreen({super.key, required this.result});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: const Text('Generation Complete')),
      body: ListView(
        padding: AppSpacing.screenAll,
        children: [
          // Success icon
          Center(
            child: Container(
              width: 80,
              height: 80,
              decoration: BoxDecoration(
                color: AppColors.successBg,
                shape: BoxShape.circle,
                border: Border.all(
                    color: AppColors.success.withOpacity(0.3), width: 2),
              ),
              child: const Center(
                child: Text('✅', style: TextStyle(fontSize: 36)),
              ),
            ),
          ),
          AppSpacing.vLg,

          Center(
            child: ShaderMask(
              shaderCallback: (b) =>
                  AppGradients.scoreGood.createShader(b),
              child: Text(
                'Scenarios Generated!',
                style: AppTextStyles.headlineLarge
                    .copyWith(color: Colors.white),
              ),
            ),
          ),
          AppSpacing.vXl,

          // Stats
          ..._buildStatRow('📈', 'Symbol', result.stockSymbol),
          ..._buildStatRow('🔍', 'Events Analyzed',
              result.eventsAnalyzed.toString()),
          ..._buildStatRow('🧠', 'Scenarios Generated',
              result.scenariosGenerated.toString()),
          ..._buildStatRow('💾', 'Saved to Database',
              result.scenariosSavedToDb.toString()),

          if (result.message != null) ...[
            AppSpacing.vMd,
            Container(
              padding: AppSpacing.cardInner,
              decoration: BoxDecoration(
                color: AppColors.infoBg,
                borderRadius: AppRadius.smAll,
              ),
              child: Text(result.message!,
                  style: AppTextStyles.bodySmall
                      .copyWith(color: AppColors.infoLight)),
            ),
          ],

          AppSpacing.vXl,

          if (result.hasScenarios) ...[
            FSButton(
              label: '📚 Browse Scenarios',
              onPressed: () => context.go('/app/more/scenarios'),
              variant: FSButtonVariant.gradient,
            ),
            AppSpacing.vSm,
            FSButton(
              label: '📝 Start Quiz',
              onPressed: () => context.go('/app/quiz'),
              variant: FSButtonVariant.secondary,
            ),
          ],
          AppSpacing.vSm,
          FSButton(
            label: '⚡ Generate More',
            onPressed: () => context.pop(),
            variant: FSButtonVariant.ghost,
          ),
        ],
      ),
    );
  }

  List<Widget> _buildStatRow(String icon, String label, String value) {
    return [
      Container(
        margin: const EdgeInsets.only(bottom: 10),
        padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 14),
        decoration: BoxDecoration(
          color: AppColors.panel,
          border: Border.all(color: AppColors.border),
          borderRadius: AppRadius.smAll,
        ),
        child: Row(
          children: [
            Text(icon, style: const TextStyle(fontSize: 20)),
            const SizedBox(width: 12),
            Expanded(
              child: Text(label, style: AppTextStyles.bodyMedium),
            ),
            Text(
              value,
              style: AppTextStyles.titleMedium
                  .copyWith(color: AppColors.accentLight),
            ),
          ],
        ),
      ),
    ];
  }
}