/// Invoice Section Widget
///
/// Optional invoice information section with address selection.
library;

import 'package:flutter/material.dart';
import 'package:font_awesome_flutter/font_awesome_flutter.dart';
import 'package:go_router/go_router.dart';
import 'package:hooks_riverpod/hooks_riverpod.dart';
import 'package:worker/core/constants/ui_constants.dart';
import 'package:worker/core/theme/colors.dart';
import 'package:worker/features/account/presentation/providers/address_provider.dart';

/// Invoice Section
///
/// Shows invoice toggle and default address when enabled.
/// Matches HTML design from checkout.html.
class InvoiceSection extends HookConsumerWidget {
  const InvoiceSection({super.key, required this.needsInvoice});

  final ValueNotifier<bool> needsInvoice;

  @override
  Widget build(BuildContext context, WidgetRef ref) {
    // Watch the default address
    final defaultAddr = ref.watch(defaultAddressProvider);

    return Container(
      margin: const EdgeInsets.symmetric(horizontal: AppSpacing.md),
      padding: const EdgeInsets.all(AppSpacing.md),
      decoration: BoxDecoration(
        color: Colors.white,
        borderRadius: BorderRadius.circular(AppRadius.card),
        boxShadow: [
          BoxShadow(
            color: Colors.black.withValues(alpha: 0.05),
            blurRadius: 8,
            offset: const Offset(0, 2),
          ),
        ],
      ),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          // Header with Toggle
          Row(
            children: [
              const FaIcon(
                FontAwesomeIcons.fileInvoice,
                color: AppColors.primaryBlue,
                size: 16,
              ),
              const SizedBox(width: AppSpacing.sm),
              const Expanded(
                child: Text(
                  'Phát hành hóa đơn',
                  style: TextStyle(
                    fontSize: 16,
                    fontWeight: FontWeight.w600,
                    color: Color(0xFF212121),
                  ),
                ),
              ),
              // Toggle Switch
              Switch(
                value: needsInvoice.value,
                onChanged: (value) {
                  needsInvoice.value = value;
                },
                activeTrackColor: AppColors.primaryBlue,
              ),
            ],
          ),

          // Invoice Information (visible when toggle is ON)
          if (needsInvoice.value) ...[
            const SizedBox(height: AppSpacing.md),
            const Divider(color: Color(0xFFE0E0E0)),
            const SizedBox(height: AppSpacing.md),

            // Address Card
            if (defaultAddr != null)
              InkWell(
                onTap: () {
                  // Navigate to addresses page for selection
                  context.push('/account/addresses');
                },
                borderRadius: BorderRadius.circular(AppRadius.sm),
                child: Container(
                  padding: const EdgeInsets.all(AppSpacing.sm),
                  decoration: BoxDecoration(
                    border: Border.all(color: const Color(0xFFE0E0E0)),
                    borderRadius: BorderRadius.circular(AppRadius.sm),
                  ),
                  child: Row(
                    children: [
                      Expanded(
                        child: Column(
                          crossAxisAlignment: CrossAxisAlignment.start,
                          children: [
                            // Company/Address Title
                            Text(
                              defaultAddr.addressTitle,
                              style: const TextStyle(
                                fontSize: 14,
                                fontWeight: FontWeight.w600,
                                color: Color(0xFF212121),
                              ),
                            ),
                            const SizedBox(height: 4),

                            // Tax Code (if available)
                            if (defaultAddr.taxCode != null &&
                                defaultAddr.taxCode!.isNotEmpty) ...[
                              Text(
                                'Mã số thuế: ${defaultAddr.taxCode}',
                                style: const TextStyle(
                                  fontSize: 12,
                                  color: Color(0xFF757575),
                                ),
                              ),
                              const SizedBox(height: 2),
                            ],

                            // Phone
                            Text(
                              'Số điện thoại: ${defaultAddr.phone}',
                              style: const TextStyle(
                                fontSize: 12,
                                color: Color(0xFF757575),
                              ),
                            ),
                            const SizedBox(height: 2),

                            // Email (if available)
                            if (defaultAddr.email != null &&
                                defaultAddr.email!.isNotEmpty) ...[
                              Text(
                                'Email: ${defaultAddr.email}',
                                style: const TextStyle(
                                  fontSize: 12,
                                  color: Color(0xFF757575),
                                ),
                              ),
                              const SizedBox(height: 2),
                            ],

                            // Address
                            Text(
                              'Địa chỉ: ${defaultAddr.fullAddress}',
                              style: const TextStyle(
                                fontSize: 12,
                                color: Color(0xFF757575),
                              ),
                            ),
                          ],
                        ),
                      ),
                      const SizedBox(width: AppSpacing.sm),
                      const FaIcon(
                        FontAwesomeIcons.chevronRight,
                        size: 14,
                        color: Color(0xFF9E9E9E),
                      ),
                    ],
                  ),
                ),
              )
            else
              // No default address - show button to add
              InkWell(
                onTap: () {
                  context.push('/account/addresses');
                },
                borderRadius: BorderRadius.circular(AppRadius.sm),
                child: Container(
                  padding: const EdgeInsets.all(AppSpacing.md),
                  decoration: BoxDecoration(
                    border: Border.all(
                      color: AppColors.primaryBlue,
                      style: BorderStyle.solid,
                    ),
                    borderRadius: BorderRadius.circular(AppRadius.sm),
                  ),
                  child: const Row(
                    mainAxisAlignment: MainAxisAlignment.center,
                    children: [
                      FaIcon(
                        FontAwesomeIcons.plus,
                        size: 14,
                        color: AppColors.primaryBlue,
                      ),
                      SizedBox(width: AppSpacing.sm),
                      Text(
                        'Thêm địa chỉ xuất hóa đơn',
                        style: TextStyle(
                          fontSize: 14,
                          fontWeight: FontWeight.w600,
                          color: AppColors.primaryBlue,
                        ),
                      ),
                    ],
                  ),
                ),
              ),
          ],
        ],
      ),
    );
  }
}
