<?php

namespace classes;

class error_report_schema_bootstrap
{
    private static bool $initialized = false;
    private static ?bool $tablesExist = null;

    public static function ensureTables(): void
    {
        if (self::$initialized) {
            return;
        }

        global $db;

        $db->query("CREATE TABLE IF NOT EXISTS error_reports (
            id BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
            status VARCHAR(16) NOT NULL DEFAULT 'open',
            reporter_type VARCHAR(16) NOT NULL,
            reporter_user_id INT NULL,
            reporter_subuser_id INT NULL,
            reporter_customer_number INT NULL,
            reporter_customer_number_context INT NULL,
            reporter_name VARCHAR(255) NULL,
            reporter_email VARCHAR(255) NULL,
            route_path VARCHAR(512) NULL,
            page_url VARCHAR(1024) NULL,
            release_trace_id VARCHAR(64) NULL,
            frontend_version VARCHAR(128) NULL,
            api_version VARCHAR(128) NULL,
            screenshot_object_key VARCHAR(512) NOT NULL,
            screenshot_mime_type VARCHAR(64) NOT NULL,
            screenshot_size_bytes INT UNSIGNED NOT NULL DEFAULT 0,
            before_error TEXT NOT NULL,
            expected TEXT NOT NULL,
            actual TEXT NOT NULL,
            request_error_count INT UNSIGNED NOT NULL DEFAULT 0,
            vue_error_count INT UNSIGNED NOT NULL DEFAULT 0,
            request_errors_json LONGTEXT NULL,
            vue_errors_json LONGTEXT NULL,
            runtime_context_json LONGTEXT NULL,
            data_collection_accepted TINYINT(1) NOT NULL DEFAULT 0,
            data_collection_accepted_at DATETIME NOT NULL,
            data_collection_policy_version VARCHAR(64) NOT NULL DEFAULT 'error-report-v1',
            resolved_at DATETIME NULL,
            resolved_by_user_id INT NULL,
            resolution_note TEXT NULL,
            created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
            updated_at TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
            INDEX idx_error_reports_status_created (status, created_at),
            INDEX idx_error_reports_reporter_user (reporter_user_id, created_at),
            INDEX idx_error_reports_reporter_subuser (reporter_subuser_id, created_at),
            INDEX idx_error_reports_customer (reporter_customer_number, reporter_customer_number_context),
            INDEX idx_error_reports_trace (release_trace_id),
            INDEX idx_error_reports_route (route_path)
        ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci");

        self::$initialized = true;
        self::$tablesExist = true;
    }

    public static function tablesExist(): bool
    {
        if (self::$tablesExist !== null) {
            return self::$tablesExist;
        }

        global $db;

        $result = $db->query("SHOW TABLES LIKE 'error_reports'");
        self::$tablesExist = $result !== false && $result->num_rows > 0;
        return self::$tablesExist;
    }
}
