CREATE TABLE IF NOT EXISTS auth_login_attempt (
    id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
    attempt_key VARCHAR(191) NOT NULL,
    failure_count INT NOT NULL DEFAULT 0,
    window_started_at DATETIME NOT NULL,
    last_failed_at DATETIME NOT NULL,
    locked_until DATETIME NULL,
    created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    UNIQUE KEY uk_attempt_key (attempt_key),
    KEY idx_locked_until (locked_until),
    KEY idx_last_failed_at (last_failed_at)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

CREATE TABLE IF NOT EXISTS api_rate_limit_counter (
    id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
    bucket_key VARCHAR(191) NOT NULL,
    scope VARCHAR(32) NOT NULL,
    client_ip VARCHAR(64) NOT NULL,
    window_start_at DATETIME NOT NULL,
    expires_at DATETIME NOT NULL,
    request_count INT NOT NULL DEFAULT 0,
    created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    UNIQUE KEY uk_bucket_key (bucket_key),
    KEY idx_scope_client_ip (scope, client_ip),
    KEY idx_expires_at (expires_at)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
