import 'package:flutter/material.dart';
import '../../../../app/theme/app_colors.dart';

/// 信用评级页面
///
/// 四因子信用评分：核销率35% + (1-Breakage率)25% + 市场存续20% + 用户满意度20%
/// AI建议列表：信用提升建议
class CreditPage extends StatelessWidget {
  const CreditPage({super.key});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: const Text('信用评级')),
      body: SingleChildScrollView(
        padding: const EdgeInsets.all(20),
        child: Column(
          children: [
            // Score Gauge
            _buildScoreGauge(),
            const SizedBox(height: 24),

            // Four Factors
            _buildFactorsCard(),
            const SizedBox(height: 20),

            // Tier Progress
            _buildTierProgress(),
            const SizedBox(height: 20),

            // AI Suggestions
            _buildAiSuggestions(),
            const SizedBox(height: 20),

            // Credit History
            _buildCreditHistory(),
          ],
        ),
      ),
    );
  }

  Widget _buildScoreGauge() {
    return Container(
      padding: const EdgeInsets.all(24),
      decoration: BoxDecoration(
        color: AppColors.surface,
        borderRadius: BorderRadius.circular(16),
        border: Border.all(color: AppColors.borderLight),
      ),
      child: Column(
        children: [
          Container(
            width: 120,
            height: 120,
            decoration: BoxDecoration(
              shape: BoxShape.circle,
              gradient: LinearGradient(
                begin: Alignment.topCenter,
                end: Alignment.bottomCenter,
                colors: [AppColors.creditAA, AppColors.creditAA.withValues(alpha: 0.3)],
              ),
            ),
            child: const Center(
              child: Column(
                mainAxisAlignment: MainAxisAlignment.center,
                children: [
                  Text('AA', style: TextStyle(fontSize: 32, fontWeight: FontWeight.w700, color: Colors.white)),
                  Text('82分', style: TextStyle(fontSize: 14, color: Colors.white70)),
                ],
              ),
            ),
          ),
          const SizedBox(height: 16),
          const Text('信用等级 AA', style: TextStyle(fontSize: 18, fontWeight: FontWeight.w600)),
          const SizedBox(height: 4),
          const Text('距离 AAA 等级还差 8 分', style: TextStyle(fontSize: 13, color: AppColors.textSecondary)),
        ],
      ),
    );
  }

  Widget _buildFactorsCard() {
    final factors = [
      ('核销率', 0.85, 0.35, AppColors.success),
      ('沉淀控制', 0.72, 0.25, AppColors.info),
      ('市场存续', 0.90, 0.20, AppColors.primary),
      ('用户满意度', 0.78, 0.20, AppColors.warning),
    ];

    return Container(
      padding: const EdgeInsets.all(16),
      decoration: BoxDecoration(
        color: AppColors.surface,
        borderRadius: BorderRadius.circular(12),
        border: Border.all(color: AppColors.borderLight),
      ),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          const Text('评分因子', style: TextStyle(fontSize: 15, fontWeight: FontWeight.w600)),
          const SizedBox(height: 16),
          ...factors.map((f) {
            final (label, score, weight, color) = f;
            return Padding(
              padding: const EdgeInsets.only(bottom: 16),
              child: Column(
                children: [
                  Row(
                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                    children: [
                      Text(label, style: const TextStyle(fontSize: 13)),
                      Text(
                        '${(score * 100).toInt()}分 (权重${(weight * 100).toInt()}%)',
                        style: const TextStyle(fontSize: 12, color: AppColors.textSecondary),
                      ),
                    ],
                  ),
                  const SizedBox(height: 6),
                  ClipRRect(
                    borderRadius: BorderRadius.circular(4),
                    child: LinearProgressIndicator(
                      value: score,
                      backgroundColor: AppColors.gray100,
                      valueColor: AlwaysStoppedAnimation(color),
                      minHeight: 8,
                    ),
                  ),
                ],
              ),
            );
          }),
        ],
      ),
    );
  }

  Widget _buildTierProgress() {
    final tiers = [
      ('白银', AppColors.tierSilver, true),
      ('黄金', AppColors.tierGold, true),
      ('铂金', AppColors.tierPlatinum, false),
      ('钻石', AppColors.tierDiamond, false),
    ];

    return Container(
      padding: const EdgeInsets.all(16),
      decoration: BoxDecoration(
        color: AppColors.surface,
        borderRadius: BorderRadius.circular(12),
        border: Border.all(color: AppColors.borderLight),
      ),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          const Text('发行方层级', style: TextStyle(fontSize: 15, fontWeight: FontWeight.w600)),
          const SizedBox(height: 16),
          Row(
            mainAxisAlignment: MainAxisAlignment.spaceAround,
            children: tiers.map((t) {
              final (name, color, isReached) = t;
              return Column(
                children: [
                  Container(
                    width: 44,
                    height: 44,
                    decoration: BoxDecoration(
                      color: isReached ? color.withValues(alpha: 0.15) : AppColors.gray100,
                      shape: BoxShape.circle,
                      border: isReached ? Border.all(color: color, width: 2) : null,
                    ),
                    child: Icon(
                      Icons.star_rounded,
                      color: isReached ? color : AppColors.textTertiary,
                      size: 22,
                    ),
                  ),
                  const SizedBox(height: 6),
                  Text(
                    name,
                    style: TextStyle(
                      fontSize: 12,
                      color: isReached ? color : AppColors.textTertiary,
                      fontWeight: isReached ? FontWeight.w600 : FontWeight.w400,
                    ),
                  ),
                ],
              );
            }).toList(),
          ),
          const SizedBox(height: 12),
          const Text(
            '当前：黄金 → 铂金需月发行量达500万',
            style: TextStyle(fontSize: 12, color: AppColors.textSecondary),
          ),
        ],
      ),
    );
  }

  Widget _buildAiSuggestions() {
    final suggestions = [
      ('提升核销率', '建议在周末推出限时核销活动，预计可提升核销率5%', Icons.trending_up_rounded),
      ('降低Breakage', '当前有12%的券过期未用，建议到期前7天推送提醒', Icons.notification_important_rounded),
      ('增加用户满意度', '回复消费者评价可提升满意度评分', Icons.rate_review_rounded),
    ];

    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        const Row(
          children: [
            Icon(Icons.auto_awesome_rounded, color: AppColors.primary, size: 20),
            SizedBox(width: 8),
            Text('AI 信用提升建议', style: TextStyle(fontSize: 15, fontWeight: FontWeight.w600)),
          ],
        ),
        const SizedBox(height: 12),
        ...suggestions.map((s) {
          final (title, desc, icon) = s;
          return Container(
            margin: const EdgeInsets.only(bottom: 10),
            padding: const EdgeInsets.all(14),
            decoration: BoxDecoration(
              color: AppColors.primarySurface,
              borderRadius: BorderRadius.circular(10),
            ),
            child: Row(
              children: [
                Icon(icon, color: AppColors.primary, size: 20),
                const SizedBox(width: 12),
                Expanded(
                  child: Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      Text(title, style: const TextStyle(fontSize: 13, fontWeight: FontWeight.w600)),
                      const SizedBox(height: 2),
                      Text(desc, style: const TextStyle(fontSize: 12, color: AppColors.textSecondary)),
                    ],
                  ),
                ),
              ],
            ),
          );
        }),
      ],
    );
  }

  Widget _buildCreditHistory() {
    return Container(
      padding: const EdgeInsets.all(16),
      decoration: BoxDecoration(
        color: AppColors.surface,
        borderRadius: BorderRadius.circular(12),
        border: Border.all(color: AppColors.borderLight),
      ),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          const Text('信用变动记录', style: TextStyle(fontSize: 15, fontWeight: FontWeight.w600)),
          const SizedBox(height: 12),
          _buildHistoryItem('信用分 +3', '核销率提升至85%', '2天前', AppColors.success),
          _buildHistoryItem('信用分 -1', 'Breakage率微升', '1周前', AppColors.error),
          _buildHistoryItem('升级至黄金', '月发行量达100万', '2周前', AppColors.tierGold),
          _buildHistoryItem('信用分 +5', '完成首月营业', '1月前', AppColors.success),
        ],
      ),
    );
  }

  Widget _buildHistoryItem(String title, String desc, String time, Color color) {
    return Padding(
      padding: const EdgeInsets.symmetric(vertical: 8),
      child: Row(
        children: [
          Container(width: 8, height: 8, decoration: BoxDecoration(color: color, shape: BoxShape.circle)),
          const SizedBox(width: 12),
          Expanded(
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Text(title, style: TextStyle(fontSize: 13, fontWeight: FontWeight.w600, color: color)),
                Text(desc, style: const TextStyle(fontSize: 12, color: AppColors.textSecondary)),
              ],
            ),
          ),
          Text(time, style: const TextStyle(fontSize: 11, color: AppColors.textTertiary)),
        ],
      ),
    );
  }
}
