import 'package:test_sa/models/base.dart';
import 'package:test_sa/models/new_models/department.dart';

class Floor extends Base {
  Floor({
    this.id,
    String? name, // Name is now nullable
    this.value,
    this.departments,
  }) : super(identifier: id?.toString() ?? '', name: name); // Handle potentialnull id

  Floor.fromJson(dynamic json) {
    id = json['id'];
    identifier = id?.toString() ?? ''; // Handle potential null id
    name = json['name'];
    value = json['value'];
    if (json['departments'] != null) {
      departments = [];
      json['departments'].forEach((v) {
        departments!.add(Department.fromJson(v)); // Use '!' since departments is non-nullable after initialization
      });
    }
  }

  num? id; // Now nullable
  num? value; //Now nullable
  List<Department>? departments; // Now nullable

  Floor copyWith({
    num? id, // Parameters are now nullable
    String? name,
    num? value,
    List<Department>? departments,
  }) =>
      Floor(
        id: id ?? this.id,
        name: name ?? this.name,
        value: value ?? this.value,
        departments: departments ?? this.departments,
      );

  Map<String, dynamic> toJson({bool addDepartments = true}) {
    final map = <String, dynamic>{};
    map['id'] = id;
    map['name'] = name;
    map['value'] = value;
    if (addDepartments) {
      if (departments != null) {
        map['departments'] = departments!.map((v) => v.toJson()).toList(); // Use '!' since departments could be null
      }
    }
    return map;
  }
}
