part of '../category_dtos.dart';

@freezed
class ListCategoryDto with _$ListCategoryDto {
  const ListCategoryDto._();

  const factory ListCategoryDto({
    @JsonKey(name: "categories") List<CategoryDto>? categories,
    @JsonKey(name: "total_count") int? totalCount,
    @JsonKey(name: "page") int? page,
    @JsonKey(name: "limit") int? limit,
    @JsonKey(name: "total_pages") int? totalPages,
  }) = _ListCategoryDto;

  factory ListCategoryDto.fromJson(Map<String, dynamic> json) =>
      _$ListCategoryDtoFromJson(json);

  ListCategory toDomain() => ListCategory(
    categories: categories?.map((dto) => dto.toDomain()).toList() ?? [],
    totalCount: totalCount ?? 0,
    page: page ?? 0,
    limit: limit ?? 0,
    totalPages: totalPages ?? 0,
  );
}

@freezed
class CategoryDto with _$CategoryDto {
  const CategoryDto._();

  const factory CategoryDto({
    @JsonKey(name: "id") String? id,
    @JsonKey(name: "organization_id") String? organizationId,
    @JsonKey(name: "name") String? name,
    @JsonKey(name: "description") String? description,
    @JsonKey(name: "business_type") String? businessType,
    @JsonKey(name: "order") int? order,
    @JsonKey(name: "metadata") Map<String, dynamic>? metadata,
    @JsonKey(name: "created_at") String? createdAt,
    @JsonKey(name: "updated_at") String? updatedAt,
  }) = _CategoryDto;

  factory CategoryDto.fromJson(Map<String, dynamic> json) =>
      _$CategoryDtoFromJson(json);

  /// Mapping ke domain
  Category toDomain() => Category(
    id: id ?? '',
    organizationId: organizationId ?? '',
    name: name ?? '',
    description: description ?? '',
    businessType: businessType ?? '',
    order: order ?? 0,
    metadata: metadata ?? {},
    createdAt: createdAt ?? '',
    updatedAt: updatedAt ?? '',
  );

  /// Mapping ke Map untuk SQLite
  Map<String, dynamic> toMap() => {
    'id': id,
    'organization_id': organizationId,
    'name': name,
    'description': description,
    'business_type': businessType,
    'sort_order': order,
    'metadata': metadata != null ? jsonEncode(metadata) : null,
    'created_at': createdAt,
    'updated_at': updatedAt,
  };

  /// Mapping dari Map SQLite
  factory CategoryDto.fromMap(Map<String, dynamic> map) => CategoryDto(
    id: map['id'] as String?,
    organizationId: map['organization_id'] as String?,
    name: map['name'] as String?,
    description: map['description'] as String?,
    businessType: map['business_type'] as String?,
    order: map['order'] as int?,
    metadata: map['metadata'] != null
        ? jsonDecode(map['metadata'] as String) as Map<String, dynamic>
        : null,
    createdAt: map['created_at'] as String?,
    updatedAt: map['updated_at'] as String?,
  );

  factory CategoryDto.fromDomain(Category category) => CategoryDto(
    id: category.id,
    organizationId: category.organizationId,
    name: category.name,
    description: category.description,
    businessType: category.businessType,
    order: category.order,
    metadata: category.metadata,
    createdAt: category.createdAt,
    updatedAt: category.updatedAt,
  );
}
