import 'dart:convert';

class CategoryResponseModel {
  final bool success;
  final CategoryData data;
  final dynamic errors;

  CategoryResponseModel({
    required this.success,
    required this.data,
    this.errors,
  });

  factory CategoryResponseModel.fromMap(Map<String, dynamic> map) {
    return CategoryResponseModel(
      success: map['success'] as bool,
      data: CategoryData.fromMap(map['data'] as Map<String, dynamic>),
      errors: map['errors'],
    );
  }

  factory CategoryResponseModel.fromJson(String str) =>
      CategoryResponseModel.fromMap(json.decode(str));

  Map<String, dynamic> toMap() {
    return {
      'success': success,
      'data': data.toMap(),
      'errors': errors,
    };
  }

  String toJson() => json.encode(toMap());
}

class CategoryData {
  final List<CategoryModel> categories;
  final int totalCount;
  final int page;
  final int limit;
  final int totalPages;

  CategoryData({
    required this.categories,
    required this.totalCount,
    required this.page,
    required this.limit,
    required this.totalPages,
  });

  factory CategoryData.fromMap(Map<String, dynamic> map) {
    return CategoryData(
      categories: List<CategoryModel>.from(
        (map['categories'] as List).map((x) => CategoryModel.fromMap(x)),
      ),
      totalCount: map['total_count'] as int,
      page: map['page'] as int,
      limit: map['limit'] as int,
      totalPages: map['total_pages'] as int,
    );
  }

  Map<String, dynamic> toMap() {
    return {
      'categories': categories.map((x) => x.toMap()).toList(),
      'total_count': totalCount,
      'page': page,
      'limit': limit,
      'total_pages': totalPages,
    };
  }
}

class CategoryModel {
  String id;
  final String organizationId;
  final String name;
  final String? description;
  final String businessType;
  final Map<String, dynamic> metadata;
  final DateTime createdAt;
  final DateTime updatedAt;

  CategoryModel({
    required this.id,
    required this.organizationId,
    required this.name,
    this.description,
    required this.businessType,
    required this.metadata,
    required this.createdAt,
    required this.updatedAt,
  });

  factory CategoryModel.fromMap(Map<String, dynamic> map) {
    return CategoryModel(
      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,
      metadata: Map<String, dynamic>.from(map['metadata'] ?? {}),
      createdAt: DateTime.parse(map['created_at'] as String),
      updatedAt: DateTime.parse(map['updated_at'] as String),
    );
  }

  Map<String, dynamic> toMap() {
    return {
      'id': id,
      'organization_id': organizationId,
      'name': name,
      'description': description,
      'business_type': businessType,
      'metadata': metadata,
      'created_at': createdAt.toIso8601String(),
      'updated_at': updatedAt.toIso8601String(),
    };
  }

  factory CategoryModel.all() => CategoryModel(
        id: 'all',
        organizationId: '',
        name: 'Semua',
        businessType: 'restaurant',
        metadata: {},
        createdAt: DateTime.now(),
        updatedAt: DateTime.now(),
      );
}
