import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:riverpod_annotation/riverpod_annotation.dart';
import '../api/api_client_provider.dart';

part 'chat_opening_provider.g.dart';

class PriceTier {
  final int durationMinutes;
  final int price;
  final String label;

  PriceTier({required this.durationMinutes, required this.price, required this.label});

  factory PriceTier.fromJson(Map<String, dynamic> json) {
    return PriceTier(
      durationMinutes: json['duration_minutes'] as int,
      price: json['price'] as int,
      label: json['label'] as String,
    );
  }
}

class PricingData {
  final List<PriceTier> tiers;
  final bool freeTrialEligible;
  final int freeTrialDurationMinutes;

  const PricingData({
    required this.tiers,
    required this.freeTrialEligible,
    this.freeTrialDurationMinutes = 5,
  });
}

@riverpod
Future<PricingData> chatPricing(Ref ref) async {
  final apiClient = ref.read(apiClientProvider);
  final response = await apiClient.get('/api/client/chat/pricing');
  final data = response['data'] as Map<String, dynamic>;
  final tiersJson = data['tiers'] as List<dynamic>;
  final tiers = tiersJson.map((t) => PriceTier.fromJson(t as Map<String, dynamic>)).toList();
  final freeTrial = data['free_trial'] as Map<String, dynamic>;

  return PricingData(
    tiers: tiers,
    freeTrialEligible: freeTrial['eligible'] as bool? ?? false,
    freeTrialDurationMinutes: freeTrial['duration_minutes'] as int? ?? 5,
  );
}
