import 'package:json_annotation/json_annotation.dart';

import '../../../api_response.dart';

part 'get_breakers_response.g.dart';

/// App maintenance breaker rules delivered by
/// `GET /api/mobile/v1/breaker`. Each rule blocks the listed [areas] with a
/// [message]; [BreakerRule.maxBuild] optionally scopes it to old app builds.
///
/// [rules] defaults to empty so a payload in the pre-migration shape
/// (`global`/`regional`) — which may still sit in the hydrated/cache store
/// after an update — parses to "nothing blocked" instead of throwing.
@JsonSerializable(explicitToJson: true)
class GetBreakersResponse extends ApiResponse {
  @JsonKey(defaultValue: [])
  List<BreakerRule> rules;

  GetBreakersResponse(this.rules);

  factory GetBreakersResponse.fromJson(Map<String, dynamic> json) =>
      _$GetBreakersResponseFromJson(json);
  Map<String, dynamic> toJson() => _$GetBreakersResponseToJson(this);
}

@JsonSerializable()
class BreakerRule {
  List<BreakerArea> areas;
  String message;

  /// When set, the rule only applies to app builds whose build number is
  /// &le; [maxBuild] (force-update of old versions). `null` = every build.
  int? maxBuild;

  BreakerRule(this.areas, this.message, this.maxBuild);

  factory BreakerRule.fromJson(Map<String, dynamic> json) =>
      _$BreakerRuleFromJson(json);
  Map<String, dynamic> toJson() => _$BreakerRuleToJson(this);
}

/// Blockable areas of the app. Each app module maps itself to one of these via
/// its `breakerArea` (see `app_modules.dart`). Must stay in sync with the
/// backend enum `BreakerArea` in the MarianumConnect `marMobileApi` service —
/// the backend only offers these values in the admin UI, so when a new blockable
/// module is added here, add the matching value on the backend too.
enum BreakerArea {
  @JsonValue('GLOBAL')
  global,
  @JsonValue('TIMETABLE')
  timetable,
  @JsonValue('TALK')
  talk,
  @JsonValue('FILES')
  files,
  @JsonValue('NEWS')
  news,
  @JsonValue('TICKER')
  ticker,
  @JsonValue('ROOMPLAN')
  roomPlan,
  @JsonValue('GRADES')
  grades,
  @JsonValue('HOLIDAYS')
  holidays,
  @JsonValue('DATES')
  dates,
  @JsonValue('FEEDBACK')
  feedback,
  @JsonValue('MORE')
  more,
}
