/// Widget: Product Info Section
///
/// Displays SKU, product name, pricing, and quick info cards.
library;

import 'package:flutter/material.dart';
import 'package:font_awesome_flutter/font_awesome_flutter.dart';
import 'package:intl/intl.dart';
import 'package:worker/core/theme/colors.dart';
import 'package:worker/features/products/domain/entities/product.dart';

/// Product Info Section
///
/// Displays:
/// - SKU text (small, gray)
/// - Product title (large, bold)
/// - Pricing row (current price, original price, discount badge)
/// - Rating section (5 stars with review count)
/// - Dynamic intro attribute cards (shown only if non-null)
class ProductInfoSection extends StatelessWidget {
  const ProductInfoSection({super.key, required this.product});

  final Product product;

  String _formatPrice(double price) {
    final formatter = NumberFormat('#,###', 'vi_VN');
    return '${formatter.format(price)} VND';
  }

  @override
  Widget build(BuildContext context) {
    final colorScheme = Theme.of(context).colorScheme;

    return Container(
      color: colorScheme.surface,
      padding: const EdgeInsets.all(20),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          // SKU
          Text(
            'SKU: ${product.erpnextItemCode ?? product.productId}',
            style: TextStyle(fontSize: 12, color: colorScheme.onSurfaceVariant),
          ),

          const SizedBox(height: 8),

          // Product Title
          Text(
            product.name,
            style: TextStyle(
              fontSize: 24,
              fontWeight: FontWeight.w700,
              color: colorScheme.onSurface,
              height: 1.3,
            ),
          ),

          const SizedBox(height: 16),

          // Pricing Row
          Row(
            children: [
              // Current Price
              Text(
                '${_formatPrice(product.basePrice)}/m²',
                style: TextStyle(
                  fontSize: 22,
                  fontWeight: FontWeight.w700,
                  color: colorScheme.primary,
                ),
              ),

              // Original Price (if on sale)
              if (product.isOnSale) ...[
                const SizedBox(width: 12),
                Text(
                  _formatPrice(product.basePrice * 1.12), // Mock original price
                  style: TextStyle(
                    fontSize: 16,
                    color: colorScheme.onSurfaceVariant,
                    decoration: TextDecoration.lineThrough,
                  ),
                ),

                const SizedBox(width: 12),

                // Discount Badge
                Container(
                  padding: const EdgeInsets.symmetric(
                    horizontal: 8,
                    vertical: 4,
                  ),
                  decoration: BoxDecoration(
                    color: AppColors.danger,
                    borderRadius: BorderRadius.circular(12),
                  ),
                  child: Text(
                    '-${product.discountPercentage}%',
                    style: TextStyle(
                      color: colorScheme.surface,
                      fontSize: 12,
                      fontWeight: FontWeight.w600,
                    ),
                  ),
                ),
              ],
            ],
          ),

          const SizedBox(height: 16),

          // Rating & Reviews Section
          Row(
            children: [
              // Rating Stars
              const Row(
                children: [
                  Icon(FontAwesomeIcons.solidStar, color: Color(0xFFffc107), size: 16),
                  SizedBox(width: 2),
                  Icon(FontAwesomeIcons.solidStar, color: Color(0xFFffc107), size: 16),
                  SizedBox(width: 2),
                  Icon(FontAwesomeIcons.solidStar, color: Color(0xFFffc107), size: 16),
                  SizedBox(width: 2),
                  Icon(FontAwesomeIcons.solidStar, color: Color(0xFFffc107), size: 16),
                  SizedBox(width: 2),
                  Icon(FontAwesomeIcons.starHalfStroke, color: Color(0xFFffc107), size: 16),
                ],
              ),

              const SizedBox(width: 12),

              // Rating Text
              Text(
                '4.8 (125 đánh giá)',
                style: TextStyle(
                  fontSize: 14,
                  color: colorScheme.onSurfaceVariant,
                ),
              ),
            ],
          ),

          const SizedBox(height: 16),

          // Intro attributes quick info cards (dynamic based on non-null values)
          if (_buildIntroAttributeCards(product, colorScheme).isNotEmpty)
            Row(
              children: _buildIntroAttributeCards(product, colorScheme),
            ),
        ],
      ),
    );
  }

  /// Build intro attribute cards dynamically based on non-null values
  List<Widget> _buildIntroAttributeCards(Product product, ColorScheme colorScheme) {
    final cards = <Widget>[];

    // Define available intro attributes with their display info
    final availableAttributes = [
      {
        'code': 'Size',
        'icon': FontAwesomeIcons.expand,
        'label': 'Kích thước',
      },
      {
        'code': 'Colour',
        'icon': FontAwesomeIcons.palette,
        'label': 'Màu sắc',
      },
      {
        'code': 'UOM',
        'icon': FontAwesomeIcons.boxArchive,
        'label': 'Đóng gói',
      },
    ];

    // Build cards only for non-null values
    for (final attr in availableAttributes) {
      final value = product.getIntroAttribute(attr['code'] as String);
      if (value != null && value.isNotEmpty) {
        cards.add(
          Expanded(
            child: _QuickInfoCard(
              icon: attr['icon'] as IconData,
              label: attr['label'] as String,
              value: value,
              colorScheme: colorScheme,
            ),
          ),
        );

        // Add spacing between cards (except after the last one)
        if (cards.length < availableAttributes.length) {
          cards.add(const SizedBox(width: 8));
        }
      }
    }

    // Remove trailing spacer if it exists
    if (cards.isNotEmpty && cards.last is SizedBox) {
      cards.removeLast();
    }

    return cards;
  }
}

/// Quick Info Card Widget
class _QuickInfoCard extends StatelessWidget {
  const _QuickInfoCard({
    required this.icon,
    required this.label,
    required this.value,
    required this.colorScheme,
  });

  final IconData icon;
  final String label;
  final String value;
  final ColorScheme colorScheme;

  @override
  Widget build(BuildContext context) {
    return Column(
      children: [
        Icon(icon, color: colorScheme.primary, size: 24),
        const SizedBox(height: 4),
        Text(
          label,
          style: TextStyle(fontSize: 12, color: colorScheme.onSurfaceVariant),
          textAlign: TextAlign.center,
        ),
        const SizedBox(height: 2),
        Text(
          value,
          style: TextStyle(
            fontSize: 13,
            fontWeight: FontWeight.w600,
            color: colorScheme.onSurface,
          ),
          textAlign: TextAlign.center,
        ),
      ],
    );
  }
}
