<?php
require_once __DIR__ . '/../config/config.php';
try {
    $db = Database::getInstance();
    $ids = [3];
    $placeholders = implode(',', array_fill(0,count($ids),'?'));
    $existing = $db->fetchAll("SELECT id, name, template_name FROM message_templates WHERE id IN ($placeholders)", $ids);
    var_export($existing);

    $db->beginTransaction();
    $db->execute("DELETE FROM message_templates WHERE id IN ($placeholders)", $ids);
    $operatorId = 1;
    $now = date('Y-m-d H:i:s');
    foreach ($existing as $t) {
        $db->insert('operator_activity', [
            'user_id' => null,
            'operator_id' => $operatorId,
            'action' => 'delete_template',
            'details' => 'Deleted template: ' . ($t['name'] ?? $t['template_name'] ?? $t['id']),
            'created_at' => $now
        ]);
    }
    $db->commit();
    echo "Deleted and logged\n";
} catch (Exception $e) {
    echo "Exception: " . $e->getMessage() . PHP_EOL;
    $db->rollback();
}
