-- Security module: a security check instance (the "project").
-- Fixed 7-step process state + product-specific technical checklist state are
-- stored as JSON. tech_schema_snapshot_json freezes the template's checklist at
-- creation time so later template edits don't mutate in-flight checks.
CREATE TABLE IF NOT EXISTS `security_checks` (
    `id`                        BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
    `tenant_id`                 INT UNSIGNED    NOT NULL,
    `debitor_no`                VARCHAR(30)     NOT NULL DEFAULT '',
    `debitor_name`              VARCHAR(255)    NOT NULL DEFAULT '',
    `domain_no`                 VARCHAR(30)     NOT NULL DEFAULT '',
    `domain_url`                VARCHAR(255)    NOT NULL DEFAULT '',
    `product_code`              VARCHAR(60)     NOT NULL DEFAULT '',
    `product_name`              VARCHAR(255)    NOT NULL DEFAULT '',
    `template_id`               BIGINT UNSIGNED NULL     DEFAULT NULL,
    `owner_user_id`             INT UNSIGNED    NULL     DEFAULT NULL,
    `status`                    VARCHAR(20)     NOT NULL DEFAULT 'open',
    `process_state_json`        TEXT            NULL     DEFAULT NULL,
    `tech_schema_snapshot_json` TEXT            NULL     DEFAULT NULL,
    `tech_state_json`           TEXT            NULL     DEFAULT NULL,
    `created_by`                INT UNSIGNED    NOT NULL,
    `updated_by`                INT UNSIGNED    NULL     DEFAULT NULL,
    `created_at`                DATETIME        NOT NULL DEFAULT CURRENT_TIMESTAMP,
    `updated_at`                DATETIME        NULL     DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP,
    PRIMARY KEY (`id`),
    KEY `idx_sc_tenant`         (`tenant_id`),
    KEY `idx_sc_tenant_status`  (`tenant_id`, `status`),
    KEY `idx_sc_tenant_debitor` (`tenant_id`, `debitor_no`),
    KEY `idx_sc_tenant_product` (`tenant_id`, `product_code`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
