import '../../domain/entities/logistic_object.dart';

class LogisticObjectModel extends LogisticObject {
  const LogisticObjectModel({
    required super.id,
    required super.objectId,
    required super.type,
    required super.version,
    super.locationId,
    required super.code,
    super.remark,
    required super.state,
    required super.subtype,
    super.origin,
    super.isManual,
    super.lastModified,
    super.typeName,
    super.typeMnemonic,
  });

  factory LogisticObjectModel.fromJson(Map<String, dynamic> json) {
    return LogisticObjectModel(
      id: json['id'] ?? 0,
      objectId: json['object_id'] ?? json['id'] ?? 0,
      type: json['type'] ?? 0,
      version: json['version'] ?? json['ver'] ?? 0,
      locationId: json['loc_id'],
      code: json['code'] ?? '',
      remark: json['rem'],
      state: json['state'] ?? 'unknown',
      subtype: json['subtype'] ?? json['type']?.toString() ?? '',
      origin: json['origin'],
      isManual: json['manual'] == '1' || json['manual'] == true,
      lastModified: json['last_modified'] != null
          ? DateTime.parse(json['last_modified'])
          : null,
    );
  }

  factory LogisticObjectModel.fromMap(Map<String, dynamic> map) {
    return LogisticObjectModel(
      id: map['id'] ?? 0,
      objectId: map['object_id'] ?? 0,
      type: map['type'] ?? 0,
      version: map['version'] ?? 0,
      locationId: map['loc_id'],
      code: map['code'] ?? '',
      remark: map['rem'],
      state: map['state'] ?? 'unknown',
      subtype: map['subtype'] ?? '',
      origin: map['origin'],
      isManual: map['manual'] == '1' || map['manual'] == 1,
      lastModified: map['last_modified'] != null
          ? DateTime.tryParse(map['last_modified'])
          : null,
      typeName: map['type_name'],
      typeMnemonic: map['type_mnemonic'],
    );
  }

  Map<String, dynamic> toMap() {
    return {
      'id': id,
      'object_id': objectId,
      'type': type,
      'version': version,
      'loc_id': locationId,
      'code': code,
      'rem': remark,
      'state': state,
      'subtype': subtype,
      'origin': origin,
      'manual': isManual ? 1 : 0,
      'last_modified': lastModified?.toIso8601String(),
    };
  }

  LogisticObjectModel copyWithModel({
    int? id,
    int? objectId,
    int? type,
    int? version,
    int? locationId,
    String? code,
    String? remark,
    String? state,
    String? subtype,
    String? origin,
    bool? isManual,
    DateTime? lastModified,
    String? typeName,
    String? typeMnemonic,
  }) {
    return LogisticObjectModel(
      id: id ?? this.id,
      objectId: objectId ?? this.objectId,
      type: type ?? this.type,
      version: version ?? this.version,
      locationId: locationId ?? this.locationId,
      code: code ?? this.code,
      remark: remark ?? this.remark,
      state: state ?? this.state,
      subtype: subtype ?? this.subtype,
      origin: origin ?? this.origin,
      isManual: isManual ?? this.isManual,
      lastModified: lastModified ?? this.lastModified,
      typeName: typeName ?? this.typeName,
      typeMnemonic: typeMnemonic ?? this.typeMnemonic,
    );
  }
}

class ObjectMetadataModel extends ObjectMetadata {
  const ObjectMetadataModel({
    required super.id,
    required super.type,
    required super.version,
    required super.mnemonic,
    required super.name,
    required super.prefix,
    required super.subtype,
    required super.counterText,
  });

  factory ObjectMetadataModel.fromJson(Map<String, dynamic> json) {
    return ObjectMetadataModel(
      id: json['id'] ?? 0,
      type: json['type'] ?? 0,
      version: json['version'] ?? json['ver'] ?? 0,
      mnemonic: json['mnemonic'] ?? '',
      name: json['name'] ?? '',
      prefix: json['pre'] ?? '',
      subtype: json['subtype'] ?? '',
      counterText: json['counter_text'] ?? '',
    );
  }

  factory ObjectMetadataModel.fromMap(Map<String, dynamic> map) {
    return ObjectMetadataModel(
      id: map['id'] ?? 0,
      type: map['type'] ?? 0,
      version: map['version'] ?? 0,
      mnemonic: map['mnemonic'] ?? '',
      name: map['name'] ?? '',
      prefix: map['prefix'] ?? '',
      subtype: map['subtype'] ?? '',
      counterText: map['counter_text'] ?? '',
    );
  }

  Map<String, dynamic> toMap() {
    return {
      'id': id,
      'type': type,
      'version': version,
      'mnemonic': mnemonic,
      'name': name,
      'prefix': prefix,
      'subtype': subtype,
      'counter_text': counterText,
    };
  }
}
