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

/// 券管理 - 列表页
///
/// 展示发行方所有券批次，支持按状态筛选
/// 顶部AI建议条 + FAB创建新券
class CouponListPage extends StatelessWidget {
  const CouponListPage({super.key});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('券管理'),
        actions: [
          IconButton(
            icon: const Icon(Icons.search_rounded),
            onPressed: () {
              // TODO: Open search overlay
            },
          ),
          IconButton(
            icon: const Icon(Icons.filter_list_rounded),
            onPressed: () {
              // TODO: Show filter bottom sheet
            },
          ),
        ],
      ),
      body: Column(
        children: [
          // AI Suggestion
          _buildAiSuggestion(context),

          // Filter Chips
          _buildFilterChips(),

          // Coupon List
          Expanded(
            child: ListView.builder(
              padding: const EdgeInsets.symmetric(horizontal: 20),
              itemCount: _mockCoupons.length,
              itemBuilder: (context, index) {
                final coupon = _mockCoupons[index];
                return _buildCouponItem(context, coupon);
              },
            ),
          ),
        ],
      ),
      floatingActionButton: FloatingActionButton.extended(
        onPressed: () => Navigator.pushNamed(context, AppRouter.createCoupon),
        backgroundColor: AppColors.primary,
        foregroundColor: Colors.white,
        icon: const Icon(Icons.add_rounded),
        label: const Text('发券'),
      ),
    );
  }

  Widget _buildAiSuggestion(BuildContext context) {
    return Container(
      margin: const EdgeInsets.fromLTRB(20, 8, 20, 8),
      padding: const EdgeInsets.symmetric(horizontal: 14, vertical: 10),
      decoration: BoxDecoration(
        color: AppColors.primarySurface,
        borderRadius: BorderRadius.circular(10),
      ),
      child: Row(
        children: [
          const Icon(Icons.auto_awesome_rounded, color: AppColors.primary, size: 18),
          const SizedBox(width: 8),
          const Expanded(
            child: Text(
              '建议：周末发行餐饮券销量通常提升30%',
              style: TextStyle(fontSize: 12, color: AppColors.primary),
            ),
          ),
          GestureDetector(
            onTap: () {
              // TODO: Dismiss AI suggestion banner
            },
            child: const Icon(Icons.close, size: 16, color: AppColors.textTertiary),
          ),
        ],
      ),
    );
  }

  Widget _buildFilterChips() {
    final filters = ['全部', '在售中', '已售罄', '待审核', '已下架'];
    return SingleChildScrollView(
      scrollDirection: Axis.horizontal,
      padding: const EdgeInsets.symmetric(horizontal: 20, vertical: 8),
      child: Row(
        children: filters.map((f) {
          final isSelected = f == '全部';
          return Padding(
            padding: const EdgeInsets.only(right: 8),
            child: FilterChip(
              label: Text(f),
              selected: isSelected,
              onSelected: (_) {
                // TODO: Apply coupon status filter
              },
              selectedColor: AppColors.primaryContainer,
              checkmarkColor: AppColors.primary,
            ),
          );
        }).toList(),
      ),
    );
  }

  Widget _buildCouponItem(BuildContext context, _MockCoupon coupon) {
    return GestureDetector(
      onTap: () => Navigator.pushNamed(context, AppRouter.couponDetail),
      child: Container(
        margin: const EdgeInsets.only(bottom: 12),
        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: [
            Row(
              children: [
                Container(
                  width: 44,
                  height: 44,
                  decoration: BoxDecoration(
                    color: AppColors.primarySurface,
                    borderRadius: BorderRadius.circular(10),
                  ),
                  child: const Icon(Icons.confirmation_number_rounded, color: AppColors.primary, size: 22),
                ),
                const SizedBox(width: 12),
                Expanded(
                  child: Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      Text(
                        coupon.name,
                        style: const TextStyle(fontSize: 15, fontWeight: FontWeight.w600),
                      ),
                      const SizedBox(height: 2),
                      Text(
                        '${coupon.template} · 面值 \$${coupon.faceValue}',
                        style: const TextStyle(fontSize: 12, color: AppColors.textSecondary),
                      ),
                    ],
                  ),
                ),
                _buildStatusBadge(coupon.status),
              ],
            ),
            const SizedBox(height: 12),
            Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: [
                _buildMiniStat('发行量', '${coupon.issued}'),
                _buildMiniStat('已售', '${coupon.sold}'),
                _buildMiniStat('已核销', '${coupon.redeemed}'),
                _buildMiniStat('核销率', '${coupon.redemptionRate}%'),
              ],
            ),
          ],
        ),
      ),
    );
  }

  Widget _buildStatusBadge(String status) {
    Color color;
    switch (status) {
      case '在售中':
        color = AppColors.success;
        break;
      case '待审核':
        color = AppColors.warning;
        break;
      case '已售罄':
        color = AppColors.info;
        break;
      default:
        color = AppColors.textTertiary;
    }
    return Container(
      padding: const EdgeInsets.symmetric(horizontal: 8, vertical: 3),
      decoration: BoxDecoration(
        color: color.withValues(alpha: 0.1),
        borderRadius: BorderRadius.circular(999),
      ),
      child: Text(status, style: TextStyle(fontSize: 11, color: color, fontWeight: FontWeight.w600)),
    );
  }

  Widget _buildMiniStat(String label, String value) {
    return Column(
      children: [
        Text(value, style: const TextStyle(fontSize: 15, fontWeight: FontWeight.w700, color: AppColors.textPrimary)),
        const SizedBox(height: 2),
        Text(label, style: const TextStyle(fontSize: 11, color: AppColors.textTertiary)),
      ],
    );
  }
}

class _MockCoupon {
  final String name;
  final String template;
  final double faceValue;
  final String status;
  final int issued;
  final int sold;
  final int redeemed;
  final double redemptionRate;

  const _MockCoupon({
    required this.name,
    required this.template,
    required this.faceValue,
    required this.status,
    required this.issued,
    required this.sold,
    required this.redeemed,
    required this.redemptionRate,
  });
}

const _mockCoupons = [
  _MockCoupon(name: '¥25 星巴克礼品卡', template: '礼品卡', faceValue: 25, status: '在售中', issued: 5000, sold: 4200, redeemed: 3300, redemptionRate: 78.5),
  _MockCoupon(name: '¥100 购物代金券', template: '代金券', faceValue: 100, status: '在售中', issued: 2000, sold: 1580, redeemed: 980, redemptionRate: 62.0),
  _MockCoupon(name: '8折餐饮折扣券', template: '折扣券', faceValue: 50, status: '待审核', issued: 1000, sold: 0, redeemed: 0, redemptionRate: 0),
  _MockCoupon(name: '¥200 储值卡', template: '储值券', faceValue: 200, status: '已售罄', issued: 500, sold: 500, redeemed: 420, redemptionRate: 84.0),
];
