CREATE TABLE api_keys (
    id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
    public_id VARCHAR(40) NOT NULL,
    tenant_id BIGINT UNSIGNED NOT NULL,
    name VARCHAR(150) NOT NULL,
    key_hash CHAR(64) NOT NULL,
    key_class VARCHAR(32) NOT NULL,
    rate_limit_strategy VARCHAR(32) NOT NULL,
    rate_limit_per_minute INT UNSIGNED NOT NULL,
    scopes JSON NULL,
    metadata JSON NULL,
    is_active TINYINT(1) NOT NULL DEFAULT 1,
    expires_at DATETIME(6) NULL DEFAULT NULL,
    last_used_at DATETIME(6) NULL DEFAULT NULL,
    revoked_at DATETIME(6) NULL DEFAULT NULL,
    created_at DATETIME(6) NULL DEFAULT CURRENT_TIMESTAMP(6),
    updated_at DATETIME(6) NULL DEFAULT CURRENT_TIMESTAMP(6) ON UPDATE CURRENT_TIMESTAMP(6),
    PRIMARY KEY (id),
    UNIQUE KEY api_keys_public_id_unique (public_id),
    UNIQUE KEY api_keys_key_hash_unique (key_hash),
    KEY api_keys_tenant_id_is_active_index (tenant_id, is_active),
    CONSTRAINT api_keys_tenant_id_foreign FOREIGN KEY (tenant_id) REFERENCES tenants (id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
