import 'package:test_sa/models/base.dart';
import 'package:test_sa/models/generic_attachment_model.dart';
import 'package:test_sa/models/lookup.dart';

class MedicalGasDataModel {
  int? id;
  String? requestNumber;
  String? requestedBy;
  String? requestedDate;
  int? requestOrderTypeId;
  String? requestOrderType;
  int? itemTypeId;
  String? itemType;
  int? siteId;
  String? site;
  int? supplierId;
  String? supplier;
  String? contact;
  String? email;
  bool? isSubmitted;
  String? requestOrderStatus;
  bool? isPressureChecked;
  bool? isInspectedGasLeak;
  Lookup? requestStatus;
  DateTime? deliveredDate;
  String? deliveryNote;
  String? lastModifiedBy;

  /// ✅ non-nullable lists
  List<MedicalGasRequestDetailModel> requestDetailDtos = [];
  List<GenericAttachmentModel> deliveryNoteAttachmentDto = [];

  MedicalGasDataModel({
    this.id,
    this.requestNumber,
    this.requestedBy,
    this.requestedDate,
    this.requestOrderTypeId,
    this.requestOrderType,
    this.itemTypeId,
    this.itemType,
    this.siteId,
    this.site,
    this.requestStatus,
    this.supplierId,
    this.supplier,
    this.contact,
    this.email,
    this.isSubmitted,
    this.requestOrderStatus,
    this.isPressureChecked,
    this.isInspectedGasLeak,
    this.deliveredDate,
    this.deliveryNote,
    this.lastModifiedBy,
    List<MedicalGasRequestDetailModel>? requestDetailDtos,
    List<GenericAttachmentModel>? deliveryNoteAttachmentDto,
  }) {
    this.requestDetailDtos = requestDetailDtos ?? [];
    this.deliveryNoteAttachmentDto = deliveryNoteAttachmentDto ?? [];
  }

  MedicalGasDataModel.fromJson(Map<String, dynamic> json) {
    id = json['id'];
    requestNumber = json['requestNumber'];
    requestedBy = json['requestedBy'];
    requestedDate = json['requestedDate'];
    requestOrderTypeId = json['requestOrderTypeId'];
    requestOrderType = json['requestOrderType'];
    itemTypeId = json['itemTypeId'];
    itemType = json['itemType'];
    siteId = json['siteId'];
    site = json['site'];
    supplierId = json['supplierId'];
    supplier = json['supplier'];
    contact = json['contact'];
    email = json['email'];
    isSubmitted = json['isSubmitted'];
    deliveryNote = json['deliveryNote'];
    lastModifiedBy = json['lastModifiedBy'];
    requestOrderStatus = json['requestOrderStatus'];
    isPressureChecked = json['isPressureChecked'];
    isInspectedGasLeak = json['isInspectedGasLeak'];

    deliveredDate = json['deliveredDate'] != null ? DateTime.parse(json['deliveredDate']) : null;
    requestStatus = json['requestOrderStatusDto'] != null ? Lookup.fromJson(json['requestOrderStatusDto']) : null;
    if (json['requestDetailDtos'] != null) {
      requestDetailDtos = [];
      json['requestDetailDtos'].forEach((v) {
        requestDetailDtos.add(MedicalGasRequestDetailModel.fromJson(v));
      });
    }

    if (json['deliveryNoteAttachmentDto'] != null) {
      deliveryNoteAttachmentDto = [];
      json['deliveryNoteAttachmentDto'].forEach((v) {
        deliveryNoteAttachmentDto.add(GenericAttachmentModel.fromJson(v));
      });
    }
  }

  Map<String, dynamic> toJson() {
    return {
      'id': id,
      'requestNumber': requestNumber,
      'requestedBy': requestedBy,
      'requestedDate': requestedDate,
      'requestOrderTypeId': requestOrderTypeId,
      'requestOrderType': requestOrderType,
      'itemTypeId': itemTypeId,
      'itemType': itemType,
      'siteId': siteId,
      'site': site,
      'supplierId': supplierId,
      'deliveryNote': deliveryNote,
      'lastModifiedBy': lastModifiedBy,
      'supplier': supplier,
      'contact': contact,
      'email': email,
      'isSubmitted': isSubmitted,
      'requestOrderStatus': requestOrderStatus,
      'isPressureChecked': isPressureChecked,
      'isInspectedGasLeak': isInspectedGasLeak,
      'deliveredDate': deliveredDate?.toIso8601String(),
      'requestDetailDtos': requestDetailDtos.map((e) => e.toJson()).toList(),
      'requestOrderStatusDto': requestStatus?.toJson(),
      'deliveryNoteAttachmentDto': deliveryNoteAttachmentDto.map((e) => e.toJson()).toList(),
    };
  }
}

class MedicalGasRequestDetailModel extends Base {
  int? id;
  int? medicalGasAttributeId;
  String? itemName;
  String? oracleName;
  String? oracleCode;
  num? requestedQuantity;
  num? deliveredQuantity;

  MedicalGasRequestDetailModel({
    this.id,
    this.medicalGasAttributeId,
    this.itemName,
    this.oracleName,
    this.oracleCode,
    this.requestedQuantity,
    this.deliveredQuantity,
  }) : super(identifier: id?.toString(), name: itemName);

  MedicalGasRequestDetailModel.fromJson(Map<String, dynamic> json) {
    id = json['id'];
    medicalGasAttributeId = json['medicalGasAttributeId'];
    itemName = json['itemName'];
    identifier = id.toString();
    name = itemName;
    oracleName = json['oracleName'];
    oracleCode = json['oracleCode'];
    requestedQuantity = json['requestedQuantity'];
    deliveredQuantity = json['deliveredQuantity'];
  }

  Map<String, dynamic> toJson() {
    return {
      'id': id,
      'medicalGasAttributeId': medicalGasAttributeId,
      'itemName': itemName,
      'oracleName': oracleName,
      'oracleCode': oracleCode,
      'requestedQuantity': requestedQuantity,
      'deliveredQuantity': deliveredQuantity,
    };
  }
}
