import 'package:flutter_riverpod/flutter_riverpod.dart';
import '../services/sakenowa_service.dart';
import '../services/sakenowa_recommendation_service.dart';
import '../services/sakenowa_similar_recommendation_service.dart';
import '../services/sakenowa_auto_matching_service.dart';
import '../models/sakenowa/sakenowa_models.dart';
import '../models/sake_item.dart';

/// SakenowaServiceのシングルトンプロバイダー
final sakenowaServiceProvider = Provider<SakenowaService>((ref) {
  return SakenowaService();
});

/// 銘柄データプロバイダー（キャッシュ付き）
final sakenowaBrandsProvider = FutureProvider<List<SakenowaBrand>>((ref) async {
  final service = ref.watch(sakenowaServiceProvider);
  return service.getBrands();
});

/// 蔵元データプロバイダー
final sakenowaBreweriesProvider = FutureProvider<List<SakenowaBrewery>>((ref) async {
  final service = ref.watch(sakenowaServiceProvider);
  return service.getBreweries();
});

/// 地域データプロバイダー
final sakenowaAreasProvider = FutureProvider<List<SakenowaArea>>((ref) async {
  final service = ref.watch(sakenowaServiceProvider);
  return service.getAreas();
});

/// フレーバーチャートプロバイダー
final sakenowaFlavorChartsProvider = FutureProvider<List<SakenowaFlavorChart>>((ref) async {
  final service = ref.watch(sakenowaServiceProvider);
  return service.getFlavorCharts();
});

/// ランキングプロバイダー
final sakenowaRankingsProvider = FutureProvider<List<SakenowaRanking>>((ref) async {
  final service = ref.watch(sakenowaServiceProvider);
  return service.getRankings();
});

/// 銘柄名検索プロバイダー
final sakenowaBrandSearchProvider = FutureProvider.family<List<SakenowaBrand>, String>((ref, query) async {
  if (query.isEmpty) return [];
  final service = ref.watch(sakenowaServiceProvider);
  return service.searchBrandsByName(query);
});

/// 銘柄IDからフレーバーチャートを取得
final sakenowaFlavorChartByBrandIdProvider = FutureProvider.family<SakenowaFlavorChart?, int>((ref, brandId) async {
  final service = ref.watch(sakenowaServiceProvider);
  return service.getFlavorChartByBrandId(brandId);
});

/// SakenowaRecommendationServiceプロバイダー
final sakenowaRecommendationServiceProvider = Provider<SakenowaRecommendationService>((ref) {
  final sakenowaService = ref.watch(sakenowaServiceProvider);
  return SakenowaRecommendationService(sakenowaService);
});

/// ✅ SakenowaSimilarRecommendationServiceプロバイダー
final sakenowaSimilarRecommendationServiceProvider = Provider<SakenowaSimilarRecommendationService>((ref) {
  final sakenowaService = ref.watch(sakenowaServiceProvider);
  return SakenowaSimilarRecommendationService(sakenowaService);
});

/// ✅ 類似推薦プロバイダー（特定の日本酒に対する類似銘柄）
/// [baseSake]: 基準となる日本酒
/// [userItems]: ユーザーの全日本酒リスト（除外判定用）
final sakenowaSimilarRecommendationsProvider = FutureProvider.family<
  List<SimilarSakeRecommendation>,
  ({SakeItem baseSake, List<SakeItem>? userItems})
>((ref, params) async {
  final service = ref.watch(sakenowaSimilarRecommendationServiceProvider);
  return service.getSimilarFromSakenowa(
    baseSake: params.baseSake,
    limit: 5,
    excludeOwned: true,
    userItems: params.userItems,
  );
});

/// ✅ SakenowaAutoMatchingServiceプロバイダー
final sakenowaAutoMatchingServiceProvider = Provider<SakenowaAutoMatchingService>((ref) {
  final sakenowaService = ref.watch(sakenowaServiceProvider);
  return SakenowaAutoMatchingService(sakenowaService);
});
