// quota_provider.dart — 免费版每日额度状态机(Riverpod,mock 数据)
//
// 设计约定(design/CLAUDE.md §7 / §2):免费额度权威在服务端,本地仅展示。
// 这里以 mock 实现 UI 与数据层解耦的接口形态:剩余分钟 + 是否已看广告解锁。
// 接 API 时只替换本通知器内部,UI 不动。
import 'package:flutter_riverpod/flutter_riverpod.dart';

/// 免费额度快照。
class FreeQuotaState {
  const FreeQuotaState({
    this.totalMinutes = 10,
    this.remainingMinutes = 6,
    this.adUnlocked = false,
  });

  /// 每日总额度(分钟)。§7:免费版每日 10 分钟。
  final int totalMinutes;

  /// 今日剩余分钟(展示值,权威以服务端为准)。
  final int remainingMinutes;

  /// 今日是否已观看激励视频解锁。
  final bool adUnlocked;

  /// 进度(0–1),用于进度条宽度。
  double get progress =>
      totalMinutes == 0 ? 0 : (remainingMinutes / totalMinutes).clamp(0.0, 1.0);

  /// 是否进入低额度警示(≤3 分钟切 warning 色)。
  bool get isLow => remainingMinutes <= 3;

  FreeQuotaState copyWith({int? totalMinutes, int? remainingMinutes, bool? adUnlocked}) =>
      FreeQuotaState(
        totalMinutes: totalMinutes ?? this.totalMinutes,
        remainingMinutes: remainingMinutes ?? this.remainingMinutes,
        adUnlocked: adUnlocked ?? this.adUnlocked,
      );
}

class QuotaController extends StateNotifier<FreeQuotaState> {
  QuotaController([FreeQuotaState? initial])
      : super(initial ?? const FreeQuotaState());

  /// 观看激励视频后解锁今日使用。
  void watchAd() => state = state.copyWith(adUnlocked: true);

  /// 演示重置(回到未解锁)。
  void reset() => state = const FreeQuotaState();
}

final quotaProvider = StateNotifierProvider<QuotaController, FreeQuotaState>(
  (ref) => QuotaController(),
);
