import 'package:test_sa/models/lookup.dart';
import 'package:test_sa/modules/internal_audit_module/models/engineer_data_model.dart';
import 'package:test_sa/models/generic_attachment_model.dart';
import 'package:test_sa/modules/internal_audit_module/models/system_internal_audit_form_model.dart';

class SystemInternalAuditDataModel {
  int? id;
  int? assetGroupId;
  String? requestNo;
  int? requestNoSequence;
  Auditor? auditor;
  Lookup? status;
  Lookup? findingType;
  String? findingDescription;
  Lookup? workOrderType;
  Auditor? assignEmployee;
  SystemAuditWorkOrderDetailModel? workOrderDetails; // Already exists as a separate model
  String? createdBy;
  String? createdDate;
  String? modifiedBy;
  String? modifiedDate;
  List<GenericAttachmentModel>? attachments;
  EngineerData? engineerData;

  SystemInternalAuditDataModel({
    this.id,
    this.assetGroupId,
    this.requestNo,
    this.requestNoSequence,
    this.auditor,
    this.status,
    this.findingType,
    this.findingDescription,
    this.workOrderType,
    this.assignEmployee,
    this.workOrderDetails,
    this.createdBy,
    this.createdDate,
    this.modifiedBy,
    this.modifiedDate,
    this.attachments,
    this.engineerData,
  });

  SystemInternalAuditDataModel.fromJson(Map<String, dynamic> json) {
    id = json['id'];
    assetGroupId = json['assetGroupId'];
    requestNo = json['requestNo'];
    requestNoSequence = json['requestNoSequence'];
    auditor = json['auditor'] != null ? Auditor.fromJson(json['auditor']) : null;
    status = json['status'] != null ? Lookup.fromJson(json['status']) : null;
    findingType = json['findingType'] != null ? Lookup.fromJson(json['findingType']) : null;
    findingDescription = json['findingDescription'];
    workOrderType = json['workOrderType'] != null ? Lookup.fromJson(json['workOrderType']) : null;
    assignEmployee = json['assignEmployee'] != null ? Auditor.fromJson(json['assignEmployee']) : null;
    workOrderDetails = json['workOrderDetails'] != null ? SystemAuditWorkOrderDetailModel.fromJson(json['workOrderDetails']) : null; // Keep as-is
    createdBy = json['createdBy'];
    createdDate = json['createdDate'];
    modifiedBy = json['modifiedBy'];
    modifiedDate = json['modifiedDate'];
    attachments = json['attachments'] != null
        ? (json['attachments'] as List)
        .map((e) => GenericAttachmentModel.fromInternalAuditJson(e))
        .where((e) => e.name != null) // optional filter if you want to skip null names
        .toList()
        : [];
    engineerData = json['engineerData'] != null ? EngineerData.fromJson(json['engineerData']) : null;
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = {};
    data['id'] = id;
    data['assetGroupId'] = assetGroupId;
    data['requestNo'] = requestNo;
    data['requestNoSequence'] = requestNoSequence;
    if (auditor != null) data['auditor'] = auditor!.toJson();
    if (status != null) data['status'] = status!.toJson();
    if (findingType != null) data['findingType'] = findingType!.toJson();
    data['findingDescription'] = findingDescription;
    if (workOrderType != null) data['workOrderType'] = workOrderType!.toJson();
    if (assignEmployee != null) data['assignEmployee'] = assignEmployee!.toJson();
    data['workOrderDetails'] = workOrderDetails;
    data['createdBy'] = createdBy;
    data['createdDate'] = createdDate;
    data['modifiedBy'] = modifiedBy;
    data['modifiedDate'] = modifiedDate;
    if (attachments != null) {
      data['attachments'] = attachments!.whereType<GenericAttachmentModel>().map((v) => v.toInternalAuditJson()).toList();
    }
    if (engineerData != null) data['engineerData'] = engineerData!.toJson();
    return data;
  }
}

class Auditor {
  String? userId;
  String? userName;
  String? email;
  String? employeeId;
  int? languageId;
  String? extensionNo;
  String? phoneNumber;
  bool? isActive;

  Auditor({
    this.userId,
    this.userName,
    this.email,
    this.employeeId,
    this.languageId,
    this.extensionNo,
    this.phoneNumber,
    this.isActive,
  });

  Auditor.fromJson(Map<String, dynamic> json) {
    userId = json['userId'];
    userName = json['userName'];
    email = json['email'];
    employeeId = json['employeeId'];
    languageId = json['languageId'];
    extensionNo = json['extensionNo'];
    phoneNumber = json['phoneNumber'];
    isActive = json['isActive'];
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = {};
    data['userId'] = userId;
    data['userName'] = userName;
    data['email'] = email;
    data['employeeId'] = employeeId;
    data['languageId'] = languageId;
    data['extensionNo'] = extensionNo;
    data['phoneNumber'] = phoneNumber;
    data['isActive'] = isActive;
    return data;
  }
}


