import 'package:auto_route/auto_route.dart';
import 'package:flutter/material.dart';

import '../../../../../common/theme/theme.dart';

@RoutePage()
class VoucherDetailPage extends StatelessWidget {
  const VoucherDetailPage({super.key});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: AppColor.background,
      appBar: AppBar(
        title: Text(
          'Detail Voucher',
          style: AppStyle.xl.copyWith(
            fontWeight: FontWeight.w600,
            color: AppColor.textPrimary,
          ),
        ),
        backgroundColor: AppColor.backgroundLight,
        elevation: 0,
        iconTheme: IconThemeData(color: AppColor.textPrimary),
        bottom: PreferredSize(
          preferredSize: Size.fromHeight(1),
          child: Container(height: 1, color: AppColor.borderLight),
        ),
      ),
      body: SingleChildScrollView(
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            // Voucher Card Section
            Container(
              margin: EdgeInsets.all(16),
              padding: EdgeInsets.all(20),
              decoration: BoxDecoration(
                gradient: LinearGradient(
                  colors: AppColor.primaryGradient,
                  begin: Alignment.topLeft,
                  end: Alignment.bottomRight,
                ),
                borderRadius: BorderRadius.circular(16),
                boxShadow: [
                  BoxShadow(
                    color: AppColor.primary.withOpacity(0.3),
                    offset: Offset(0, 4),
                    blurRadius: 12,
                  ),
                ],
              ),
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  // Voucher Icon
                  Container(
                    width: 48,
                    height: 48,
                    decoration: BoxDecoration(
                      color: AppColor.white.withOpacity(0.2),
                      borderRadius: BorderRadius.circular(12),
                    ),
                    child: Icon(
                      Icons.local_offer,
                      color: AppColor.white,
                      size: 24,
                    ),
                  ),
                  SizedBox(height: 16),

                  // Title
                  Text(
                    'New User Voucher - Diskon 50% hingga Rp35K',
                    style: AppStyle.xl.copyWith(
                      color: AppColor.white,
                      fontWeight: FontWeight.bold,
                    ),
                  ),
                  SizedBox(height: 8),

                  // Subtitle
                  Text(
                    'Tanpa Min. Belanja',
                    style: AppStyle.md.copyWith(
                      color: AppColor.white.withOpacity(0.9),
                    ),
                  ),
                  SizedBox(height: 20),

                  // Voucher Details Row
                  Row(
                    children: [
                      Expanded(
                        child: _buildDetailItem(
                          icon: Icons.schedule,
                          label: 'Berlaku hingga',
                          value: '25 Sep 2025',
                        ),
                      ),
                      Container(
                        width: 1,
                        height: 40,
                        color: AppColor.white.withOpacity(0.3),
                        margin: EdgeInsets.symmetric(horizontal: 16),
                      ),
                      Expanded(
                        child: _buildDetailItem(
                          icon: Icons.shopping_cart,
                          label: 'Min. Transaksi',
                          value: '-',
                        ),
                      ),
                    ],
                  ),
                ],
              ),
            ),

            // Action Buttons
            Padding(
              padding: EdgeInsets.symmetric(horizontal: 16),
              child: Row(
                children: [
                  Expanded(
                    child: ElevatedButton.icon(
                      onPressed: () {
                        // Copy voucher code functionality
                        _copyVoucherCode(context);
                      },
                      style: ElevatedButton.styleFrom(
                        backgroundColor: AppColor.backgroundLight,
                        foregroundColor: AppColor.primary,
                        elevation: 0,
                        padding: EdgeInsets.symmetric(vertical: 16),
                        shape: RoundedRectangleBorder(
                          borderRadius: BorderRadius.circular(12),
                          side: BorderSide(color: AppColor.border),
                        ),
                      ),
                      icon: Icon(Icons.copy, size: 20),
                      label: Text(
                        'Salin Kode',
                        style: AppStyle.md.copyWith(
                          fontWeight: FontWeight.w600,
                          color: AppColor.primary,
                        ),
                      ),
                    ),
                  ),
                  SizedBox(width: 12),
                  Expanded(
                    child: ElevatedButton(
                      onPressed: () {
                        // Use voucher functionality
                        _useVoucher(context);
                      },
                      style: ElevatedButton.styleFrom(
                        backgroundColor: AppColor.primary,
                        foregroundColor: AppColor.white,
                        elevation: 0,
                        padding: EdgeInsets.symmetric(vertical: 16),
                        shape: RoundedRectangleBorder(
                          borderRadius: BorderRadius.circular(12),
                        ),
                      ),
                      child: Text(
                        'Gunakan',
                        style: AppStyle.md.copyWith(
                          fontWeight: FontWeight.w600,
                          color: AppColor.white,
                        ),
                      ),
                    ),
                  ),
                ],
              ),
            ),

            SizedBox(height: 24),

            // Description Section
            _buildSection(
              title: 'Deskripsi',
              content:
                  'Dapatkan diskon hingga 50% untuk pembelian pertama Anda! Voucher ini khusus untuk pengguna baru dan berlaku untuk semua kategori produk tanpa minimum pembelian.',
            ),
            SizedBox(height: 16),

            // Terms and Conditions Section
            _buildSection(
              title: 'Syarat dan Ketentuan',
              content: _getDefaultTermsAndConditions(),
            ),

            SizedBox(height: 24),
          ],
        ),
      ),
    );
  }

  Widget _buildDetailItem({
    required IconData icon,
    required String label,
    required String value,
  }) {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        Row(
          children: [
            Icon(icon, size: 16, color: AppColor.white.withOpacity(0.8)),
            SizedBox(width: 4),
            Text(
              label,
              style: AppStyle.xs.copyWith(
                color: AppColor.white.withOpacity(0.8),
              ),
            ),
          ],
        ),
        SizedBox(height: 4),
        Text(
          value,
          style: AppStyle.sm.copyWith(
            color: AppColor.white,
            fontWeight: FontWeight.w600,
          ),
        ),
      ],
    );
  }

  Widget _buildSection({required String title, required String content}) {
    return Container(
      margin: EdgeInsets.symmetric(horizontal: 16),
      padding: EdgeInsets.all(20),
      decoration: BoxDecoration(
        color: AppColor.backgroundLight,
        borderRadius: BorderRadius.circular(16),
        border: Border.all(color: AppColor.borderLight),
      ),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Text(
            title,
            style: AppStyle.lg.copyWith(
              fontWeight: FontWeight.bold,
              color: AppColor.textPrimary,
            ),
          ),
          SizedBox(height: 12),
          Text(
            content,
            style: AppStyle.md.copyWith(
              color: AppColor.textSecondary,
              height: 1.5,
            ),
          ),
        ],
      ),
    );
  }

  void _copyVoucherCode(BuildContext context) {
    // Implementation for copying voucher code
    ScaffoldMessenger.of(context).showSnackBar(
      SnackBar(
        content: Text(
          'Kode voucher berhasil disalin!',
          style: AppStyle.md.copyWith(color: AppColor.white),
        ),
        backgroundColor: AppColor.success,
        behavior: SnackBarBehavior.floating,
        margin: EdgeInsets.all(16),
        shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(12)),
      ),
    );
  }

  void _useVoucher(BuildContext context) {
    // Implementation for using voucher
    // Navigate back to checkout or shopping cart
    context.router.back(); // Return true to indicate voucher was selected
  }

  String _getDefaultTermsAndConditions() {
    return '''• Voucher hanya berlaku untuk pengguna baru
• Tidak dapat digabungkan dengan promo lain
• Berlaku untuk semua kategori produk
• Voucher tidak dapat diuangkan
• Voucher akan hangus jika tidak digunakan sebelum tanggal expired
• Satu voucher hanya berlaku untuk satu kali transaksi
• Voucher tidak berlaku untuk produk yang sudah didiskon
• Kebijakan voucher dapat berubah sewaktu-waktu''';
  }
}
