/// Gifts Catalog Provider
///
/// State management for gift catalog and filtering.
library;

import 'package:riverpod_annotation/riverpod_annotation.dart';
import 'package:worker/features/loyalty/domain/entities/gift_catalog.dart';

part 'gifts_provider.g.dart';

/// Gift catalog provider
///
/// Provides the complete list of available gifts.
/// Currently returns mock data matching the HTML design.
@riverpod
class Gifts extends _$Gifts {
  @override
  List<GiftCatalog> build() {
    // Mock gift catalog matching HTML design
    return _getMockGifts();
  }

  /// Refresh gift catalog
  Future<void> refresh() async {
    // TODO: Fetch from API
    state = _getMockGifts();
  }

  /// Get mock gifts data
  List<GiftCatalog> _getMockGifts() {
    final now = DateTime.now();

    return [
      GiftCatalog(
        catalogId: 'gift_001',
        name: 'Voucher 500.000đ',
        description: 'Áp dụng cho đơn từ 5 triệu',
        imageUrl:
            'https://images.unsplash.com/photo-1556742049-0cfed4f6a45d?w=300&h=200&fit=crop',
        category: GiftCategory.voucher,
        pointsCost: 2500,
        cashValue: 500000,
        quantityAvailable: 100,
        quantityRedeemed: 20,
        isActive: true,
        createdAt: now,
        updatedAt: now,
      ),
      GiftCatalog(
        catalogId: 'gift_002',
        name: 'Bộ keo chà ron cao cấp',
        description: 'Weber.color comfort',
        imageUrl:
            'https://images.unsplash.com/photo-1607082348824-0a96f2a4b9da?w=300&h=200&fit=crop',
        category: GiftCategory.product,
        pointsCost: 3000,
        quantityAvailable: 50,
        quantityRedeemed: 15,
        isActive: true,
        createdAt: now,
        updatedAt: now,
      ),
      GiftCatalog(
        catalogId: 'gift_003',
        name: 'Tư vấn thiết kế miễn phí',
        description: '1 buổi tư vấn tại nhà',
        imageUrl:
            'https://images.unsplash.com/photo-1540932239986-30128078f3c5?w=300&h=200&fit=crop',
        category: GiftCategory.service,
        pointsCost: 5000,
        quantityAvailable: 30,
        quantityRedeemed: 8,
        isActive: true,
        createdAt: now,
        updatedAt: now,
      ),
      GiftCatalog(
        catalogId: 'gift_004',
        name: 'Gạch trang trí Premium',
        description: 'Bộ 10m² gạch cao cấp',
        imageUrl:
            'https://images.unsplash.com/photo-1615874694520-474822394e73?w=300&h=200&fit=crop',
        category: GiftCategory.product,
        pointsCost: 8000,
        quantityAvailable: 25,
        quantityRedeemed: 5,
        isActive: true,
        createdAt: now,
        updatedAt: now,
      ),
      GiftCatalog(
        catalogId: 'gift_005',
        name: 'Áo thun EuroTile',
        description: 'Limited Edition 2023',
        imageUrl:
            'https://images.unsplash.com/photo-1523381210434-271e8be1f52b?w=300&h=200&fit=crop',
        category: GiftCategory.product,
        pointsCost: 1500,
        quantityAvailable: 200,
        quantityRedeemed: 50,
        isActive: true,
        createdAt: now,
        updatedAt: now,
      ),
      GiftCatalog(
        catalogId: 'gift_006',
        name: 'Nâng hạng thẻ Platinum',
        description: 'Ưu đãi cao cấp 1 năm',
        imageUrl:
            'https://images.unsplash.com/photo-1556742400-b5b7a512f3d7?w=300&h=200&fit=crop',
        category: GiftCategory.other,
        pointsCost: 15000,
        quantityAvailable: 10,
        quantityRedeemed: 2,
        isActive: true,
        createdAt: now,
        updatedAt: now,
      ),
    ];
  }
}

/// Selected gift category state provider
@riverpod
class SelectedGiftCategory extends _$SelectedGiftCategory {
  @override
  GiftCategory? build() {
    // null means "All" is selected
    return null;
  }

  /// Set selected category
  void setCategory(GiftCategory? category) {
    state = category;
  }

  /// Clear selection (show all)
  void clearSelection() {
    state = null;
  }
}

/// Filtered gifts provider
///
/// Filters gifts based on selected category.
@riverpod
List<GiftCatalog> filteredGifts(Ref ref) {
  final allGifts = ref.watch(giftsProvider);
  final selectedCategory = ref.watch(selectedGiftCategoryProvider);

  // If no category selected, return all gifts
  if (selectedCategory == null) {
    return allGifts;
  }

  // Filter by selected category
  return allGifts.where((gift) => gift.category == selectedCategory).toList();
}

/// Vietnamese category display names
extension GiftCategoryVi on GiftCategory {
  String get displayNameVi {
    switch (this) {
      case GiftCategory.voucher:
        return 'Voucher';
      case GiftCategory.product:
        return 'Sản phẩm';
      case GiftCategory.service:
        return 'Dịch vụ';
      case GiftCategory.discount:
        return 'Ưu đãi đặc biệt';
      case GiftCategory.other:
        return 'Khác';
    }
  }
}
