-- =============================================================================
-- Module 6: AI Memory Manager
-- Migration 007 — website_states
-- =============================================================================

CREATE TABLE IF NOT EXISTS `website_states` (
    `id`                  BIGINT UNSIGNED  NOT NULL AUTO_INCREMENT,
    `user_id`             BIGINT UNSIGNED  NOT NULL,
    `conversation_id`     BIGINT UNSIGNED  NULL       DEFAULT NULL,
    `template_id`         BIGINT UNSIGNED  NULL       DEFAULT NULL,
    `name`                VARCHAR(255)     NOT NULL   DEFAULT 'Untitled Website',
    `slug`                VARCHAR(255)     NOT NULL,
    `sections`            JSON             NOT NULL,
    `colors`              JSON             NOT NULL,
    `fonts`               JSON             NOT NULL,
    `content`             JSON             NOT NULL,
    `images`              JSON             NOT NULL,
    `current_revision_id` BIGINT UNSIGNED  NULL       DEFAULT NULL COMMENT 'Points to the active revision (undo/redo cursor)',
    `status`              ENUM('draft','published') NOT NULL DEFAULT 'draft',
    `created_at`          DATETIME         NOT NULL   DEFAULT CURRENT_TIMESTAMP,
    `updated_at`          DATETIME         NOT NULL   DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,

    PRIMARY KEY (`id`),
    UNIQUE  KEY `uq_website_states_slug`            (`slug`),
    INDEX         `idx_website_states_user`         (`user_id`),
    INDEX         `idx_website_states_conversation` (`conversation_id`),
    INDEX         `idx_website_states_template`     (`template_id`),
    INDEX         `idx_website_states_status`       (`status`),
    INDEX         `idx_website_states_updated`      (`updated_at`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
