<?php

//Biweekly Auto Task Each 07:00:00 AM
// /usr/local/bin/php /home/evosyspos/public_html/app.evosyspos.com/models/AutoTaskDeleteCloudinaryTicketsFolder.php
// curl -s https://app.evosyspos.com/models/AutoTaskDeleteCloudinaryTicketsFolder.php

require __DIR__ . '/../vendor/autoload.php';

use Dotenv\Dotenv;
use Cloudinary\Configuration\Configuration;
use Cloudinary\Api\Admin\AdminApi;

$dotenv = Dotenv::createImmutable(__DIR__ . '/../');
$dotenv->load();

date_default_timezone_set("America/New_York");

// file_put_contents(__DIR__ . '/crons.log', "Read ENV: " . date('Y-m-d H:i:s') . " " . json_encode($_ENV) . "\n", FILE_APPEND);

$CLOUDINARY_NAME = $_ENV['CLOUDINARY_NAME'];
$CLOUDINARY_KEY = $_ENV['CLOUDINARY_KEY'];
$CLOUDINARY_SECRET = $_ENV['CLOUDINARY_SECRET'];

Configuration::instance([
    'cloud' => [
        'cloud_name' => $CLOUDINARY_NAME,
        'api_key'    => $CLOUDINARY_KEY,
        'api_secret' => $CLOUDINARY_SECRET
    ],
    'url' => [
        'secure' => true
    ]
]);

$admin = new AdminApi();

$response = $admin->deleteAssetsByPrefix("evosyspos/tickets/", [
    "resource_type" => "image", // image, video, raw
    "type" => "upload",
    "invalidate" => true
]);

// file_put_contents(__DIR__ . '/crons.log', "Biweekly Auto Task Response: AutoTaskDeleteCloudinaryTicketsFolder: " . date('Y-m-d H:i:s') . " " . json_encode($response) . "\n", FILE_APPEND);


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