class TaskPlanningDetailsModel {
  final List<Building> buildings;
  final String id;
  final String name;
  final String projectAddress;
  final String contactPerson;
  final DateTime startDate;
  final DateTime endDate;
  final String projectStatusId;

  TaskPlanningDetailsModel({
    required this.buildings,
    required this.id,
    required this.name,
    required this.projectAddress,
    required this.contactPerson,
    required this.startDate,
    required this.endDate,
    required this.projectStatusId,
  });

  factory TaskPlanningDetailsModel.fromJson(Map<String, dynamic> json) {
    return TaskPlanningDetailsModel(
      buildings: (json['buildings'] as List<dynamic>?)
              ?.map((b) => Building.fromJson(b))
              .toList() ??
          [],
      id: json['id'],
      name: json['name'],
      projectAddress: json['projectAddress'],
      contactPerson: json['contactPerson'],
      startDate: DateTime.parse(json['startDate']),
      endDate: DateTime.parse(json['endDate']),
      projectStatusId: json['projectStatusId'],
    );
  }
}

class Building {
  final String id;
  final String name;
  final String description;
  final List<Floor> floors;
  final double plannedWork;
  final double completedWork; 

  Building({
    required this.id,
    required this.name,
    required this.description,
    required this.floors,
    required this.plannedWork,
    required this.completedWork,
  });

  factory Building.fromJson(Map<String, dynamic> json) {
    return Building(
      id: json['id'],
      name: json['buildingName'] ?? json['name'], 
      description: json['description'] ?? '',
      floors:
          (json['floors'] as List?)?.map((f) => Floor.fromJson(f)).toList() ??
              [],
      plannedWork: (json['plannedWork'] as num?)?.toDouble() ?? 0,
      completedWork: (json['completedWork'] as num?)?.toDouble() ?? 0,
    );
  }
}

class Floor {
  final String id;
  final String floorName;
  final List<WorkArea> workAreas;

  Floor({
    required this.id,
    required this.floorName,
    required this.workAreas,
  });

  factory Floor.fromJson(Map<String, dynamic> json) {
    return Floor(
      id: json['id'],
      floorName: json['floorName'],
      workAreas:
          (json['workAreas'] as List).map((w) => WorkArea.fromJson(w)).toList(),
    );
  }
}

class WorkArea {
  final String id;
  final String areaName;
  final List<WorkItemWrapper> workItems;

  WorkArea({
    required this.id,
    required this.areaName,
    required this.workItems,
  });

  factory WorkArea.fromJson(Map<String, dynamic> json) {
    return WorkArea(
      id: json['id'],
      areaName: json['areaName'],
      workItems: (json['workItems'] as List)
          .map((w) => WorkItemWrapper.fromJson(w))
          .toList(),
    );
  }
}

class WorkItemWrapper {
  final String workItemId;
  final WorkItem workItem;

  WorkItemWrapper({
    required this.workItemId,
    required this.workItem,
  });

  factory WorkItemWrapper.fromJson(Map<String, dynamic> json) {
    return WorkItemWrapper(
      workItemId: json['workItemId'],
      workItem: WorkItem.fromJson(json['workItem']),
    );
  }
}

class WorkItem {
  final String? id;
  final String? activityId;
  final String? workCategoryId;
  final String? workAreaId;
  final WorkAreaBasic? workArea;
  final ActivityMaster? activityMaster;
  final WorkCategoryMaster? workCategoryMaster;
  final double? plannedWork;
  final double? completedWork;
  final String? description;
  final double? todaysAssigned;
  final DateTime? taskDate;
  final String? tenantId;
  final Tenant? tenant;

  WorkItem({
    this.id,
    this.activityId,
    this.workCategoryId,
    this.workAreaId,
    this.workArea,
    this.activityMaster,
    this.workCategoryMaster,
    this.description,
    this.plannedWork,
    this.completedWork,
    this.todaysAssigned,
    this.taskDate,
    this.tenantId,
    this.tenant,
  });

  factory WorkItem.fromJson(Map<String, dynamic> json) {
    return WorkItem(
      id: json['id'] as String?,
      activityId: json['activityId'] as String?,
      workCategoryId: json['workCategoryId'] as String?,
      workAreaId: json['workAreaId'] as String?,
      workArea: json['workArea'] != null
          ? WorkAreaBasic.fromJson(json['workArea'] as Map<String, dynamic>)
          : null,
      activityMaster: json['activityMaster'] != null
          ? ActivityMaster.fromJson(
              json['activityMaster'] as Map<String, dynamic>)
          : null,
      workCategoryMaster: json['workCategoryMaster'] != null
          ? WorkCategoryMaster.fromJson(
              json['workCategoryMaster'] as Map<String, dynamic>)
          : null,
      plannedWork: json['plannedWork'] != null
          ? (json['plannedWork'] as num).toDouble()
          : null,
      completedWork: json['completedWork'] != null
          ? (json['completedWork'] as num).toDouble()
          : null,
      todaysAssigned: json['todaysAssigned'] != null
          ? (json['todaysAssigned'] as num).toDouble()
          : null,
      description: json['description'] as String?,
      taskDate:
          json['taskDate'] != null ? DateTime.tryParse(json['taskDate']) : null,
      tenantId: json['tenantId'] as String?,
      tenant: json['tenant'] != null
          ? Tenant.fromJson(json['tenant'] as Map<String, dynamic>)
          : null,
    );
  }
}

class WorkAreaBasic {
  final String? id;
  final String? name;

  WorkAreaBasic({this.id, this.name});

  factory WorkAreaBasic.fromJson(Map<String, dynamic> json) {
    return WorkAreaBasic(
      id: json['id'] as String?,
      name: json['name'] as String?,
    );
  }
}

class ActivityMaster {
  final String? id;
  final String? name;

  ActivityMaster({this.id, this.name});

  factory ActivityMaster.fromJson(Map<String, dynamic> json) {
    return ActivityMaster(
      id: json['id'] as String?,
      name: json['activityName'] as String?,
    );
  }
}

class WorkCategoryMaster {
  final String? id;
  final String? name;

  WorkCategoryMaster({this.id, this.name});

  factory WorkCategoryMaster.fromJson(Map<String, dynamic> json) {
    return WorkCategoryMaster(
      id: json['id'] as String?,
      name: json['name'] as String?,
    );
  }
}

class Tenant {
  final String? id;
  final String? name;

  Tenant({this.id, this.name});

  factory Tenant.fromJson(Map<String, dynamic> json) {
    return Tenant(
      id: json['id'] as String?,
      name: json['name'] as String?,
    );
  }
}
