<?php
/**
 * Approve all pending templates (script)
 * Usage: php approve_pending_templates.php
 */

require_once __DIR__ . '/../config/config_enhanced.php';

try {
    $db = Database::getInstance();
    echo "Connected DB\n";

    $pending = $db->fetchAll("SELECT id, name, template_name, status, created_at FROM message_templates WHERE status = 'pending'");

    if (empty($pending)) {
        echo "No pending templates found.\n";
        exit(0);
    }

    $ids = array_map(function($r){return $r['id'];}, $pending);

    $in = implode(',', array_map('intval', $ids));

    $updated = $db->query("UPDATE message_templates SET status = 'approved' WHERE id IN ($in)");

    echo "Updated templates: " . count($ids) . "\n";
    echo json_encode($pending, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE) . "\n";

} catch (Exception $e) {
    echo "Error: " . $e->getMessage() . "\n";
    exit(1);
}
