import 'package:flutter/material.dart';

/// App Header 数据模型 — 来自 /admin/cms/app-header/active-list 接口
class AppHeaderItem {
  final int id;
  final String title;
  final String mediaType;      // "image" | "video"
  final int displayType;       // 0=通用(用mediaUrl), 1=日夜分离(用dayMediaUrl/nightMediaUrl)
  final String mediaUrl;       // 通用媒体地址 (displayType=0 时使用)
  final String dayMediaUrl;    // 日间媒体地址 (displayType=1 时使用)
  final String nightMediaUrl;  // 夜间媒体地址 (displayType=1 时使用)
  final String linkType;       // "internal" | "external"
  final String linkUrl;
  final int sort;

  const AppHeaderItem({
    required this.id,
    required this.title,
    required this.mediaType,
    required this.displayType,
    this.mediaUrl = '',
    this.dayMediaUrl = '',
    this.nightMediaUrl = '',
    this.linkType = 'internal',
    this.linkUrl = '',
    this.sort = 0,
  });

  /// 根据 Brightness 返回正确的媒体 URL
  String resolveUrl(Brightness brightness) {
    if (displayType == 0) return mediaUrl;
    return brightness == Brightness.dark ? nightMediaUrl : dayMediaUrl;
  }

  bool get isVideo => mediaType == 'video';
  bool get isImage => mediaType == 'image';
  bool get isExternal => linkType == 'external';
  bool get isInternal => linkType == 'internal';

  factory AppHeaderItem.fromJson(Map<String, dynamic> json) {
    return AppHeaderItem(
      id: json['id'] as int? ?? 0,
      title: json['title'] as String? ?? '',
      mediaType: json['mediaType'] as String? ?? 'image',
      displayType: json['displayType'] as int? ?? 0,
      mediaUrl: json['mediaUrl'] as String? ?? '',
      dayMediaUrl: json['dayMediaUrl'] as String? ?? '',
      nightMediaUrl: json['nightMediaUrl'] as String? ?? '',
      linkType: json['linkType'] as String? ?? 'internal',
      linkUrl: json['linkUrl'] as String? ?? '',
      sort: json['sort'] as int? ?? 0,
    );
  }
}
