/// Data Model: Blog Category
///
/// Data Transfer Object for blog/news category information from Frappe API.
/// This model handles JSON serialization/deserialization for API responses.
library;

import 'package:json_annotation/json_annotation.dart';
import 'package:worker/features/news/domain/entities/blog_category.dart';

part 'blog_category_model.g.dart';

/// Blog Category Model
///
/// Used for:
/// - API JSON serialization/deserialization
/// - Converting to/from domain entity
///
/// Example API response:
/// ```json
/// {
///   "title": "Tin tức",
///   "name": "tin-tức"
/// }
/// ```
@JsonSerializable()
class BlogCategoryModel {
  /// Display title of the category (e.g., "Tin tức", "Chuyên môn")
  final String title;

  /// URL-safe name/slug of the category (e.g., "tin-tức", "chuyên-môn")
  final String name;

  const BlogCategoryModel({
    required this.title,
    required this.name,
  });

  /// From JSON constructor
  factory BlogCategoryModel.fromJson(Map<String, dynamic> json) =>
      _$BlogCategoryModelFromJson(json);

  /// To JSON method
  Map<String, dynamic> toJson() => _$BlogCategoryModelToJson(this);

  /// Convert to domain entity
  BlogCategory toEntity() {
    return BlogCategory(
      title: title,
      name: name,
    );
  }

  /// Create from domain entity
  factory BlogCategoryModel.fromEntity(BlogCategory entity) {
    return BlogCategoryModel(
      title: entity.title,
      name: entity.name,
    );
  }

  /// Copy with method for creating modified copies
  BlogCategoryModel copyWith({
    String? title,
    String? name,
  }) {
    return BlogCategoryModel(
      title: title ?? this.title,
      name: name ?? this.name,
    );
  }

  @override
  String toString() {
    return 'BlogCategoryModel(title: $title, name: $name)';
  }

  @override
  bool operator ==(Object other) {
    if (identical(this, other)) return true;

    return other is BlogCategoryModel &&
        other.title == title &&
        other.name == name;
  }

  @override
  int get hashCode {
    return Object.hash(title, name);
  }
}

/// API Response wrapper for blog categories list
///
/// Frappe API wraps the response in a "message" field.
/// Example:
/// ```json
/// {
///   "message": [
///     {"title": "Tin tức", "name": "tin-tức"},
///     {"title": "Chuyên môn", "name": "chuyên-môn"}
///   ]
/// }
/// ```
class BlogCategoriesResponse {
  /// List of blog categories
  final List<BlogCategoryModel> message;

  BlogCategoriesResponse({
    required this.message,
  });

  /// From JSON constructor
  factory BlogCategoriesResponse.fromJson(Map<String, dynamic> json) {
    final messageList = json['message'] as List<dynamic>;
    final categories = messageList
        .map((item) => BlogCategoryModel.fromJson(item as Map<String, dynamic>))
        .toList();

    return BlogCategoriesResponse(message: categories);
  }

  /// To JSON method
  Map<String, dynamic> toJson() {
    return {
      'message': message.map((category) => category.toJson()).toList(),
    };
  }
}
