class AnnouncementModel {
  final String id;
  final String title;
  final String content;
  final String contentType;
  final String type; // notice / policy / activity
  final String status; // draft, published
  final String publisherId;
  final String publisherName;
  final DateTime publishTime;
  final bool isTop;
  final int privateLevel; // 0=全员, 1=按部门, 2=按指定用户
  final List<String> targetDepts;
  final List<String> targetUsers;
  final DateTime? expiryDate;
  final List<String> attachments;
  final int readCount;
  final int unreadCount;
  final bool isRead;
  final DateTime createTime;
  final DateTime? updateTime;

  const AnnouncementModel({
    required this.id,
    required this.title,
    required this.content,
    this.contentType = 'text',
    this.type = 'notice',
    this.status = 'published',
    required this.publisherId,
    this.publisherName = '',
    required this.publishTime,
    this.isTop = false,
    this.privateLevel = 0,
    this.targetDepts = const [],
    this.targetUsers = const [],
    this.expiryDate,
    this.attachments = const [],
    this.readCount = 0,
    this.unreadCount = 0,
    this.isRead = false,
    required this.createTime,
    this.updateTime,
  });

  bool get isExpired {
    if (expiryDate == null) return false;
    return DateTime.now().isAfter(expiryDate!);
  }

  String get typeLabel {
    switch (type) {
      case 'notice':
        return '通知公告';
      case 'policy':
        return '人事与制度';
      case 'activity':
        return '放假与活动';
      default:
        return type;
    }
  }

  bool get isDraft => status == 'draft';

  factory AnnouncementModel.fromJson(Map<String, dynamic> json) {
    return AnnouncementModel(
      id: json['id'] as String,
      title: json['title'] as String,
      content: json['content'] as String? ?? '',
      contentType: json['contentType'] as String? ?? 'text',
      type: json['type'] as String? ?? 'notice',
      status: json['status'] as String? ?? 'published',
      publisherId: json['publisherId'] as String? ?? '',
      publisherName: json['publisherName'] as String? ?? '',
      publishTime: DateTime.parse(json['publishTime'] as String),
      isTop: json['isTop'] as bool? ?? false,
      privateLevel: json['privateLevel'] as int? ?? 0,
      targetDepts:
          (json['targetDepts'] as List<dynamic>?)
              ?.map((e) => e as String)
              .toList() ??
          [],
      targetUsers:
          (json['targetUsers'] as List<dynamic>?)
              ?.map((e) => e as String)
              .toList() ??
          [],
      expiryDate: json['expiryDate'] != null
          ? DateTime.parse(json['expiryDate'] as String)
          : null,
      attachments:
          (json['attachments'] as List<dynamic>?)
              ?.map((e) => e as String)
              .toList() ??
          [],
      readCount: json['readCount'] as int? ?? 0,
      unreadCount: json['unreadCount'] as int? ?? 0,
      isRead: json['isRead'] as bool? ?? false,
      createTime: DateTime.parse(json['createTime'] as String),
      updateTime: json['updateTime'] != null
          ? DateTime.parse(json['updateTime'] as String)
          : null,
    );
  }
}
