import 'package:json_annotation/json_annotation.dart';

part 'get_ticker_page_response.g.dart';

/// Canonical ticker page kinds. Kept as plain strings on the wire so an unknown
/// future kind never breaks JSON parsing.
abstract class TickerPageKind {
  static const String content = 'CONTENT';
  static const String redirect = 'REDIRECT';
  static const String proxiedFile = 'PROXIED_FILE';
}

/// A single ticker page from `GET /api/mobile/v1/ticker/pages/{slug}`.
///
/// The populated fields depend on [kind]:
/// - `CONTENT` → [content] (nested ProseMirror document)
/// - `REDIRECT` → [externalUrl]
/// - `PROXIED_FILE` → [fileUrl], [contentType], [filename]
@JsonSerializable()
class TickerPageResponse {
  @JsonKey(defaultValue: 1)
  final int schemaVersion;

  final String? slug;
  final String? title;

  @JsonKey(defaultValue: 'CONTENT')
  final String kind;

  final Map<String, dynamic>? content;

  final String? externalUrl;
  final bool? externalUrlNewTab;

  final String? fileUrl;
  final String? contentType;
  final String? filename;

  final String? hash;
  final String? webUrl;

  /// ISO timestamp the page was last published/updated (`ticker_pages.published_at`),
  /// shown as "Aktualisiert am …" — the same date the web view displays. Null
  /// when the page has never been published.
  final String? publishedAt;

  /// PROXIED_FILE only: ISO timestamp of the last successful re-fetch of the
  /// file by the server proxy (`ticker_pages.proxy_last_success_at`) — the
  /// document's data currency, shown as "Aktualisiert am …" instead of
  /// [publishedAt]. Null for other kinds / files never fetched yet.
  final String? fileFetchedAt;

  TickerPageResponse({
    required this.schemaVersion,
    this.slug,
    this.title,
    required this.kind,
    this.content,
    this.externalUrl,
    this.externalUrlNewTab,
    this.fileUrl,
    this.contentType,
    this.filename,
    this.hash,
    this.webUrl,
    this.publishedAt,
    this.fileFetchedAt,
  });

  factory TickerPageResponse.fromJson(Map<String, dynamic> json) =>
      _$TickerPageResponseFromJson(json);
  Map<String, dynamic> toJson() => _$TickerPageResponseToJson(this);
}
