import 'package:flutter/material.dart';
import '../../../../app/theme/app_colors.dart';
import '../../../../app/theme/app_typography.dart';
import '../../../../app/theme/app_spacing.dart';
import '../../../../shared/widgets/price_tag.dart';
import '../../../../shared/widgets/credit_badge.dart';
import '../../../../shared/widgets/genex_button.dart';

/// A2. 券详情页
///
/// 券图片、品牌Logo、面值、当前价格、折扣率、有效期、使用条件、
/// 使用门店、发行方信用等级、立即购买、加入收藏、价格走势图、同类券推荐
class CouponDetailPage extends StatelessWidget {
  const CouponDetailPage({super.key});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: CustomScrollView(
        slivers: [
          // Hero Image + AppBar
          SliverAppBar(
            expandedHeight: 220,
            pinned: true,
            backgroundColor: AppColors.surface,
            leading: _buildBackButton(context),
            actions: [
              IconButton(
                icon: const Icon(Icons.share_rounded, size: 22),
                onPressed: () {},
                style: IconButton.styleFrom(
                  backgroundColor: Colors.black26,
                  foregroundColor: Colors.white,
                ),
              ),
              const SizedBox(width: 8),
            ],
            flexibleSpace: FlexibleSpaceBar(
              background: Container(
                decoration: const BoxDecoration(gradient: AppColors.primaryGradient),
                child: const Center(
                  child: Icon(
                    Icons.confirmation_number_rounded,
                    size: 64,
                    color: Colors.white24,
                  ),
                ),
              ),
            ),
          ),

          SliverToBoxAdapter(
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                // Brand + Title + Rating
                Padding(
                  padding: const EdgeInsets.fromLTRB(20, 20, 20, 0),
                  child: Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      Row(
                        children: [
                          // Brand logo placeholder
                          Container(
                            width: 40,
                            height: 40,
                            decoration: BoxDecoration(
                              color: AppColors.gray100,
                              borderRadius: AppSpacing.borderRadiusSm,
                            ),
                            child: const Icon(Icons.store_rounded,
                                color: AppColors.textTertiary, size: 20),
                          ),
                          const SizedBox(width: 12),
                          Expanded(
                            child: Column(
                              crossAxisAlignment: CrossAxisAlignment.start,
                              children: [
                                Text('Starbucks', style: AppTypography.bodySmall),
                                Text('星巴克 \$25 礼品卡', style: AppTypography.h2),
                              ],
                            ),
                          ),
                          const CreditBadge(rating: 'AAA', size: CreditBadgeSize.large),
                        ],
                      ),
                    ],
                  ),
                ),

                // Price Section
                Padding(
                  padding: const EdgeInsets.fromLTRB(20, 16, 20, 0),
                  child: Container(
                    padding: const EdgeInsets.all(16),
                    decoration: BoxDecoration(
                      color: AppColors.primarySurface,
                      borderRadius: AppSpacing.borderRadiusMd,
                    ),
                    child: Column(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: [
                        const PriceTag(
                          currentPrice: 21.25,
                          faceValue: 25.0,
                          size: PriceTagSize.large,
                        ),
                        const SizedBox(height: 8),
                        Text(
                          '比面值节省 \$3.75',
                          style: AppTypography.bodySmall.copyWith(color: AppColors.success),
                        ),
                      ],
                    ),
                  ),
                ),

                // Info Cards
                Padding(
                  padding: const EdgeInsets.fromLTRB(20, 16, 20, 0),
                  child: _buildInfoSection(),
                ),

                // Usage Rules
                Padding(
                  padding: const EdgeInsets.fromLTRB(20, 16, 20, 0),
                  child: _buildUsageRules(),
                ),

                // Available Stores
                Padding(
                  padding: const EdgeInsets.fromLTRB(20, 16, 20, 0),
                  child: _buildStores(),
                ),

                // Price Trend (Optional)
                Padding(
                  padding: const EdgeInsets.fromLTRB(20, 16, 20, 0),
                  child: _buildPriceTrend(),
                ),

                // Similar Coupons
                Padding(
                  padding: const EdgeInsets.fromLTRB(20, 24, 20, 0),
                  child: Text('同类券推荐', style: AppTypography.h3),
                ),
                SizedBox(
                  height: 180,
                  child: ListView.separated(
                    scrollDirection: Axis.horizontal,
                    padding: const EdgeInsets.fromLTRB(20, 12, 20, 12),
                    itemCount: 5,
                    separatorBuilder: (_, __) => const SizedBox(width: 12),
                    itemBuilder: (context, index) => _buildSimilarCard(index),
                  ),
                ),

                const SizedBox(height: 120),
              ],
            ),
          ),
        ],
      ),

      // Bottom Buy Bar
      bottomNavigationBar: Container(
        padding: const EdgeInsets.fromLTRB(20, 12, 20, 24),
        decoration: const BoxDecoration(
          color: AppColors.surface,
          border: Border(top: BorderSide(color: AppColors.borderLight)),
        ),
        child: Row(
          children: [
            // Favorite
            Column(
              mainAxisSize: MainAxisSize.min,
              children: [
                const Icon(Icons.favorite_border_rounded, color: AppColors.textTertiary, size: 22),
                Text('收藏', style: AppTypography.caption),
              ],
            ),
            const SizedBox(width: 24),

            // Buy Button
            Expanded(
              child: GenexButton(
                label: '立即购买  \$21.25',
                onPressed: () {
                  Navigator.pushNamed(context, '/order/confirm');
                },
              ),
            ),
          ],
        ),
      ),
    );
  }

  Widget _buildBackButton(BuildContext context) {
    return Padding(
      padding: const EdgeInsets.all(8),
      child: IconButton(
        icon: const Icon(Icons.arrow_back_ios_new_rounded, size: 18),
        onPressed: () => Navigator.of(context).pop(),
        style: IconButton.styleFrom(
          backgroundColor: Colors.black26,
          foregroundColor: Colors.white,
        ),
      ),
    );
  }

  Widget _buildInfoSection() {
    final items = [
      ('面值', '\$25.00'),
      ('有效期', '2026/12/31'),
      ('类型', '消费券'),
      ('发行方', 'Starbucks Inc.'),
    ];

    return Container(
      padding: AppSpacing.cardPadding,
      decoration: BoxDecoration(
        color: AppColors.surface,
        borderRadius: AppSpacing.borderRadiusMd,
        border: Border.all(color: AppColors.borderLight),
      ),
      child: Column(
        children: items.asMap().entries.map((entry) {
          final isLast = entry.key == items.length - 1;
          return Column(
            children: [
              Row(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: [
                  Text(entry.value.$1, style: AppTypography.bodyMedium.copyWith(
                    color: AppColors.textSecondary,
                  )),
                  Text(entry.value.$2, style: AppTypography.labelMedium),
                ],
              ),
              if (!isLast) const Padding(
                padding: EdgeInsets.symmetric(vertical: 10),
                child: Divider(),
              ),
            ],
          );
        }).toList(),
      ),
    );
  }

  Widget _buildUsageRules() {
    return Container(
      padding: AppSpacing.cardPadding,
      decoration: BoxDecoration(
        color: AppColors.surface,
        borderRadius: AppSpacing.borderRadiusMd,
        border: Border.all(color: AppColors.borderLight),
      ),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Text('使用说明', style: AppTypography.labelMedium),
          const SizedBox(height: 12),
          _buildRuleItem(Icons.check_circle_outline, '全国星巴克门店通用'),
          _buildRuleItem(Icons.check_circle_outline, '可转赠给好友'),
          _buildRuleItem(Icons.check_circle_outline, '有效期内随时使用'),
          _buildRuleItem(Icons.info_outline_rounded, '不可叠加使用'),
          _buildRuleItem(Icons.info_outline_rounded, '不可兑换现金'),
        ],
      ),
    );
  }

  Widget _buildRuleItem(IconData icon, String text) {
    final isPositive = icon == Icons.check_circle_outline;
    return Padding(
      padding: const EdgeInsets.only(bottom: 8),
      child: Row(
        children: [
          Icon(icon, size: 16, color: isPositive ? AppColors.success : AppColors.textTertiary),
          const SizedBox(width: 8),
          Text(text, style: AppTypography.bodySmall),
        ],
      ),
    );
  }

  Widget _buildStores() {
    return Container(
      padding: AppSpacing.cardPadding,
      decoration: BoxDecoration(
        color: AppColors.surface,
        borderRadius: AppSpacing.borderRadiusMd,
        border: Border.all(color: AppColors.borderLight),
      ),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: [
              Text('使用门店', style: AppTypography.labelMedium),
              Text('全国 12,800+ 门店', style: AppTypography.caption.copyWith(
                color: AppColors.primary,
              )),
            ],
          ),
          const SizedBox(height: 12),
          Text(
            '支持全国所有星巴克直营门店使用',
            style: AppTypography.bodySmall,
          ),
        ],
      ),
    );
  }

  Widget _buildPriceTrend() {
    return Container(
      padding: AppSpacing.cardPadding,
      decoration: BoxDecoration(
        color: AppColors.surface,
        borderRadius: AppSpacing.borderRadiusMd,
        border: Border.all(color: AppColors.borderLight),
      ),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: [
              Text('价格走势', style: AppTypography.labelMedium),
              Text('近30天', style: AppTypography.caption),
            ],
          ),
          const SizedBox(height: 16),
          // Placeholder for price chart
          Container(
            height: 120,
            decoration: BoxDecoration(
              color: AppColors.gray50,
              borderRadius: AppSpacing.borderRadiusSm,
            ),
            child: Center(
              child: Text(
                '价格走势图 (fl_chart)',
                style: AppTypography.bodySmall.copyWith(color: AppColors.textTertiary),
              ),
            ),
          ),
          const SizedBox(height: 12),
          Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: [
              _buildTrendStat('最高', '\$22.50', AppColors.error),
              _buildTrendStat('最低', '\$20.00', AppColors.success),
              _buildTrendStat('均价', '\$21.10', AppColors.textSecondary),
              _buildTrendStat('历史成交', '1,234笔', AppColors.primary),
            ],
          ),
        ],
      ),
    );
  }

  Widget _buildTrendStat(String label, String value, Color color) {
    return Column(
      children: [
        Text(value, style: AppTypography.labelSmall.copyWith(color: color)),
        const SizedBox(height: 2),
        Text(label, style: AppTypography.caption),
      ],
    );
  }

  Widget _buildSimilarCard(int index) {
    return Container(
      width: 130,
      decoration: BoxDecoration(
        color: AppColors.surface,
        borderRadius: AppSpacing.borderRadiusMd,
        border: Border.all(color: AppColors.borderLight),
      ),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Container(
            height: 80,
            decoration: BoxDecoration(
              color: AppColors.primarySurface,
              borderRadius: const BorderRadius.vertical(top: Radius.circular(12)),
            ),
            child: Center(
              child: Icon(Icons.confirmation_number_outlined,
                  color: AppColors.primary.withValues(alpha: 0.3), size: 28),
            ),
          ),
          Padding(
            padding: const EdgeInsets.all(8),
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Text('品牌 ${index + 1}', style: AppTypography.caption),
                const SizedBox(height: 2),
                Text('\$${(index + 1) * 8}.50',
                    style: AppTypography.priceSmall.copyWith(fontSize: 14)),
                Text('\$${(index + 1) * 10}', style: AppTypography.priceOriginal.copyWith(fontSize: 10)),
              ],
            ),
          ),
        ],
      ),
    );
  }
}
