<?php
// Check if the request contains form data
if ($_SERVER['REQUEST_METHOD'] === 'POST' && empty($_POST) === false) {
    // Handle form data
    $name = $_POST['name'];
    $idNumber = $_POST['id_number'];
    $client = $_POST['client'];
    $validity = $_POST['validity'];
    $plan = $_POST['plan'];
    $imageUrl = $_POST['image_url'];
} 

elseif ($_SERVER['REQUEST_METHOD'] === 'POST') {
    // Get the raw JSON data from the request body
    $inputJSON = file_get_contents('php://input');

    // Check if JSON data was received
    if ($inputJSON === false || empty($inputJSON)) {
        http_response_code(400); // Bad Request
        echo json_encode(array('error' => 'No valid input data received'));
        exit;
    }

    // Decode the JSON data
    $data = json_decode($inputJSON, true);

    // Check if the JSON data is valid
    if ($data === null) {
        http_response_code(400); // Bad Request
        echo json_encode(array('error' => 'Invalid JSON data'));
        exit;
    }

    // Extract data from the JSON object
    $name = $data['name'];
    $idNumber = $data['id_number'];
    $client = $data['client'];
    $validity = $data['validity'];
    $plan = $data['plan'];
    $imageUrl = $data['image_url'];
} else {
    http_response_code(400); // Bad Request
    echo json_encode(array('error' => 'Invalid request'));
    exit;
}

    // Fetch image data from URL
    $imageData = file_get_contents($imageUrl);
    if ($imageData === false) {
        // Handle error if unable to fetch image data
        http_response_code(400); // Bad Request
        echo json_encode(array('error' => 'Failed to fetch image from the provided URL'));
        exit;
    }

    // Create image from fetched data
    $image = imagecreatefromstring($imageData);

    // Load your A5-sized background image
    $backgroundImage = imagecreatefromjpeg('../images/healthassureid.jpg');

    // Create an empty image (A5 size: 148mm x 210mm at 300dpi)
    $idCard = imagecreatetruecolor(2480, 3508); // A5 size at 300dpi

    // Set background color (optional)
    $backgroundColor = imagecolorallocate($idCard, 255, 255, 255); // White background
    imagefill($idCard, 0, 0, $backgroundColor);

    // Merge the background image with the ID card
    imagecopyresampled($idCard, $backgroundImage, 0, 0, 0, 0, 2480, 3508, imagesx($backgroundImage), imagesy($backgroundImage));

    // Overlay text (name, ID number, etc.) on the ID card
    $textColor = imagecolorallocate($idCard, 0, 0, 0); // Black text color

    // Font settings
    $font = './Montserrat-Bold.ttf'; // Replace with your font file
    $fontSize = 70;
    
    // Merge the passport photo onto the ID card
    $imageWidth = imagesx($image);
    $imageHeight = imagesy($image);
    $photoWidth = 600; // Adjust the size of the passport photo as needed
    $photoHeight = 600; // Adjust the size of the passport photo as needed
    $photoX = 110; // X-coordinate for the passport photo position
    $photoY = 800; // Y-coordinate for the passport photo position
    imagecopyresampled($idCard, $image, $photoX, $photoY, 0, 0, $photoWidth, $photoHeight, $imageWidth, $imageHeight);
    
    // Example: Write name on ID card
    imagettftext($idCard, $fontSize, 0, $textX = 800, $textY = 850, $textColor, $font, "NAME: $name");

    // Example: Write ID number on ID card
    $idTextX = 800;
    $idTextY = 1000; // Adjust Y-coordinate as needed
    imagettftext($idCard, $fontSize, 0, $idTextX, $idTextY, $textColor, $font, "HMO ID: $idNumber");
    
    // Example: Write Plan on ID card
    $planTextX = 800;
    $planTextY = 1150; // Adjust Y-coordinate as needed
    imagettftext($idCard, $fontSize, 0, $planTextX, $planTextY, $textColor, $font, "PLAN: $plan");
    
    // Example: Write Validity on ID card
    $validityTextX = 800;
    $validityTextY = 1300; // Adjust Y-coordinate as needed
    imagettftext($idCard, $fontSize, 0, $validityTextX, $validityTextY, $textColor, $font, "SEX: $validity");


// Example: Write Client on ID card
    $clientTextX = 800;
    $clientTextY = 1450; // Adjust Y-coordinate as needed
    imagettftext($idCard, $fontSize, 0, $clientTextX, $clientTextY, $textColor, $font, "CLIENT: $client");


    // Save the generated ID card image to a local folder
    $outputFileName = time() . '.jpg'; // Example file name with timestamp
    $outputFilePath = 'cards/' . $outputFileName;

    imagejpeg($idCard, $outputFilePath); // Save the image as JPEG (you can use PNG if preferred)

    // Free up memory
    imagedestroy($backgroundImage);
    imagedestroy($idCard);
    imagedestroy($image);

    // Return the URL or file path of the saved ID card image in the API response
    $response = array(
        'success' => true,
        'file_url' => "https://visuretech.com/enrollment/data/" . $outputFilePath // You can return the URL here if your server is publicly accessible
    );

    header('Content-Type: application/json');
    echo json_encode($response);
 
?>
