<?php

// ====== GET parameter ======
$passport = isset($_GET['passport']) ? trim($_GET['passport']) : '';

// ====== JSON payload ======
$payload = json_encode([
  "page" => "1",
  "sort" => "created_at",
  "type" => "asc",
  "query" => "",
  "item" => "10",
  "status" => "",
  "country_id" => "",
  "job_category_id" => "",
  "education_degree" => "",
  "filter_type" => "advance",
  "gender" => "",
  "passport_status" => "",
  "bmet_status" => "Yes",
  "training_course" => "",
  "passports" => $passport,
  "bmet_date" => "",
  "district_id" => "",
  "from" => "",
  "to" => "",
  "employment_status" => "",
  "education_filter" => ""
]);

// ====== cURL Request ======
$curl = curl_init();

curl_setopt_array($curl, [
  CURLOPT_URL => 'https://moewoe.amiprobashi.com/recruiting-agency/skill/skill_search',
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => '',
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 30,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => 'POST',
  CURLOPT_POSTFIELDS => $payload,
  CURLOPT_COOKIE => 'django_session_id=p5kkov6yuglfb2lojm1palcv7727r247; XSRF-TOKEN=eyJpdiI6IjBEdFZCS1lZYmk3SWJCdVg2N2laTUE9PSIsInZhbHVlIjoiRkpuR3gzSk9yditHSWpJT3E1YWRWNUsyTndlYmljK1VydDFFbFJiZDc2a3lFOU1IcVpyMUhRTkp5OUpyRjQyUyIsIm1hYyI6IjFjODgzZmZjNjc1MjU1ZTVmNTdjYzQ5MDRiOTY2ZmQ5MGVmMTUzNTVjMDU3MTZkYzFkNjk0YmM4NjkxZDA4ZWEifQ%3D%3D; amiprobashi_session=eyJpdiI6InRVRUQ1WmFaMDdsQVZjR1FhdzU3c3c9PSIsInZhbHVlIjoidHlzMU5oQ2JERVVNSnhzNGxkZStjTmpmV1JaK0lKREYwQlFKcXhncmx4NmZNek5QWkdDZ0thZndaSFgrWklxWCIsIm1hYyI6ImI1ZWQzYmM2NzA5MGM4NWJiMzczZmFkNzE4OTNjOWQ3MTViY2UxNDBjNGJmODM1NDFkMDFhNzU4YzNjZTI3N2MifQ%3D%3D',
  CURLOPT_HTTPHEADER => [
    'User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/116.0.0.0 Safari/537.36',
    'Accept: application/json, text/plain, */*',
    'Accept-Encoding: gzip, deflate, br, zstd',
    'Content-Type: application/json',
    'x-xsrf-token: eyJpdiI6IjdvMkpwXC9DdUU1RmpqYjk1cmJrVjd3PT0iLCJ2YWx1ZSI6IktBd1cwSHNXTkkwUEJFeFwvUWhHenJHMzNhMnU5T3FlTVhhU2grQWtzNDNQNGhJS1VTMVZ5SkVpdXptM3hVOUR1IiwibWFjIjoiZWExN2M4MmU4YzgyOTk3ZTRiNmE5NzUyNjQyMTZmMzUwZmM0ZTk1ODZhYjA1NDM0YTQwYzA0MjFkMmNlNTMyMCJ9',
    'x-requested-with: XMLHttpRequest',
    'origin: https://moewoe.amiprobashi.com',
    'referer: https://moewoe.amiprobashi.com/admin/agency/bmet-database/skill_search',
  ],
]);

$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);

// ====== Output ======
header('Content-Type: application/json; charset=utf-8');

if ($err) {
  echo json_encode(['error' => $err]);
  exit;
}

// Decode response
$data = json_decode($response, true);

// যদি data না থাকে
if (!isset($data['data'][0])) {
  echo json_encode(['error' => 'No data found for this passport']);
  exit;
}

// ====== Main Data ======
$info = $data['data'][0];
$id = $info['id'] ?? null;

// ====== Second API call (api1.php?id=) ======
$api1_url = "api.blackfiretools.xyz/passport/api1.php?id=" . urlencode($id); // <-- এখানে তোমার আসল api1.php এর URL দাও

$ch = curl_init($api1_url);
curl_setopt_array($ch, [
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_TIMEOUT => 15,
]);
$api1_response = curl_exec($ch);
curl_close($ch);

$api1 = json_decode($api1_response, true);

// ====== Custom Response তৈরি ======
$custom = [
  'current_page' => 1,
  'data' => [
    [
      'user_id' => $info['id'] ?? null,
      'id' => $info['id'] ?? null,
      'first_name' => $info['first_name'] ?? null,
      'gender' => $info['gender'] ?? null,
      'age' => $info['age'] ?? null,
      'bmet_no' => $info['bmet_no'] ?? null,
      'passport_number' => $info['passport_number'] ?? null,
      'verification_status' => $info['verification_status'] ?? null,
      'total_training' => $info['total_training'] ?? null,
      'concatenated_course_names' => $info['concatenated_course_names'] ?? null,
      'degree_name' => $info['degree_name'] ?? null,
      'total_skill' => $info['total_skill'] ?? null,
      'concatenated_skill_names' => $info['concatenated_skill_names'] ?? null,
      'e_expat_id' => $info['e_expat_id'] ?? null,
      'EXPAT-DATA' => $api1['expat_data'] ?? null, // api1 থেকে পাওয়া ডেটা
      'present_address' => $api1['present_address'] ?? null,
      'permanent_address' => $api1['permanent_address'] ?? null,
      'nominee' => $api1['nominee'] ?? null,
      'contact' => $api1['contact'] ?? null,
      'education' => $api1['education'] ?? null
      
    ]
  ],

  'OWNER' => RAFI69,
  'SYSTEM' => BLACKFIRETOOLS
];

echo json_encode($custom, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);