/// Reward Card Widget
///
/// Displays a gift catalog item with redemption button.
library;

import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:font_awesome_flutter/font_awesome_flutter.dart';
import 'package:intl/intl.dart';
import 'package:worker/core/widgets/loading_indicator.dart';
import 'package:worker/features/loyalty/domain/entities/gift_catalog.dart';
import 'package:worker/features/loyalty/presentation/providers/loyalty_points_provider.dart';

/// Reward Card Widget
///
/// Shows gift information and redemption button.
/// Button state changes based on whether user has enough points.
class RewardCard extends ConsumerWidget {
  /// Gift to display
  final GiftCatalog gift;

  /// Callback when redeem button is pressed
  final VoidCallback onRedeem;

  const RewardCard({required this.gift, required this.onRedeem, super.key});

  @override
  Widget build(BuildContext context, WidgetRef ref) {
    final colorScheme = Theme.of(context).colorScheme;
    final hasEnoughPoints = ref.watch(hasEnoughPointsProvider(gift.pointsCost));
    final numberFormat = NumberFormat('#,###', 'vi_VN');

    return Card(
      elevation: 2,
      margin: EdgeInsets.symmetric(horizontal: 8, vertical: 8),
      shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(12)),
      clipBehavior: Clip.antiAlias,
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.stretch,
        children: [
          // Gift Image
          _buildImage(colorScheme),

          // Gift Info
          Expanded(
            child: Padding(
              padding: const EdgeInsets.all(12),
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  // Gift Name
                  Text(
                    gift.name,
                    style: TextStyle(
                      fontSize: 14,
                      fontWeight: FontWeight.w600,
                      color: colorScheme.onSurface,
                      height: 1.3,
                    ),
                    maxLines: 2,
                    overflow: TextOverflow.ellipsis,
                  ),
                  const SizedBox(height: 4),

                  // Gift Description
                  if (gift.description != null && gift.description!.isNotEmpty)
                    Text(
                      gift.description!,
                      style: TextStyle(
                        fontSize: 12,
                        color: colorScheme.onSurfaceVariant,
                        height: 1.2,
                      ),
                      maxLines: 1,
                      overflow: TextOverflow.ellipsis,
                    ),

                  // Spacer to push points and button to bottom
                  const Spacer(),

                  // Points Cost (at bottom)
                  Text(
                    '${numberFormat.format(gift.pointsCost)} điểm',
                    style: TextStyle(
                      fontSize: 14,
                      fontWeight: FontWeight.w700,
                      color: colorScheme.primary,
                    ),
                  ),
                  const SizedBox(height: 8),

                  // Redeem Button (at bottom)
                  SizedBox(
                    width: double.infinity,
                    height: 36,
                    child: ElevatedButton.icon(
                      onPressed: hasEnoughPoints && gift.isAvailable
                          ? onRedeem
                          : null,
                      icon: FaIcon(
                        FontAwesomeIcons.gift,
                        size: 14,
                        color: hasEnoughPoints && gift.isAvailable
                            ? colorScheme.onPrimary
                            : colorScheme.onSurfaceVariant,
                      ),
                      label: Text(
                        hasEnoughPoints && gift.isAvailable
                            ? 'Đổi quà'
                            : 'Không đủ điểm',
                        style: const TextStyle(
                          fontSize: 13,
                          fontWeight: FontWeight.w600,
                        ),
                      ),
                      style: ElevatedButton.styleFrom(
                        backgroundColor: hasEnoughPoints && gift.isAvailable
                            ? colorScheme.primary
                            : colorScheme.surfaceContainerHighest,
                        foregroundColor: hasEnoughPoints && gift.isAvailable
                            ? colorScheme.onPrimary
                            : colorScheme.onSurfaceVariant,
                        elevation: 0,
                        shape: RoundedRectangleBorder(
                          borderRadius: BorderRadius.circular(8),
                        ),
                        padding: const EdgeInsets.symmetric(
                          horizontal: 12,
                          vertical: 8,
                        ),
                      ),
                    ),
                  ),
                ],
              ),
            ),
          ),
        ],
      ),
    );
  }

  /// Build gift image
  Widget _buildImage(ColorScheme colorScheme) {
    return SizedBox(
      height: 120,
      child: gift.imageUrl != null && gift.imageUrl!.isNotEmpty
          ? CachedNetworkImage(
              imageUrl: gift.imageUrl!,
              fit: BoxFit.cover,
              placeholder: (context, url) => Container(
                color: colorScheme.surfaceContainerHighest,
                child: Center(
                  child: CustomLoadingIndicator(color: Theme.of(context).colorScheme.primary, size: 40),
                ),
              ),
              errorWidget: (context, url, error) => Container(
                color: colorScheme.surfaceContainerHighest,
                child: FaIcon(
                  FontAwesomeIcons.gift,
                  size: 48,
                  color: colorScheme.onSurfaceVariant,
                ),
              ),
            )
          : Container(
              color: colorScheme.surfaceContainerHighest,
              child: FaIcon(
                FontAwesomeIcons.gift,
                size: 48,
                color: colorScheme.onSurfaceVariant,
              ),
            ),
    );
  }
}
