import 'dart:developer';

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

class Building extends Base {
  Building({
    this.id,
    this.value,
    this.floors,
    String? name,
  }) : super(identifier: id?.toString() ?? '', name: name); // Handle potential null id

  Building.fromJson(dynamic json) {
    id = json['id'] ?? json['buildingId'];
    identifier = id?.toString() ?? '';
    name = json['name'] ?? json['buildingName'];
    value = json['value'];
    if (json['floors'] != null) {
      floors = [];
      json['floors'].forEach((v) {
        floors!.add(Floor.fromJson(v));
      });
    } else if (json['assetInventoryFloors'] != null) {
      floors = [];
      json['assetInventoryFloors'].forEach((v) {
        floors!.add(Floor.fromJson(v));
      });
    }
  }

  num? id;
  num? value;
  List<Floor>? floors;

  Building copyWith({
    num? id,
    String? name,
    num? value,
    List<Floor>? floors,
  }) =>
      Building(
        id: id ?? this.id,
        name: name ?? this.name,
        value: value ?? this.value,
        floors: floors ?? this.floors,
      );

  Map<String, dynamic> toJson({bool addFloor = true}) {
    final map = <String, dynamic>{};
    map['id'] = id;
    map['name'] = name;
    map['value'] = value;
    if (addFloor) {
      if (floors != null) {
        map['floors'] = floors!.map((v) => v.toJson()).toList();
      }
    }

    return map;
  }

  @override
  bool operator ==(Object other) =>
      identical(this, other) ||
      other is Building &&
          runtimeType == other.runtimeType &&
          id == other.id;

  @override
  int get hashCode => id.hashCode;
}
