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/coupon_card.dart';
import '../../../ai_agent/presentation/widgets/ai_fab.dart';

/// A2. 首页 - 搜索栏 + Banner + 热门分类 + 精选券 + AI Agent
///
/// Tab导航：首页/市场/我的券/消息/我的
class HomePage extends StatelessWidget {
  const HomePage({super.key});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Stack(
        children: [
          CustomScrollView(
            slivers: [
              // Floating App Bar
              SliverAppBar(
                floating: true,
                pinned: false,
                backgroundColor: AppColors.background,
                elevation: 0,
                toolbarHeight: 60,
                title: _buildSearchBar(context),
                actions: [
                  IconButton(
                    icon: const Icon(Icons.qr_code_scanner_rounded, size: 24),
                    onPressed: () {},
                    color: AppColors.textPrimary,
                  ),
                ],
              ),

              // Banner Carousel
              SliverToBoxAdapter(child: _buildBanner()),

              // Category Grid
              SliverToBoxAdapter(child: _buildCategoryGrid()),

              // AI Smart Suggestions
              SliverToBoxAdapter(child: _buildAiSuggestions()),

              // Section: Featured Coupons
              SliverToBoxAdapter(
                child: Padding(
                  padding: const EdgeInsets.fromLTRB(20, 24, 20, 12),
                  child: Row(
                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                    children: [
                      Text('精选好券', style: AppTypography.h2),
                      GestureDetector(
                        onTap: () {},
                        child: Text('查看全部', style: AppTypography.labelSmall.copyWith(
                          color: AppColors.primary,
                        )),
                      ),
                    ],
                  ),
                ),
              ),

              // Coupon List
              SliverPadding(
                padding: AppSpacing.pagePadding,
                sliver: SliverList(
                  delegate: SliverChildBuilderDelegate(
                    (context, index) => Padding(
                      padding: const EdgeInsets.only(bottom: 12),
                      child: CouponCard(
                        brandName: _mockBrands[index % _mockBrands.length],
                        couponName: _mockNames[index % _mockNames.length],
                        faceValue: _mockFaceValues[index % _mockFaceValues.length],
                        currentPrice: _mockPrices[index % _mockPrices.length],
                        creditRating: _mockRatings[index % _mockRatings.length],
                        expiryDate: DateTime.now().add(Duration(days: (index + 1) * 5)),
                        onTap: () {
                          Navigator.pushNamed(context, '/coupon/detail');
                        },
                      ),
                    ),
                    childCount: 10,
                  ),
                ),
              ),

              const SliverPadding(padding: EdgeInsets.only(bottom: 100)),
            ],
          ),

          // AI FAB
          Positioned(
            right: 20,
            bottom: 100,
            child: AiFab(
              unreadCount: 3,
              onTap: () {
                Navigator.pushNamed(context, '/ai-chat');
              },
            ),
          ),
        ],
      ),
    );
  }

  Widget _buildSearchBar(BuildContext context) {
    return GestureDetector(
      onTap: () {
        Navigator.pushNamed(context, '/search');
      },
      child: Container(
        height: 40,
        decoration: BoxDecoration(
          color: AppColors.gray50,
          borderRadius: AppSpacing.borderRadiusFull,
          border: Border.all(color: AppColors.borderLight),
        ),
        child: Row(
          children: [
            const SizedBox(width: 14),
            const Icon(Icons.search_rounded, size: 20, color: AppColors.textTertiary),
            const SizedBox(width: 8),
            Text(
              '搜索券、品牌、分类...',
              style: AppTypography.bodyMedium.copyWith(color: AppColors.textTertiary),
            ),
          ],
        ),
      ),
    );
  }

  Widget _buildBanner() {
    return Container(
      height: 160,
      margin: const EdgeInsets.fromLTRB(20, 8, 20, 0),
      child: PageView.builder(
        itemCount: 3,
        itemBuilder: (context, index) {
          final colors = [
            AppColors.primaryGradient,
            AppColors.successGradient,
            AppColors.cardGradient,
          ];
          final titles = ['新用户专享', '限时折扣', '热门推荐'];
          final subtitles = ['首单立减 \$10', '全场低至7折', '精选高折扣券'];

          return Container(
            margin: const EdgeInsets.symmetric(horizontal: 4),
            decoration: BoxDecoration(
              gradient: colors[index],
              borderRadius: AppSpacing.borderRadiusLg,
            ),
            padding: const EdgeInsets.all(20),
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              mainAxisAlignment: MainAxisAlignment.end,
              children: [
                Text(
                  titles[index],
                  style: AppTypography.h1.copyWith(color: Colors.white),
                ),
                const SizedBox(height: 4),
                Text(
                  subtitles[index],
                  style: AppTypography.bodyMedium.copyWith(
                    color: Colors.white.withValues(alpha: 0.8),
                  ),
                ),
              ],
            ),
          );
        },
      ),
    );
  }

  Widget _buildCategoryGrid() {
    final categories = [
      ('餐饮', Icons.restaurant_rounded, AppColors.couponDining),
      ('购物', Icons.shopping_bag_rounded, AppColors.couponShopping),
      ('娱乐', Icons.sports_esports_rounded, AppColors.couponEntertainment),
      ('出行', Icons.directions_car_rounded, AppColors.couponTravel),
      ('生活', Icons.home_rounded, AppColors.couponOther),
      ('品牌', Icons.storefront_rounded, AppColors.primary),
      ('折扣', Icons.local_offer_rounded, AppColors.error),
      ('全部', Icons.grid_view_rounded, AppColors.textSecondary),
    ];

    return Padding(
      padding: const EdgeInsets.fromLTRB(20, 20, 20, 0),
      child: GridView.builder(
        shrinkWrap: true,
        physics: const NeverScrollableScrollPhysics(),
        gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(
          crossAxisCount: 4,
          mainAxisSpacing: 8,
          crossAxisSpacing: 8,
          childAspectRatio: 0.85,
        ),
        itemCount: categories.length,
        itemBuilder: (context, index) {
          final (name, icon, color) = categories[index];
          return GestureDetector(
            onTap: () {},
            child: Column(
              mainAxisSize: MainAxisSize.min,
              children: [
                Container(
                  width: 48,
                  height: 48,
                  decoration: BoxDecoration(
                    color: color.withValues(alpha: 0.1),
                    borderRadius: AppSpacing.borderRadiusMd,
                  ),
                  child: Icon(icon, color: color, size: 24),
                ),
                const SizedBox(height: 6),
                Text(name, style: AppTypography.caption.copyWith(
                  color: AppColors.textPrimary,
                  fontWeight: FontWeight.w500,
                )),
              ],
            ),
          );
        },
      ),
    );
  }

  Widget _buildAiSuggestions() {
    return Container(
      margin: const EdgeInsets.fromLTRB(20, 16, 20, 0),
      padding: const EdgeInsets.all(14),
      decoration: BoxDecoration(
        color: AppColors.primarySurface,
        borderRadius: AppSpacing.borderRadiusMd,
        border: Border.all(color: AppColors.primary.withValues(alpha: 0.15)),
      ),
      child: Row(
        children: [
          Container(
            width: 32,
            height: 32,
            decoration: BoxDecoration(
              gradient: AppColors.primaryGradient,
              borderRadius: AppSpacing.borderRadiusSm,
            ),
            child: const Icon(Icons.auto_awesome_rounded, color: Colors.white, size: 16),
          ),
          const SizedBox(width: 10),
          Expanded(
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Text('AI 推荐', style: AppTypography.labelSmall.copyWith(
                  color: AppColors.primary,
                )),
                const SizedBox(height: 2),
                Text(
                  '根据你的偏好，发现了3张高性价比券',
                  style: AppTypography.bodySmall,
                  maxLines: 1,
                  overflow: TextOverflow.ellipsis,
                ),
              ],
            ),
          ),
          const Icon(Icons.chevron_right_rounded, color: AppColors.primary, size: 20),
        ],
      ),
    );
  }
}

// Mock data
const _mockBrands = ['Starbucks', 'Amazon', 'Walmart', 'Target', 'Nike'];
const _mockNames = ['星巴克 \$25 礼品卡', 'Amazon \$100 购物券', 'Walmart \$50 生活券', 'Target \$30 折扣券', 'Nike \$80 运动券'];
const _mockFaceValues = [25.0, 100.0, 50.0, 30.0, 80.0];
const _mockPrices = [21.25, 85.0, 42.5, 24.0, 68.0];
const _mockRatings = ['AAA', 'AA', 'AAA', 'A', 'AA'];
