part of 'profit_sharing_loader_bloc.dart';

/// Tampilan rincian bagi hasil: per minggu atau per bulan.
enum ProfitSharingPeriodType { weekly, monthly }

@freezed
class ProfitSharingLoaderState with _$ProfitSharingLoaderState {
  const ProfitSharingLoaderState._();

  const factory ProfitSharingLoaderState({
    required ProfitSharing profitSharing,
    required Option<AnalyticFailure> failureOption,
    @Default(false) bool isFetching,
    @Default(ProfitSharingPeriodType.weekly) ProfitSharingPeriodType periodType,
    required DateTime dateFrom,
    required DateTime dateTo,
  }) = _ProfitSharingLoaderState;

  factory ProfitSharingLoaderState.initial() {
    final now = DateTime.now();
    return ProfitSharingLoaderState(
      profitSharing: ProfitSharing.empty(),
      failureOption: none(),
      dateFrom: DateTime(now.year, now.month, 1),
      dateTo: DateTime(now.year, now.month + 1, 0),
    );
  }

  /// Baris periode sesuai tab yang dipilih, periode kosong disembunyikan.
  List<ProfitSharingPeriod> get periods {
    final budget = profitSharing.budget;
    final list = periodType == ProfitSharingPeriodType.weekly
        ? budget.weekly
        : budget.monthly;
    return list.where((e) => e.hasRevenue).toList();
  }
}
