<?php
// prevent direct access
if (!defined('WD')) {
    exit;
}
global $MINIO;

use classes\wash_certificate_store;

function warn($message): void
{
    echo "\n\033[33m$message\033[0m\n";
}

// Check if the MINIO array is set
if (!isset($MINIO)) {
    throw new Exception('MINIO array is not set in config.php');
}
// Check if the MINIO array has the required keys
if (!isset($MINIO['endpoint']) || !isset($MINIO['access_key']) || !isset($MINIO['secret_key'])) {
    throw new Exception('MINIO array is missing required keys');
}
// Create a new S3Client object
$wash_certificate_store = new wash_certificate_store();

// List all wash certificates
$wash_certificates = $wash_certificate_store->listFiles();
// Check if the request was successful
if (count($wash_certificates) > 0) {
    // Return the count of wash certificates
    echo "\nWash certificates found: " . count($wash_certificates);
} else {
    // Show warning message
    warn('No wash certificates found');
}

// Create a new wash certificate
$wash_certificate = $wash_certificate_store->createObject('test.txt', 'Hello, World!');
// Check if the request was successful
if ($wash_certificate) {
    // Show success message
    echo "\nWash certificate created successfully";
} else {
    // Show error message
    warn('Failed to create wash certificate');
}

// Check if the file exists in the bucket
$wash_certificate_exists = $wash_certificate_store->doesObjectExist('test.txt');
// Check if the request was successful
if ($wash_certificate_exists) {
    // Show success message
    echo "\nWash certificate exists";
} else {
    // Show warning message
    warn('Wash certificate does not exist');
}

// Create a download link for the wash certificate
$wash_certificate_download_link = $wash_certificate_store->getPresignedUrl('test.txt');
// Check if the request was successful
if ($wash_certificate_download_link) {
    // Show the download link
    echo "\nDownload link: $wash_certificate_download_link";
} else {
    // Show error message
    warn('Failed to create download link');
}

// Upload a wash certificate from a file
$wash_certificate_upload = $wash_certificate_store->uploadFile('test.txt', 'tests/minio/testUpload.txt');
// Check if the request was successful
if ($wash_certificate_upload) {
    // Show success message
    echo "\nWash certificate uploaded successfully from file";
} else {
    // Show error message
    warn('Failed to upload wash certificate from file');
}