<?php
include_once('init.php');

class UserControllerProxy
{
  public function Login($data)
  {
    return call_api("b_user", "login", $data);
  }
  public function check_sesi($data)
  {
    return call_api("b_user", "check_sesi", $data);
  }
  public function Logout($data)
  {
    return call_api("b_user", "logout", $data);
  }
  public function Insert($data)
  {
    return call_api("user", "Insert", $data);
  }
  public function Update($data)
  {
    return call_api("user", "Update", $data);
  }
  public function Delete($data)
  {
    return call_api("user", "Delete", $data);
  }
  public function Select($data)
  {
    return call_api("user", "Select", $data);
  }
}

// Membersihkan input sederhana
function clean_text($text)
{
  return htmlspecialchars(trim($text), ENT_QUOTES, 'UTF-8');
}

// Validasi field wajib sesuai action
function validate_required_fields($action, $data)
{
  $requiredFields = [
    'Login' => ['Email', 'PasswordHash'],
    'Insert' => ['Email', 'Password'],
    'Update' => ['ID'],
    'Delete' => ['ID'],
    'Select' => [], // bisa ditambahkan jika perlu
  ];

  $missing = [];
  if (isset($requiredFields[$action])) {
    foreach ($requiredFields[$action] as $field) {
      if (!isset($data[$field]) || trim($data[$field]) === '') {
        $missing[] = $field;
      }
    }
  }

  if (count($missing) > 0) {
    return [
      'Status' => 0,
      'Keterangan' => 'Field tidak boleh kosong: ' . implode(', ', $missing)
    ];
  }

  return null;
}

// Menangkap data utama POST, bisa dikembangkan untuk $_FILES
function main_data($action)
{
  global $globalTitle, $glovalVersion, $IsWeb;

  $ip_address = $_SERVER['REMOTE_ADDR'] ?? '';
  $yourbrowser = $_SERVER['HTTP_USER_AGENT'] ?? '';

  $data = [
    'ID' => $_POST['ID'] ?? '',
    'Email' => clean_text($_POST['Email'] ?? ''),
    'UserLogin' => clean_text($_POST['Username'] ?? ''),
    'PasswordHash' => $_POST['Password'] ?? '',
    'PasswordBaru2' => $_POST['PasswordBaru2'] ?? '',
    'PasswordBaru' => $_POST['PasswordBaru'] ?? '',
    'UID_F' => $_POST['UID_F'] ?? '',
    'CheckProviderUser' => $_POST['CheckProviderUser'] ?? '', 
    'DeviceInfo' => $_POST['device_info'] ?? '',
    'NamaApp' => $globalTitle,
    'Version' => $glovalVersion,
    'IP' => $ip_address,
    'DeviceData' => $yourbrowser,
    'IsWeb' => $IsWeb,
    'type' => $_POST['type'] ?? '',
    'u_token' => $_POST['u_token'] ?? '',
  ];


 
  // Encrypt password untuk login
  if ($action === 'Login') { 
    if (!empty($data['Password'])) {
      //$data['Password'] = password_hash($data['Password'], PASSWORD_BCRYPT);
    }

  }

  return $data;
}

// Ambil action
$action = $_POST['action'] ?? '';
$UserController = new UserControllerProxy();

// Tangkap dan proses data
$data = main_data($action);

// Validasi field wajib
$validationResult = validate_required_fields($action, $data);
if ($validationResult !== null) {
  header('Content-Type: application/json');
  echo json_encode($validationResult);
  exit;
}

// Eksekusi sesuai action
switch ($action) {
  case 'Login':
 //$result = ["Status" => 0, "Keterangan" => " data = " . json_encode($data)];

    $result = $UserController->Login($data);
    break;
  case 'check_sesi':
    $result = $UserController->check_sesi($data);
    break;
  case 'Logout':
    $result = $UserController->Logout($data);
    break;
  case 'Insert':
    $result = $UserController->Insert($data);
    break;
  case 'Update':
    $result = $UserController->Update($data);
    break;
  case 'Delete':
    $result = $UserController->Delete($data);
    break;
  case 'Select':
    $result = $UserController->Select($data);
    break;
  default:
    $result = ["Status" => 0, "Keterangan" => "Action tidak dikenal."];
    break;
}

header('Content-Type: application/json');
echo json_encode($result);
