import 'package:flutter/material.dart';
import 'package:em2rp/models/event_model.dart';
import 'package:em2rp/utils/colors.dart';
import 'package:em2rp/utils/price_helpers.dart';
import 'package:intl/intl.dart';
import 'package:em2rp/views/widgets/event_form/event_options_display_widget.dart';

class EventDetailsInfo extends StatelessWidget {
  final EventModel event;
  final bool canViewPrices;

  const EventDetailsInfo({
    super.key,
    required this.event,
    required this.canViewPrices,
  });

  @override
  Widget build(BuildContext context) {
    final dateFormat = DateFormat('dd/MM/yyyy HH:mm');
    final currencyFormat = NumberFormat.currency(locale: 'fr_FR', symbol: '€');

    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        _buildInfoRow(
          context,
          Icons.calendar_today,
          'Horaire de début',
          dateFormat.format(event.startDateTime),
        ),
        _buildInfoRow(
          context,
          Icons.calendar_today,
          'Horaire de fin',
          dateFormat.format(event.endDateTime),
        ),
        if (canViewPrices) ...[
          // Calcul des prix HT/TVA/TTC
          Builder(
            builder: (context) {
              final pricing = PriceHelpers.getPricing(event);
              return Column(
                children: [
                  _buildInfoRow(
                    context,
                    Icons.euro,
                    'Prix HT',
                    pricing.formattedHT,
                  ),
                  Padding(
                    padding: const EdgeInsets.symmetric(vertical: 4, horizontal: 8),
                    child: Row(
                      children: [
                        Icon(Icons.percent, size: 16, color: Colors.grey[600]),
                        const SizedBox(width: 8),
                        Text(
                          'TVA (${pricing.taxRatePercentage.toStringAsFixed(0)}%) : ${pricing.formattedTax}',
                          style: TextStyle(
                            fontSize: 12,
                            color: Colors.grey[600],
                            fontStyle: FontStyle.italic,
                          ),
                        ),
                      ],
                    ),
                  ),
                  _buildInfoRow(
                    context,
                    Icons.attach_money,
                    'Prix TTC',
                    pricing.formattedTTC,
                    highlighted: true,
                  ),
                ],
              );
            },
          ),
        ],
        if (event.options.isNotEmpty) ...[
          EventOptionsDisplayWidget(
            optionsData: event.options,
            canViewPrices: canViewPrices,
            showPriceCalculation: false,
          ),
        ],
        if (canViewPrices) ...[
          const SizedBox(height: 4),
          Builder(
            builder: (context) {
              // Total TTC = basePrice (TTC) + options (TTC)
              final totalTTC = event.basePrice +
                  event.options.fold<num>(
                    0,
                    (sum, opt) {
                      final priceTTC = opt['price'] ?? 0.0;
                      final quantity = opt['quantity'] ?? 1;
                      return sum + (priceTTC * quantity);
                    },
                  );

              // Calculer le total HT
              final totalHT = PriceHelpers.calculateHT(totalTTC.toDouble());
              final totalTVA = PriceHelpers.calculateTax(totalHT);

              return Padding(
                padding: const EdgeInsets.only(top: 8.0, bottom: 8.0),
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    // Séparateur visuel
                    const Divider(thickness: 1),
                    const SizedBox(height: 8),

                    // Prix total HT
                    Row(
                      children: [
                        const Icon(Icons.euro, color: AppColors.rouge, size: 22),
                        const SizedBox(width: 8),
                        Text(
                          'Prix total HT : ',
                          style: Theme.of(context).textTheme.titleMedium?.copyWith(
                                color: AppColors.noir,
                                fontWeight: FontWeight.bold,
                              ),
                        ),
                        Text(
                          currencyFormat.format(totalHT),
                          style: Theme.of(context).textTheme.titleMedium?.copyWith(
                                color: AppColors.noir,
                                fontWeight: FontWeight.bold,
                              ),
                        ),
                      ],
                    ),

                    // TVA en petit
                    Padding(
                      padding: const EdgeInsets.only(left: 30.0, top: 4.0, bottom: 4.0),
                      child: Text(
                        'TVA (20%) : ${currencyFormat.format(totalTVA)}',
                        style: TextStyle(
                          fontSize: 11,
                          color: Colors.grey[600],
                          fontStyle: FontStyle.italic,
                        ),
                      ),
                    ),

                    // Prix total TTC en surbrillance
                    Row(
                      children: [
                        const Icon(Icons.attach_money, color: AppColors.rouge, size: 24),
                        const SizedBox(width: 8),
                        Text(
                          'Prix total TTC : ',
                          style: Theme.of(context).textTheme.titleLarge?.copyWith(
                                color: AppColors.noir,
                                fontWeight: FontWeight.w900,
                              ),
                        ),
                        Text(
                          currencyFormat.format(totalTTC),
                          style: Theme.of(context).textTheme.titleLarge?.copyWith(
                                color: AppColors.rouge,
                                fontWeight: FontWeight.w900,
                              ),
                        ),
                      ],
                    ),
                  ],
                ),
              );
            },
          ),
        ],
        _buildInfoRow(
          context,
          Icons.build,
          'Temps d\'installation',
          '${event.installationTime} heures',
        ),
        Builder(
          builder: (context) {
            final arrival = event.startDateTime.subtract(
              Duration(hours: event.installationTime),
            );
            return Padding(
              padding: const EdgeInsets.only(left: 36.0, bottom: 4.0),
              child: Text(
                'Horaire d\'arrivée prévisionnel : ${dateFormat.format(arrival)}',
                style: Theme.of(context).textTheme.bodySmall?.copyWith(
                      color: Colors.grey[700],
                    ),
              ),
            );
          },
        ),
        _buildInfoRow(
          context,
          Icons.construction,
          'Temps de démontage',
          '${event.disassemblyTime} heures',
        ),
        Builder(
          builder: (context) {
            final departure = event.endDateTime.add(
              Duration(hours: event.disassemblyTime),
            );
            return Padding(
              padding: const EdgeInsets.only(left: 36.0, bottom: 4.0),
              child: Text(
                'Horaire de départ prévu : ${dateFormat.format(departure)}',
                style: Theme.of(context).textTheme.bodySmall?.copyWith(
                      color: Colors.grey[700],
                    ),
              ),
            );
          },
        ),
      ],
    );
  }

  Widget _buildInfoRow(
    BuildContext context,
    IconData icon,
    String label,
    String value, {
    bool highlighted = false,
  }) {
    return Padding(
      padding: const EdgeInsets.symmetric(vertical: 8),
      child: Row(
        children: [
          Icon(icon, color: highlighted ? AppColors.rouge : AppColors.rouge),
          const SizedBox(width: 8),
          Text(
            '$label : ',
            style: Theme.of(context).textTheme.titleMedium?.copyWith(
                  color: AppColors.noir,
                  fontWeight: highlighted ? FontWeight.w900 : FontWeight.bold,
                ),
          ),
          Text(
            value,
            style: Theme.of(context).textTheme.titleMedium?.copyWith(
                  color: highlighted ? AppColors.rouge : null,
                  fontWeight: highlighted ? FontWeight.bold : null,
                ),
          ),
        ],
      ),
    );
  }
}

