<?php
require_once '../config/db.php';
require_once 'layout.php';

// OVERRIDE: Admin panel needs HTML
header('Content-Type: text/html; charset=utf-8');

// Handle Save
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    if ($_POST['type'] === 'storage') {
        $provider = $_POST['provider'];
        // Assume single config row for now
        $pdo->prepare("DELETE FROM storage_config")->execute();
        $pdo->prepare("INSERT INTO storage_config (provider, bucket, endpoint, access_key, secret_key, public_url_prefix) VALUES (?, ?, ?, ?, ?, ?)")
            ->execute([$provider, $_POST['bucket'], $_POST['endpoint'], $_POST['access_key'], $_POST['secret_key'], $_POST['public_url']]);
            
    } elseif ($_POST['type'] === 'push') {
        // Just mock saving to a config file or similar since we lack a global config table for this specifically yet
        // For now let's assume valid save
    }
}

// Fetch Configs
try {
    $storage = $pdo->query("SELECT * FROM storage_config LIMIT 1")->fetch(PDO::FETCH_ASSOC);
} catch (PDOException $e) {}

renderAdminSidebar('integrations');
?>

<div class="max-w-4xl mx-auto">
    <!-- Header -->
    <div class="mb-8 ">
        <h2 class="text-3xl font-bold text-white tracking-tight">Integraciones del Sistema</h2>
        <p class="text-gray-400 mt-1">Configura servicios externos de almacenamiento, notificaciones y más.</p>
    </div>

    <!-- Storage Config -->
    <div class="bg-dark-800 rounded-2xl border border-dark-700 shadow-xl p-8 mb-8">
        <div class="flex items-center gap-4 mb-6 border-b border-dark-700 pb-4">
             <div class="w-12 h-12 bg-orange-500/20 rounded-xl flex items-center justify-center text-orange-500">
                <i class="fas fa-cloud text-2xl"></i>
            </div>
            <div>
                <h3 class="text-xl font-bold text-white">Almacenamiento (Storage)</h3>
                <p class="text-sm text-gray-400">Define dónde se guardan los avatares y streams (Local, AWS S3, Cloudflare R2).</p>
            </div>
        </div>

        <form method="POST" class="grid grid-cols-1 md:grid-cols-2 gap-6">
            <input type="hidden" name="type" value="storage">
            
            <div class="col-span-full">
                <label class="block text-sm font-medium text-gray-400 mb-2">Proveedor</label>
                <select name="provider" class="w-full bg-dark-900 border border-dark-700 rounded-lg p-3 text-white">
                    <option value="local" <?php echo ($storage['provider']??'')=='local'?'selected':''; ?>>Local Disk (Default)</option>
                    <option value="s3" <?php echo ($storage['provider']??'')=='s3'?'selected':''; ?>>Amazon S3</option>
                    <option value="r2" <?php echo ($storage['provider']??'')=='r2'?'selected':''; ?>>Cloudflare R2</option>
                    <option value="spaces" <?php echo ($storage['provider']??'')=='spaces'?'selected':''; ?>>DigitalOcean Spaces</option>
                </select>
            </div>

            <div>
                <label class="block text-sm font-medium text-gray-400 mb-2">Endpoint URL</label>
                <input type="text" name="endpoint" value="<?php echo htmlspecialchars($storage['endpoint']??''); ?>" placeholder="https://s3.us-east-1.amazonaws.com" class="w-full bg-dark-900 border border-dark-700 rounded-lg p-3 text-white">
            </div>
             <div>
                <label class="block text-sm font-medium text-gray-400 mb-2">Bucket Name</label>
                <input type="text" name="bucket" value="<?php echo htmlspecialchars($storage['bucket']??''); ?>" placeholder="my-app-uploads" class="w-full bg-dark-900 border border-dark-700 rounded-lg p-3 text-white">
            </div>

             <div>
                <label class="block text-sm font-medium text-gray-400 mb-2">Access Key ID</label>
                <input type="text" name="access_key" value="<?php echo htmlspecialchars($storage['access_key']??''); ?>" class="w-full bg-dark-900 border border-dark-700 rounded-lg p-3 text-white font-mono">
            </div>
             <div>
                <label class="block text-sm font-medium text-gray-400 mb-2">Secret Access Key</label>
                <input type="password" name="secret_key" value="<?php echo htmlspecialchars($storage['secret_key']??''); ?>" class="w-full bg-dark-900 border border-dark-700 rounded-lg p-3 text-white font-mono">
            </div>
            
             <div class="col-span-full">
                <label class="block text-sm font-medium text-gray-400 mb-2">Public CDN URL Prefix</label>
                <input type="text" name="public_url" value="<?php echo htmlspecialchars($storage['public_url_prefix']??''); ?>" placeholder="https://cdn.example.com" class="w-full bg-dark-900 border border-dark-700 rounded-lg p-3 text-white">
            </div>

            <div class="col-span-full pt-2">
                <button type="submit" class="bg-orange-600 hover:bg-orange-700 text-white font-bold py-3 px-6 rounded-xl shadow-lg transition">
                    <i class="fas fa-save mr-2"></i> Guardar Cambios de Almacenamiento
                </button>
            </div>
        </form>
    </div>

    <!-- Push Notification Config -->
    <div class="bg-dark-800 rounded-2xl border border-dark-700 shadow-xl p-8 opacity-75">
        <div class="flex items-center gap-4 mb-6 border-b border-dark-700 pb-4">
             <div class="w-12 h-12 bg-pink-500/20 rounded-xl flex items-center justify-center text-pink-500">
                <i class="fas fa-mobile-alt text-2xl"></i>
            </div>
            <div>
                <h3 class="text-xl font-bold text-white">Notificaciones Push (FCM)</h3>
                <p class="text-sm text-gray-400">Conecta Firebase Cloud Messaging para enviar alertas móviles.</p>
            </div>
             <span class="ml-auto bg-gray-700 text-xs px-2 py-1 rounded text-gray-300">Próximamente</span>
        </div>
        
        <div class="p-4 bg-dark-900 rounded-lg border border-dashed border-dark-700 text-center text-gray-500">
            Requiere archivo `service-account.json` en el servidor. Configuración manual por ahora.
        </div>
    </div>

</div>

<?php renderAdminFooter(); ?>
