-- =============================================================================
-- Module 6.6: Platform Stabilization / Part 5
-- Migration 011 — activity_logs
-- =============================================================================

CREATE TABLE IF NOT EXISTS `activity_logs` (
    `id`         BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
    `user_id`    BIGINT UNSIGNED NOT NULL,
    `website_id` BIGINT UNSIGNED NULL     DEFAULT NULL  COMMENT 'FK → websites.id (nullable for global actions)',
    `action`     VARCHAR(64)     NOT NULL               COMMENT 'Dot-notation action key e.g. template.selected',
    `metadata`   JSON            NOT NULL               COMMENT 'Arbitrary context for the action',
    `ip_address` VARCHAR(45)     NULL     DEFAULT NULL,
    `user_agent` VARCHAR(255)    NULL     DEFAULT NULL,
    `created_at` DATETIME        NOT NULL DEFAULT CURRENT_TIMESTAMP,

    PRIMARY KEY (`id`),
    INDEX `idx_activity_logs_user`       (`user_id`),
    INDEX `idx_activity_logs_website`    (`website_id`),
    INDEX `idx_activity_logs_action`     (`action`),
    INDEX `idx_activity_logs_created`    (`created_at`),

    CONSTRAINT `fk_activity_logs_website`
        FOREIGN KEY (`website_id`)
        REFERENCES  `websites` (`id`)
        ON DELETE SET NULL
        ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
