import 'package:flutter/material.dart';
import '../core/chart_engine.dart';
import '../core/key_analyzer.dart';
import '../core/liuqin.dart';
import '../core/report_generator.dart';
import '../data/tutorial_cases.dart';
import '../l10n/l10n.dart';
import 'report_screen.dart';
import 'theme.dart';
class CasesScreen extends StatelessWidget {
  const CasesScreen({super.key});

  @override
  Widget build(BuildContext context) {
    final l = LangScope.of(context).l10n;
    return Scaffold(
      appBar: AppBar(title: Text(l('cases'))),
      body: ListView.separated(
        padding: const EdgeInsets.all(20),
        itemCount: TutorialCases.all.length,
        separatorBuilder: (_, __) => const SizedBox(height: 12),
        itemBuilder: (context, i) {
          final c = TutorialCases.all[i];
          final title = switch (l.lang) {
            AppLang.zh => c.titleZh,
            AppLang.ja => c.titleJa,
            AppLang.en => c.titleEn,
          };
          final parts = c.expectedPillars.split(' ');
          final chart = ChartEngine.fromPillars(
            year: parts[0],
            month: parts[1],
            day: parts[2],
            hour: parts[3],
            input: c.input,
          );
          final engine = ChartEngine.build(c.input);
          final match = engine.fourPillarsText == c.expectedPillars;
          final keys = KeyAnalyzer.identify(chart);
          final liuQin = LiuQinLocator.locate(chart);

          return Container(
            padding: const EdgeInsets.all(16),
            decoration: BoxDecoration(
              color: Colors.white.withValues(alpha: 0.55),
              border: Border.all(color: AppTheme.mist),
            ),
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Text(title,
                    style: const TextStyle(
                        fontSize: 16, fontWeight: FontWeight.w700)),
                const SizedBox(height: 6),
                Text('标准八字：${c.expectedPillars}',
                    style: const TextStyle(fontFamily: 'monospace')),
                const SizedBox(height: 4),
                Text(
                  match
                      ? '✓ 排盘引擎回验通过'
                      : '✗ 引擎输出：${engine.fourPillarsText}',
                  style: TextStyle(
                    color: match ? AppTheme.jade : AppTheme.accent,
                    fontWeight: FontWeight.w600,
                  ),
                ),
                const SizedBox(height: 8),
                Text('键数：${keys.length} · 六亲条目：${liuQin.where((e) => e.layer.contains("第一")).length}',
                    style: TextStyle(
                        color: AppTheme.ink.withValues(alpha: 0.65))),
                ...c.notesZh.map((n) => Padding(
                      padding: const EdgeInsets.only(top: 4),
                      child: Text('· $n',
                          style: const TextStyle(fontSize: 13, height: 1.35)),
                    )),
                const SizedBox(height: 10),
                Align(
                  alignment: Alignment.centerRight,
                  child: TextButton(
                    onPressed: () {
                      final report =
                          ReportGenerator.generateFromChart(chart);
                      Navigator.of(context).push(
                        MaterialPageRoute(
                          builder: (_) => ReportScreen(report: report),
                        ),
                      );
                    },
                    child: Text(l('report')),
                  ),
                ),
              ],
            ),
          );
        },
      ),
    );
  }
}
