<?php

declare(strict_types=1);

class StorageMonitorScript
{
    public static function generate(): string
    {
        return <<<'MONITOR'
<?php
/**
 * TubePress Storage Monitor v1.0
 *
 * Place this file on your remote storage server (any web server: Apache, Nginx, LiteSpeed, IIS...).
 * It reports disk usage and health to your TubePress CMS.
 *
 * Requirements: PHP 7.4+ (no extensions needed)
 *
 * Setup:
 *   1. Upload this file to your storage server's web root or any accessible directory
 *   2. Make sure it's accessible via HTTPS (e.g., https://cdn.example.com/tubepress-storage-monitor.php)
 *   3. In TubePress admin → Storage → Edit → paste the URL in "Monitor script URL"
 *   4. The secret key below is pre-configured — do not change it
 */

$SECRET = '%%SECRET%%';

// ─── Auth ────────────────────────────────────────────────────────────────
$key = '';
if (isset($_GET['key'])) $key = $_GET['key'];
elseif (isset($_SERVER['HTTP_X_STORAGE_KEY'])) $key = $_SERVER['HTTP_X_STORAGE_KEY'];

if ($key === '' || $SECRET === '%%' . 'SECRET%%' || !hash_equals($SECRET, $key)) {
    http_response_code(403);
    header('Content-Type: application/json');
    echo json_encode(['error' => 'Unauthorized']);
    exit;
}

// ─── Config ──────────────────────────────────────────────────────────────
// Storage path: auto-detect or set manually
// By default, checks the directory where this script is located
// Change this if your files are stored elsewhere (e.g., /mnt/storage/videos)
$storagePath = isset($_GET['path']) && is_dir($_GET['path']) ? $_GET['path'] : dirname(__FILE__);

// ─── Action ──────────────────────────────────────────────────────────────
header('Content-Type: application/json');
header('Cache-Control: no-cache');
$action = isset($_GET['action']) ? $_GET['action'] : 'status';

if ($action === 'ping') {
    echo json_encode(['ok' => true, 'pong' => time()]);
    exit;
}

if ($action === 'status') {
    $result = ['ok' => true];

    // Disk space
    $total = @disk_total_space($storagePath);
    $free = @disk_free_space($storagePath);
    $result['disk_total'] = $total !== false ? (float)$total : 0;
    $result['disk_free'] = $free !== false ? (float)$free : 0;
    $result['disk_used'] = ($total !== false && $free !== false) ? (float)($total - $free) : 0;

    // Writable check
    $result['writable'] = is_writable($storagePath);

    // Write test (create + read + delete a tiny file)
    $result['write_test'] = false;
    if ($result['writable']) {
        $testFile = $storagePath . DIRECTORY_SEPARATOR . '.tubepress_monitor_' . time() . '_' . getmypid();
        $testData = 'tubepress-write-test-' . time();
        if (@file_put_contents($testFile, $testData) !== false) {
            $readBack = @file_get_contents($testFile);
            $result['write_test'] = ($readBack === $testData);
            @unlink($testFile);
        }
    }

    // System info
    $result['php_version'] = PHP_VERSION;
    $result['os'] = PHP_OS_FAMILY;
    $result['server_time'] = date('Y-m-d H:i:s');

    // Server software
    if (isset($_SERVER['SERVER_SOFTWARE'])) {
        $result['server'] = $_SERVER['SERVER_SOFTWARE'];
    }

    // Load average (Linux/macOS only)
    if (function_exists('sys_getloadavg')) {
        $load = sys_getloadavg();
        if ($load) $result['load'] = round($load[0], 2);
    }

    echo json_encode($result);
    exit;
}

echo json_encode(['error' => 'Unknown action: ' . $action]);
MONITOR;
    }
}
