-- Migration: Create http_page_fetch_log table for logging HTTP page fetches (HTTV/click-TT etc.)
-- Dient zum Verständnis der externen Seiten-Struktur und URL-Varianten je nach Verband/Saison

CREATE TABLE IF NOT EXISTS http_page_fetch_log (
    id INT AUTO_INCREMENT PRIMARY KEY,
    user_id INT NULL COMMENT 'Optional: User der den Aufruf ausgelöst hat',
    fetch_type VARCHAR(64) NOT NULL COMMENT 'z.B. leaguePage, clubInfoDisplay, regionMeetingFilter',
    base_domain VARCHAR(255) NOT NULL COMMENT 'z.B. httv.click-tt.de',
    full_url TEXT NOT NULL COMMENT 'Vollständige aufgerufene URL',
    association VARCHAR(64) NULL COMMENT 'Verband (z.B. HeTTV, RTTV)',
    championship VARCHAR(128) NULL COMMENT 'Championship-Parameter (z.B. HTTV 25/26)',
    club_id_param VARCHAR(64) NULL COMMENT 'Club-ID falls clubInfoDisplay',
    request_method VARCHAR(16) NULL COMMENT 'HTTP-Methode des ausgehenden Requests',
    request_headers LONGTEXT NULL COMMENT 'Gesendete Request-Header als JSON',
    request_body LONGTEXT NULL COMMENT 'Gesendeter Request-Body im Originalformat',
    http_status INT NULL COMMENT 'HTTP-Status der Response',
    success BOOLEAN NOT NULL DEFAULT FALSE,
    response_snippet TEXT NULL COMMENT 'Gekürzter Response-Anfang (max 2000 Zeichen) zur Strukturanalyse',
    content_type VARCHAR(128) NULL COMMENT 'Content-Type der Response',
    response_headers LONGTEXT NULL COMMENT 'Empfangene Response-Header als JSON',
    response_body LONGTEXT NULL COMMENT 'Vollständiger Response-Body',
    response_url TEXT NULL COMMENT 'Finale Response-URL nach Redirects',
    error_message TEXT NULL,
    execution_time_ms INT NULL COMMENT 'Laufzeit in Millisekunden',
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,

    INDEX idx_created_at (created_at),
    INDEX idx_base_domain_fetch_type (base_domain, fetch_type),
    INDEX idx_association_championship (association, championship),
    FOREIGN KEY (user_id) REFERENCES user(id) ON DELETE SET NULL ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
