// lib/models/air_installation_data.dart

import 'dart:io';
import 'air_collection_data.dart'; // Import the collection data model

class AirInstallationData {
  String? refID;
  int? airManId; // The ID from the server database
  String? samplingDate;
  int? clientId;
  String? installationDate;
  String? installationTime;
  String? stateID;
  String? locationName;
  String? stationID;
  String? region;
  String? weather;
  String? temp;
  bool powerFailure;
  String? pm10FilterId;
  String? pm25FilterId;
  String? remark;
  int? installationUserId;

  File? imageFront;
  File? imageBack;
  File? imageLeft;
  File? imageRight;

  File? optionalImage1;
  File? optionalImage2;
  File? optionalImage3;
  File? optionalImage4;
  String? optionalRemark1;
  String? optionalRemark2;
  String? optionalRemark3;
  String? optionalRemark4;

  String? imageFrontPath;
  String? imageBackPath;
  String? imageLeftPath;
  String? imageRightPath;
  String? optionalImage1Path;
  String? optionalImage2Path;
  String? optionalImage3Path;
  String? optionalImage4Path;

  String? status;

  // NECESSARY ADDITION: For handling nested collection data during offline saves.
  AirCollectionData? collectionData;

  AirInstallationData({
    this.refID,
    this.airManId,
    this.samplingDate,
    this.clientId,
    this.installationDate,
    this.installationTime,
    this.stateID,
    this.locationName,
    this.stationID,
    this.region,
    this.weather,
    this.temp,
    this.powerFailure = false,
    this.pm10FilterId,
    this.pm25FilterId,
    this.remark,
    this.installationUserId,
    this.imageFront,
    this.imageBack,
    this.imageLeft,
    this.imageRight,
    this.optionalImage1,
    this.optionalImage2,
    this.optionalImage3,
    this.optionalImage4,
    this.optionalRemark1,
    this.optionalRemark2,
    this.optionalRemark3,
    this.optionalRemark4,
    this.imageFrontPath,
    this.imageBackPath,
    this.imageLeftPath,
    this.imageRightPath,
    this.optionalImage1Path,
    this.optionalImage2Path,
    this.optionalImage3Path,
    this.optionalImage4Path,
    this.status,
    this.collectionData, // NECESSARY ADDITION: Add to constructor
  });

  // **CRITICAL FIX**: This method now correctly includes the raw File objects
  // instead of just their paths. This allows the local storage service to find
  // and copy the installation images.
  Map<String, dynamic> toMap() {
    return {
      'refID': refID,
      'air_man_id': airManId,
      'samplingDate': samplingDate,
      'clientId': clientId,
      'installationDate': installationDate,
      'installationTime': installationTime,
      'stateID': stateID,
      'locationName': locationName,
      'stationID': stationID,
      'region': region,
      'weather': weather,
      'temp': temp,
      'powerFailure': powerFailure,
      'pm10FilterId': pm10FilterId,
      'pm25FilterId': pm25FilterId,
      'remark': remark,
      'installationUserId': installationUserId,
      'status': status,
      'optionalRemark1': optionalRemark1,
      'optionalRemark2': optionalRemark2,
      'optionalRemark3': optionalRemark3,
      'optionalRemark4': optionalRemark4,
      // Pass the actual File objects so they can be copied
      'imageFront': imageFront,
      'imageBack': imageBack,
      'imageLeft': imageLeft,
      'imageRight': imageRight,
      'optionalImage1': optionalImage1,
      'optionalImage2': optionalImage2,
      'optionalImage3': optionalImage3,
      'optionalImage4': optionalImage4,
      // Pass the nested collection data if it exists
      'collectionData': collectionData?.toMap(),
    };
  }

  factory AirInstallationData.fromJson(Map<String, dynamic> json) {
    File? fileFromPath(String? path) => (path != null && path.isNotEmpty) ? File(path) : null;

    return AirInstallationData(
      refID: json['refID'],
      airManId: json['air_man_id'],
      samplingDate: json['samplingDate'],
      clientId: json['clientId'],
      installationDate: json['installationDate'],
      installationTime: json['installationTime'],
      stateID: json['stateID'],
      locationName: json['locationName'],
      stationID: json['stationID'],
      region: json['region'],
      weather: json['weather'],
      temp: json['temp'],
      powerFailure: json['powerFailure'] ?? false,
      pm10FilterId: json['pm10FilterId'],
      pm25FilterId: json['pm25FilterId'],
      remark: json['remark'],
      installationUserId: json['installationUserId'],
      status: json['status'],
      // When deserializing, we use the '...Path' keys created by the local storage service
      imageFront: fileFromPath(json['imageFrontPath']),
      imageBack: fileFromPath(json['imageBackPath']),
      imageLeft: fileFromPath(json['imageLeftPath']),
      imageRight: fileFromPath(json['imageRightPath']),
      optionalImage1: fileFromPath(json['optionalImage1Path']),
      optionalImage2: fileFromPath(json['optionalImage2Path']),
      optionalImage3: fileFromPath(json['optionalImage3Path']),
      optionalImage4: fileFromPath(json['optionalImage4Path']),
      optionalRemark1: json['optionalRemark1'],
      optionalRemark2: json['optionalRemark2'],
      optionalRemark3: json['optionalRemark3'],
      optionalRemark4: json['optionalRemark4'],
      // NECESSARY ADDITION: Deserialize nested collection data
      collectionData: json['collectionData'] != null && json['collectionData'] is Map
          ? AirCollectionData.fromMap(json['collectionData'])
          : null,
    );
  }

  Map<String, dynamic> toJsonForApi() {
    return {
      'air_man_station_code': stationID,
      'air_man_sampling_date': samplingDate,
      'air_man_client_id': clientId?.toString(), // Ensure client ID is a string for API
      'air_man_installation_date': installationDate,
      'air_man_installation_time': installationTime,
      'air_man_installation_weather': weather,
      'air_man_installation_temperature': temp,
      'air_man_installation_power_failure': powerFailure ? 'Yes' : 'No',
      'air_man_installation_pm10_filter_id': pm10FilterId,
      'air_man_installation_pm25_filter_id': pm25FilterId,
      'air_man_installation_remarks': remark,
      'air_man_installation_image_optional_01_remarks': optionalRemark1,
      'air_man_installation_image_optional_02_remarks': optionalRemark2,
      'air_man_installation_image_optional_03_remarks': optionalRemark3,
      'air_man_installation_image_optional_04_remarks': optionalRemark4,
    };
  }

  Map<String, File> getImagesForUpload() {
    final Map<String, File> files = {};
    if (imageFront != null) files['front'] = imageFront!;
    if (imageBack != null) files['back'] = imageBack!;
    if (imageLeft != null) files['left'] = imageLeft!;
    if (imageRight != null) files['right'] = imageRight!;
    if (optionalImage1 != null) files['optional_01'] = optionalImage1!;
    if (optionalImage2 != null) files['optional_02'] = optionalImage2!;
    if (optionalImage3 != null) files['optional_03'] = optionalImage3!;
    if (optionalImage4 != null) files['optional_04'] = optionalImage4!;
    return files;
  }
}