<?php

class FormularLogger
{
    const ACTION_VALIDATION_ERROR = 'VALIDATION_ERROR';
    const ACTION_SUCCESS = 'SUCCESS';
    const ACTION_MISSING_FIELD = 'MISSING_FIELD';
    const ACTION_INVALID_EMAIL = 'INVALID_EMAIL';

    /**
     * Loggt einen Formularvorgang in die DB
     */
    public static function log($formId, $email, $ipAddress, $action, $message = '', $isAdminPreview = false)
    {
        $db = Database::getInstance();

        $sql = "
            INSERT INTO formular_logs (form_id, email, ip_address, action, message, is_admin_preview)
            VALUES (:form_id, :email, :ip, :action, :message, :is_admin_preview)
        ";

        $params = [
            ':form_id' => $formId,
            ':email' => $email,
            ':ip' => $ipAddress,
            ':action' => $action,
            ':message' => $message,
            ':is_admin_preview' => $isAdminPreview ? 1 : 0,
        ];

        try {
            $stmt = $db->prepare($sql);
            $stmt->execute($params);
            ErrorLogger::logSqlSuccess('formular_logger: insert log entry | SQL: ' . $sql . ' | params: ' . json_encode($params, JSON_UNESCAPED_UNICODE));
        } catch (Throwable $e) {
            ErrorLogger::logSqlError('formular_logger: insert log entry failed | SQL: ' . $sql . ' | params: ' . json_encode($params, JSON_UNESCAPED_UNICODE) . ' | error: ' . $e->getMessage());
        }
    }
}
