part of '../analytic_dtos.dart';

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

  const factory ExclusiveSummaryDto({
    @JsonKey(name: 'organization_id') String? organizationId,
    @JsonKey(name: 'outlet_id') String? outletId,
    @JsonKey(name: 'outlet_name') String? outletName,
    @JsonKey(name: 'period') ExclusiveSummaryPeriodDto? period,
    @JsonKey(name: 'summary') ExclusiveSummarySummaryDto? summary,
    @JsonKey(name: 'reimburse') ExclusiveSummaryReimburseDto? reimburse,
    @JsonKey(name: 'hpp_breakdown')
    List<ExclusiveSummaryBreakdownDto>? hppBreakdown,
    @JsonKey(name: 'operational_expense_breakdown')
    List<ExclusiveSummaryBreakdownDto>? operationalExpenseBreakdown,
    @JsonKey(name: 'daily_summary')
    List<ExclusiveSummaryDailyDto>? dailySummary,
    @JsonKey(name: 'daily_transactions')
    List<ExclusiveSummaryTransactionDto>? dailyTransactions,
  }) = _ExclusiveSummaryDto;

  factory ExclusiveSummaryDto.fromJson(Map<String, dynamic> json) =>
      _$ExclusiveSummaryDtoFromJson(json);

  ExclusiveSummary toDomain() => ExclusiveSummary(
    organizationId: organizationId ?? '',
    outletId: outletId ?? '',
    outletName: outletName ?? '',
    period: period?.toDomain() ?? ExclusiveSummaryPeriod.empty(),
    summary: summary?.toDomain() ?? ExclusiveSummarySummary.empty(),
    reimburse: reimburse?.toDomain() ?? ExclusiveSummaryReimburse.empty(),
    hppBreakdown: hppBreakdown?.map((e) => e.toDomain()).toList() ?? [],
    operationalExpenseBreakdown:
        operationalExpenseBreakdown?.map((e) => e.toDomain()).toList() ?? [],
    dailySummary: dailySummary?.map((e) => e.toDomain()).toList() ?? [],
    dailyTransactions:
        dailyTransactions?.map((e) => e.toDomain()).toList() ?? [],
  );
}

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

  const factory ExclusiveSummaryPeriodDto({
    @JsonKey(name: 'date_from') DateTime? dateFrom,
    @JsonKey(name: 'date_to') DateTime? dateTo,
  }) = _ExclusiveSummaryPeriodDto;

  factory ExclusiveSummaryPeriodDto.fromJson(Map<String, dynamic> json) =>
      _$ExclusiveSummaryPeriodDtoFromJson(json);

  ExclusiveSummaryPeriod toDomain() => ExclusiveSummaryPeriod(
    dateFrom: dateFrom ?? DateTime.fromMillisecondsSinceEpoch(0),
    dateTo: dateTo ?? DateTime.fromMillisecondsSinceEpoch(0),
  );
}

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

  const factory ExclusiveSummarySummaryDto({
    @JsonKey(name: 'sales') num? sales,
    @JsonKey(name: 'hpp') num? hpp,
    @JsonKey(name: 'gross_profit') num? grossProfit,
    @JsonKey(name: 'salary_total') num? salaryTotal,
    @JsonKey(name: 'salary_dw') num? salaryDw,
    @JsonKey(name: 'salary_staff') num? salaryStaff,
    @JsonKey(name: 'salary_other') num? salaryOther,
    @JsonKey(name: 'other_operational_expenses') num? otherOperationalExpenses,
    @JsonKey(name: 'operational_expenses_total') num? operationalExpensesTotal,
    @JsonKey(name: 'total_cost') num? totalCost,
    @JsonKey(name: 'net_profit') num? netProfit,
  }) = _ExclusiveSummarySummaryDto;

  factory ExclusiveSummarySummaryDto.fromJson(Map<String, dynamic> json) =>
      _$ExclusiveSummarySummaryDtoFromJson(json);

  ExclusiveSummarySummary toDomain() => ExclusiveSummarySummary(
    sales: sales?.toInt() ?? 0,
    hpp: hpp?.toInt() ?? 0,
    grossProfit: grossProfit?.toInt() ?? 0,
    salaryTotal: salaryTotal?.toInt() ?? 0,
    salaryDw: salaryDw?.toInt() ?? 0,
    salaryStaff: salaryStaff?.toInt() ?? 0,
    salaryOther: salaryOther?.toInt() ?? 0,
    otherOperationalExpenses: otherOperationalExpenses?.toInt() ?? 0,
    operationalExpensesTotal: operationalExpensesTotal?.toInt() ?? 0,
    totalCost: totalCost?.toInt() ?? 0,
    netProfit: netProfit?.toInt() ?? 0,
  );
}

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

  const factory ExclusiveSummaryReimburseDto({
    @JsonKey(name: 'total_cost') num? totalCost,
    @JsonKey(name: 'excluded_salary_staff') num? excludedSalaryStaff,
    @JsonKey(name: 'total_reimburse') num? totalReimburse,
  }) = _ExclusiveSummaryReimburseDto;

  factory ExclusiveSummaryReimburseDto.fromJson(Map<String, dynamic> json) =>
      _$ExclusiveSummaryReimburseDtoFromJson(json);

  ExclusiveSummaryReimburse toDomain() => ExclusiveSummaryReimburse(
    totalCost: totalCost?.toInt() ?? 0,
    excludedSalaryStaff: excludedSalaryStaff?.toInt() ?? 0,
    totalReimburse: totalReimburse?.toInt() ?? 0,
  );
}

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

  const factory ExclusiveSummaryBreakdownDto({
    @JsonKey(name: 'category_code') String? categoryCode,
    @JsonKey(name: 'category_name') String? categoryName,
    @JsonKey(name: 'amount') num? amount,
    @JsonKey(name: 'percentage') num? percentage,
  }) = _ExclusiveSummaryBreakdownDto;

  factory ExclusiveSummaryBreakdownDto.fromJson(Map<String, dynamic> json) =>
      _$ExclusiveSummaryBreakdownDtoFromJson(json);

  ExclusiveSummaryBreakdown toDomain() => ExclusiveSummaryBreakdown(
    categoryCode: categoryCode ?? '',
    categoryName: categoryName ?? '',
    amount: amount?.toInt() ?? 0,
    percentage: percentage?.toDouble() ?? 0,
  );
}

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

  const factory ExclusiveSummaryDailyDto({
    @JsonKey(name: 'date') DateTime? date,
    @JsonKey(name: 'transaction_count') num? transactionCount,
    @JsonKey(name: 'total_cost') num? totalCost,
  }) = _ExclusiveSummaryDailyDto;

  factory ExclusiveSummaryDailyDto.fromJson(Map<String, dynamic> json) =>
      _$ExclusiveSummaryDailyDtoFromJson(json);

  ExclusiveSummaryDaily toDomain() => ExclusiveSummaryDaily(
    date: date ?? DateTime.fromMillisecondsSinceEpoch(0),
    transactionCount: transactionCount?.toInt() ?? 0,
    totalCost: totalCost?.toInt() ?? 0,
  );
}

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

  const factory ExclusiveSummaryTransactionDto({
    @JsonKey(name: 'date') DateTime? date,
    @JsonKey(name: 'category_code') String? categoryCode,
    @JsonKey(name: 'category_name') String? categoryName,
    @JsonKey(name: 'description') String? description,
    @JsonKey(name: 'amount') num? amount,
    @JsonKey(name: 'source') String? source,
  }) = _ExclusiveSummaryTransactionDto;

  factory ExclusiveSummaryTransactionDto.fromJson(Map<String, dynamic> json) =>
      _$ExclusiveSummaryTransactionDtoFromJson(json);

  ExclusiveSummaryTransaction toDomain() => ExclusiveSummaryTransaction(
    date: date ?? DateTime.fromMillisecondsSinceEpoch(0),
    categoryCode: categoryCode ?? '',
    categoryName: categoryName ?? '',
    description: description ?? '',
    amount: amount?.toInt() ?? 0,
    source: source ?? '',
  );
}
