import 'package:equatable/equatable.dart';
import 'package:flux/core/utils/extensions.dart';

class ModelModel extends Equatable {
  final String? id;
  final String name;
  final String nameWithBrand;
  final String brandId;
  final bool isActive;
  final DateTime? createdAt;

  const ModelModel({
    this.id,
    required this.name,
    this.nameWithBrand = '',
    required this.brandId,
    this.isActive = true,
    this.createdAt,
  });

  factory ModelModel.fromJson(Map<String, dynamic> json) {
    return ModelModel(
      id: json['id'] as String,
      name: (json['name'] as String).myFormat(),
      nameWithBrand: (json['name_with_brand'] as String).myFormat(),
      brandId: json['brand_id'] as String,
      isActive: json['is_active'] as bool? ?? true,
      createdAt: json['created_at'] != null
          ? DateTime.parse(json['created_at'])
          : null,
    );
  }

  Map<String, dynamic> toJson() {
    return {
      if (id != null) 'id': id,
      'name': name.toLowerCase().trim(),
      'brand_id': brandId,
      'is_active': isActive,
    };
  }

  ModelModel copyWith({
    String? id,
    String? name,
    String? nameWithBrand,
    String? brandId,
    bool? isActive,
    DateTime? createdAt,
  }) {
    return ModelModel(
      id: id ?? this.id,
      name: name ?? this.name,
      nameWithBrand: nameWithBrand ?? this.nameWithBrand,
      brandId: brandId ?? this.brandId,
      isActive: isActive ?? this.isActive,
      createdAt: createdAt ?? this.createdAt,
    );
  }

  @override
  List<Object?> get props => [
    id,
    name,
    nameWithBrand,
    brandId,
    isActive,
    createdAt,
  ];
}
