<?php
session_start(); // Start a session to store flags

// Hardcoded license key for comparison
$license_key = 'ABCD-1234-EFGH-5678';

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    if (isset($_FILES['csvfile']) && $_FILES['csvfile']['error'] === UPLOAD_ERR_OK) {
        $temp_file_path = $_FILES['csvfile']['tmp_name'];
        $file_name = basename($_FILES['csvfile']['name']);
        $file_extension = pathinfo($file_name, PATHINFO_EXTENSION);

        if ($file_extension !== 'csv') {
            echo json_encode(['status' => 'error', 'message' => 'Invalid file type.']);
            exit;
        }

        // Define the destination directory and file path
        $upload_dir = __DIR__ . '/../license/';
        if (!is_dir($upload_dir)) {
            mkdir($upload_dir, 0777, true); // Create directory if it doesn't exist
        }
        $destination_file_path = $upload_dir . $file_name;

        // Move the uploaded file to the desired location
        if (!move_uploaded_file($temp_file_path, $destination_file_path)) {
            echo json_encode(['status' => 'error', 'message' => 'Failed to move uploaded file.']);
            exit;
        }

        if (($opening_csv = fopen($destination_file_path, 'r')) !== false) {
            if (($header = fgetcsv($opening_csv, 1000, ',')) !== false) {
                $licenseKeyIndex = array_search('License Key', $header);
                $createdDateIndex = array_search('Created Date', $header);
                $expirationDateIndex = array_search('Expiration Date', $header);

                if ($licenseKeyIndex === false || $createdDateIndex === false || $expirationDateIndex === false) {
                    fclose($opening_csv);
                    echo json_encode(['status' => 'error', 'message' => 'CSV header missing required fields.']);
                    exit;
                }

                if (($data = fgetcsv($opening_csv, 1000, ',')) !== false) {
                    $licenseKey = $data[$licenseKeyIndex];
                    $createdDate = $data[$createdDateIndex];
                    $expirationDate = $data[$expirationDateIndex];

                    if ($licenseKey === $license_key) {
                        $_SESSION['license_valid'] = true;

                        $currentDate = new DateTime();
                        $createdDateTime = new DateTime($createdDate);
                        $expirationDateTime = new DateTime($expirationDate);

                        $dateDifference = $expirationDateTime->diff($currentDate)->days;

                        if ($currentDate >= $createdDateTime && $currentDate <= $expirationDateTime) {
                            if ($dateDifference <= 10) {
                                $_SESSION['expiration_warning'] = true;
                            } else {
                                $_SESSION['expiration_warning'] = false;
                            }
                        } else {
                            fclose($opening_csv);
                            echo json_encode(['status' => 'error', 'message' => 'License key has expired or is not yet valid.']);
                            exit;
                        }

                        fclose($opening_csv);
                        echo json_encode(['status' => 'success']);
                    } else {
                        fclose($opening_csv);
                        echo json_encode(['status' => 'error', 'message' => 'Invalid license key.']);
                    }
                } else {
                    fclose($opening_csv);
                    echo json_encode(['status' => 'error', 'message' => 'Failed to read the license key and dates from the CSV file.']);
                }
            } else {
                fclose($opening_csv);
                echo json_encode(['status' => 'error', 'message' => 'Failed to read the CSV header.']);
            }
        } else {
            echo json_encode(['status' => 'error', 'message' => 'Error opening the CSV file.']);
        }
    } else {
        echo json_encode(['status' => 'error', 'message' => 'Error uploading file.']);
    }
} else {
    echo json_encode(['status' => 'error', 'message' => 'Invalid request method.']);
}
?>
