-- ============================================================
-- USERS – Benutzerkonto + spätere Rechnungs-/Zahlungsinfos
-- ============================================================

CREATE TABLE IF NOT EXISTS users (
    id BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
    
    -- Login
    username VARCHAR(50) NOT NULL UNIQUE,
    email VARCHAR(150) NOT NULL UNIQUE,
    password_hash VARCHAR(255) NOT NULL,
    avatar_path VARCHAR(255) NULL,

    -- Persönliche Daten
    first_name VARCHAR(100) NULL,
    last_name VARCHAR(100) NULL,

    -- spätere Rechnungsdaten / Billing
    company_name VARCHAR(255) NULL,
    street VARCHAR(255) NULL,
    postal_code VARCHAR(20) NULL,
    city VARCHAR(255) NULL,
    country VARCHAR(100) NULL,
    vat_id VARCHAR(50) NULL,

    -- spätere Pro-Features / Limits
    plan ENUM('free', 'pro', 'enterprise') DEFAULT 'free',
    plan_valid_until DATETIME NULL,

    -- Sicherheit
    created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    last_login_at DATETIME NULL,
    failed_logins INT DEFAULT 0,
    is_locked TINYINT(1) DEFAULT 0
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;


-- ============================================================
-- USB DEVICES – vom Nutzer gespeicherte USB-Sticks
-- ============================================================

CREATE TABLE IF NOT EXISTS usb_devices (
    id BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
    user_id BIGINT UNSIGNED NOT NULL,

    serial_number VARCHAR(255) NULL,
    manufacturer VARCHAR(255) NULL,
    model_name VARCHAR(255) NULL,
    usb_type ENUM('USB 2.0', 'USB 3.0', 'USB 3.1', 'USB 3.2', 'USB 4.0') NULL,

    capacity_bytes BIGINT UNSIGNED NULL,
    advertised_capacity_bytes BIGINT UNSIGNED NULL,

    read_speed_mbps FLOAT NULL,
    write_speed_mbps FLOAT NULL,

    created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP
        ON UPDATE CURRENT_TIMESTAMP,

    FOREIGN KEY (user_id) REFERENCES users(id)
        ON DELETE CASCADE
);


-- ============================================================
-- USB TEST RESULTS – Schnelltest + Pro-Test
-- ============================================================

CREATE TABLE IF NOT EXISTS usb_tests (
    id BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
    usb_device_id BIGINT UNSIGNED NOT NULL,
    user_id BIGINT UNSIGNED NOT NULL,

    -- Testtyp
    test_type ENUM('quick', 'standard', 'deep', 'pro') NOT NULL,

    -- Ergebniswerte
    test_start DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    test_end DATETIME NULL,

    read_speed_mbps FLOAT NULL,
    write_speed_mbps FLOAT NULL,

    integrity_ok TINYINT(1) NULL,
    checksum_sha256 VARCHAR(255) NULL,

    -- Pro-Modus Zusatzwerte (f3, badblocks etc.)
    f3_status ENUM('unknown', 'pass', 'fail', 'warning') DEFAULT 'unknown',
    f3_real_capacity_bytes BIGINT UNSIGNED NULL,
    f3_lost_bytes BIGINT UNSIGNED NULL,

    badblocks_errors INT NULL,

    -- Metadaten
    test_report_json JSON NULL,
    ip_address VARCHAR(45) NULL,

    created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP
        ON UPDATE CURRENT_TIMESTAMP,

    FOREIGN KEY (usb_device_id) REFERENCES usb_devices(id)
        ON DELETE CASCADE,

    FOREIGN KEY (user_id) REFERENCES users(id)
        ON DELETE CASCADE
);


-- ============================================================
-- WEB QUICKTESTS – Browser-Schnellcheck (Gast + eingeloggt)
-- ============================================================

CREATE TABLE IF NOT EXISTS web_quicktests (
    id BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,

    -- Zuordnung zum Nutzer (NULL = anonym)
    user_id BIGINT UNSIGNED NULL,
    is_logged_in TINYINT(1) NOT NULL DEFAULT 0,

    -- optional: Zuordnung zu einem gespeicherten Stick (falls vorhanden)
    usb_device_id BIGINT UNSIGNED NULL,

    -- Umgebung (für Statistik / Debug)
    browser_name VARCHAR(100) NULL,
    browser_version VARCHAR(50) NULL,
    os_name VARCHAR(100) NULL,
    os_version VARCHAR(50) NULL,

    -- Stick-Infos
    volume_label VARCHAR(255) NULL,
    manufacturer VARCHAR(255) NULL,
    model_name VARCHAR(255) NULL,
    usb_type ENUM('USB 2.0', 'USB 3.0', 'USB 3.1', 'USB 3.2', 'USB 4.0') NULL,

    -- Kapazität
    advertised_capacity_bytes BIGINT UNSIGNED NULL,
    measured_capacity_bytes   BIGINT UNSIGNED NULL,

    capacity_status ENUM('unknown', 'ok', 'suspicious', 'fake')
        NOT NULL DEFAULT 'unknown',

    filesystem VARCHAR(64) NULL,

    -- Vollständiger Testreport
    test_report_json JSON NULL,

    -- Meta
    ip_address VARCHAR(45) NULL,
    session_id VARCHAR(64) NULL,

    -- NEU: persistente Browser-/Client-ID zur Wiedererkennung
    client_id VARCHAR(64) NULL,

    created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP
        ON UPDATE CURRENT_TIMESTAMP,

    FOREIGN KEY (user_id) REFERENCES users(id)
        ON DELETE CASCADE,

    FOREIGN KEY (usb_device_id) REFERENCES usb_devices(id)
        ON DELETE CASCADE,

    INDEX idx_web_quicktests_client_id (client_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
