// lib/models/marine_manual_pre_departure_checklist_data.dart

import 'dart:convert';

class MarineManualPreDepartureChecklistData {
  String? reporterName;
  int? reporterUserId;
  String? submissionDate;
  // --- START: ADDED FIELD ---
  String? location;
  // --- END: ADDED FIELD ---

  // Key: Item description, Value: true if 'Yes', false if 'No'
  Map<String, bool> checklistItems = {};

  // Key: Item description, Value: Remarks text
  Map<String, String> remarks = {};

  // --- START: Added Fields ---
  String? submissionStatus;
  String? submissionMessage;
  String? reportId;
  // --- END: Added Fields ---

  MarineManualPreDepartureChecklistData();

  // --- START: ADDED METHOD ---
  /// Creates a JSON object for offline database storage.
  Map<String, dynamic> toDbJson() {
    return {
      'reporterName': reporterName,
      'reporterUserId': reporterUserId,
      'submissionDate': submissionDate,
      'location': location, // <-- ADDED
      'checklistItems': checklistItems,
      'remarks': remarks,
      'submissionStatus': submissionStatus,
      'submissionMessage': submissionMessage,
      'reportId': reportId,
    };
  }
  // --- END: ADDED METHOD ---

  // MODIFIED: This method now builds the nested array structure the PHP controller expects.
  Map<String, dynamic> toApiFormData() {

    // Create the 'items' list required by the API
    List<Map<String, dynamic>> itemsList = [];

    // Iterate over the checklist items and build the list
    checklistItems.forEach((itemName, itemChecked) {
      itemsList.add({
        'item_name': itemName,
        'item_checked': itemChecked,
        'item_remark': remarks[itemName] ?? '' // Get the corresponding remark
      });
    });

    return {
      'reporter_user_id': reporterUserId.toString(), // The controller gets this from auth, but good to send.
      'submission_date': submissionDate,
      // Note: 'location' is not sent to the API in this method,
      // but it will be saved in the local log via toDbJson().
      // If the API needs it, it must be added here.
      'items': itemsList, // Send the formatted list
    };
  }
}