/**
 * Google Apps Script - Opel Lifts Attendance
 * Deploy as Web App (Execute as: Me, Access: Anyone)
 * 
 * Sheet columns: Date | Employee ID | Name | Action | Time | Location URL | Latitude | Longitude | IP Address | Auto Logout
 */

function doPost(e) {
  try {
    var data = JSON.parse(e.postData.contents);
    var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
    
    sheet.appendRow([
      data.date || '',
      data.employee_id || '',
      data.name || '',
      data.action || '',
      data.time || '',
      data.location_url || '',
      data.latitude || '',
      data.longitude || '',
      data.ip_address || '',
      data.auto_logout || 'No'
    ]);
    
    return ContentService.createTextOutput(
      JSON.stringify({ status: 'success', message: 'Row appended' })
    ).setMimeType(ContentService.MimeType.JSON);
    
  } catch (error) {
    return ContentService.createTextOutput(
      JSON.stringify({ status: 'error', message: error.toString() })
    ).setMimeType(ContentService.MimeType.JSON);
  }
}

function doGet(e) {
  return ContentService.createTextOutput(
    JSON.stringify({ status: 'ok', message: 'Opel Lifts Attendance API' })
  ).setMimeType(ContentService.MimeType.JSON);
}
