CREATE TABLE `user_session` (
    `id`           BINARY(16)    NOT NULL,
    `user_id`      BINARY(16)    NOT NULL,
    `token_hash`   BINARY(32)    NOT NULL,
    `created_at`   DATETIME      NOT NULL DEFAULT CURRENT_TIMESTAMP,
    `last_used_at` DATETIME      NOT NULL DEFAULT CURRENT_TIMESTAMP,
    `expires_at`   DATETIME      NOT NULL,
    `user_agent`   VARCHAR(255)  NULL,
    `ip_address`   VARBINARY(16) NULL,
    PRIMARY KEY (`id`),
    UNIQUE KEY `uniq_token_hash` (`token_hash`),
    KEY `idx_user_id` (`user_id`),
    KEY `idx_expires_at` (`expires_at`),
    CONSTRAINT `fk_user_session_user` FOREIGN KEY (`user_id`) REFERENCES `user` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
