-- Workflow Engine v1.0 - MySQL 5.6 compatible schema
-- Definition records describe workflows. Runtime records hold submitted data.
-- Requires: InnoDB, utf8mb4. JSON values are stored as LONGTEXT by design.

SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;

CREATE TABLE IF NOT EXISTS workflow_component_type (
    workflow_component_type_id INT UNSIGNED NOT NULL AUTO_INCREMENT,
    code VARCHAR(50) NOT NULL,
    name VARCHAR(100) NOT NULL,
    renderer_file VARCHAR(150) NOT NULL,
    description VARCHAR(255) NULL,
    properties_json LONGTEXT NULL,
    status TINYINT UNSIGNED NOT NULL DEFAULT 1,
    sort_order INT UNSIGNED NOT NULL DEFAULT 0,
    created_by INT UNSIGNED NULL,
    created_at DATETIME NOT NULL,
    updated_by INT UNSIGNED NULL,
    updated_at DATETIME NULL,
    PRIMARY KEY (workflow_component_type_id),
    UNIQUE KEY uq_workflow_component_type_code (code),
    KEY idx_workflow_component_type_status_sort (status, sort_order)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS workflow_lookup (
    workflow_lookup_id INT UNSIGNED NOT NULL AUTO_INCREMENT,
    lookup_group VARCHAR(100) NOT NULL,
    code VARCHAR(100) NOT NULL,
    name VARCHAR(255) NOT NULL,
    parent_workflow_lookup_id INT UNSIGNED NULL,
    value VARCHAR(255) NULL,
    properties_json LONGTEXT NULL,
    status TINYINT UNSIGNED NOT NULL DEFAULT 1,
    sort_order INT UNSIGNED NOT NULL DEFAULT 0,
    created_by INT UNSIGNED NULL,
    created_at DATETIME NOT NULL,
    updated_by INT UNSIGNED NULL,
    updated_at DATETIME NULL,
    PRIMARY KEY (workflow_lookup_id),
    UNIQUE KEY uq_workflow_lookup_group_code (lookup_group, code),
    KEY idx_workflow_lookup_parent (parent_workflow_lookup_id),
    KEY idx_workflow_lookup_group_status_sort (lookup_group, status, sort_order),
    CONSTRAINT fk_workflow_lookup_parent FOREIGN KEY (parent_workflow_lookup_id)
        REFERENCES workflow_lookup (workflow_lookup_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS workflow (
    workflow_id INT UNSIGNED NOT NULL AUTO_INCREMENT,
    code VARCHAR(100) NOT NULL,
    name VARCHAR(255) NOT NULL,
    description TEXT NULL,
    uuid CHAR(36) NOT NULL,
    status VARCHAR(20) NOT NULL DEFAULT 'ACTIVE',
    properties_json LONGTEXT NULL,
    created_by INT UNSIGNED NULL,
    created_at DATETIME NOT NULL,
    updated_by INT UNSIGNED NULL,
    updated_at DATETIME NULL,
    deleted_at DATETIME NULL,
    PRIMARY KEY (workflow_id),
    UNIQUE KEY uq_workflow_code (code),
    UNIQUE KEY uq_workflow_uuid (uuid),
    KEY idx_workflow_status (status),
    KEY idx_workflow_deleted_at (deleted_at)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS workflow_version (
    workflow_version_id INT UNSIGNED NOT NULL AUTO_INCREMENT,
    workflow_id INT UNSIGNED NOT NULL,
    version_no INT UNSIGNED NOT NULL,
    status VARCHAR(20) NOT NULL DEFAULT 'DRAFT',
    published_at DATETIME NULL,
    published_by INT UNSIGNED NULL,
    change_notes TEXT NULL,
    definition_json LONGTEXT NULL,
    properties_json LONGTEXT NULL,
    created_by INT UNSIGNED NULL,
    created_at DATETIME NOT NULL,
    updated_by INT UNSIGNED NULL,
    updated_at DATETIME NULL,
    PRIMARY KEY (workflow_version_id),
    UNIQUE KEY uq_workflow_version_no (workflow_id, version_no),
    KEY idx_workflow_version_status (workflow_id, status),
    CONSTRAINT fk_workflow_version_workflow FOREIGN KEY (workflow_id)
        REFERENCES workflow (workflow_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS workflow_group (
    workflow_group_id INT UNSIGNED NOT NULL AUTO_INCREMENT,
    workflow_version_id INT UNSIGNED NOT NULL,
    code VARCHAR(100) NOT NULL,
    title VARCHAR(255) NOT NULL,
    description TEXT NULL,
    icon VARCHAR(100) NULL,
    is_skippable TINYINT UNSIGNED NOT NULL DEFAULT 0,
    status TINYINT UNSIGNED NOT NULL DEFAULT 1,
    sort_order INT UNSIGNED NOT NULL DEFAULT 0,
    properties_json LONGTEXT NULL,
    created_by INT UNSIGNED NULL,
    created_at DATETIME NOT NULL,
    updated_by INT UNSIGNED NULL,
    updated_at DATETIME NULL,
    PRIMARY KEY (workflow_group_id),
    UNIQUE KEY uq_workflow_group_code (workflow_version_id, code),
    KEY idx_workflow_group_version_sort (workflow_version_id, status, sort_order),
    CONSTRAINT fk_workflow_group_version FOREIGN KEY (workflow_version_id)
        REFERENCES workflow_version (workflow_version_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS workflow_block (
    workflow_block_id INT UNSIGNED NOT NULL AUTO_INCREMENT,
    workflow_group_id INT UNSIGNED NOT NULL,
    code VARCHAR(100) NOT NULL,
    title VARCHAR(255) NULL,
    description TEXT NULL,
    layout_columns TINYINT UNSIGNED NOT NULL DEFAULT 12,
    css_class VARCHAR(255) NULL,
    status TINYINT UNSIGNED NOT NULL DEFAULT 1,
    sort_order INT UNSIGNED NOT NULL DEFAULT 0,
    properties_json LONGTEXT NULL,
    created_by INT UNSIGNED NULL,
    created_at DATETIME NOT NULL,
    updated_by INT UNSIGNED NULL,
    updated_at DATETIME NULL,
    PRIMARY KEY (workflow_block_id),
    UNIQUE KEY uq_workflow_block_code (workflow_group_id, code),
    KEY idx_workflow_block_group_sort (workflow_group_id, status, sort_order),
    CONSTRAINT fk_workflow_block_group FOREIGN KEY (workflow_group_id)
        REFERENCES workflow_group (workflow_group_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS workflow_datasource (
    workflow_datasource_id INT UNSIGNED NOT NULL AUTO_INCREMENT,
    code VARCHAR(100) NOT NULL,
    name VARCHAR(255) NOT NULL,
    datasource_type VARCHAR(20) NOT NULL,
    configuration_json LONGTEXT NULL,
    cache_seconds INT UNSIGNED NOT NULL DEFAULT 0,
    status TINYINT UNSIGNED NOT NULL DEFAULT 1,
    created_by INT UNSIGNED NULL,
    created_at DATETIME NOT NULL,
    updated_by INT UNSIGNED NULL,
    updated_at DATETIME NULL,
    deleted_at DATETIME NULL,
    PRIMARY KEY (workflow_datasource_id),
    UNIQUE KEY uq_workflow_datasource_code (code),
    KEY idx_workflow_datasource_type_status (datasource_type, status),
    KEY idx_workflow_datasource_deleted_at (deleted_at)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS workflow_field (
    workflow_field_id INT UNSIGNED NOT NULL AUTO_INCREMENT,
    workflow_block_id INT UNSIGNED NOT NULL,
    workflow_component_type_id INT UNSIGNED NOT NULL,
    workflow_datasource_id INT UNSIGNED NULL,
    code VARCHAR(100) NOT NULL,
    label VARCHAR(255) NULL,
    field_type VARCHAR(30) NOT NULL,
    column_name VARCHAR(100) NULL,
    placeholder VARCHAR(255) NULL,
    help_text TEXT NULL,
    default_value LONGTEXT NULL,
    grid_columns TINYINT UNSIGNED NOT NULL DEFAULT 12,
    is_required TINYINT UNSIGNED NOT NULL DEFAULT 0,
    is_readonly TINYINT UNSIGNED NOT NULL DEFAULT 0,
    is_multiple TINYINT UNSIGNED NOT NULL DEFAULT 0,
    status TINYINT UNSIGNED NOT NULL DEFAULT 1,
    sort_order INT UNSIGNED NOT NULL DEFAULT 0,
    properties_json LONGTEXT NULL,
    created_by INT UNSIGNED NULL,
    created_at DATETIME NOT NULL,
    updated_by INT UNSIGNED NULL,
    updated_at DATETIME NULL,
    PRIMARY KEY (workflow_field_id),
    UNIQUE KEY uq_workflow_field_code (workflow_block_id, code),
    KEY idx_workflow_field_block_sort (workflow_block_id, status, sort_order),
    KEY idx_workflow_field_datasource (workflow_datasource_id),
    KEY idx_workflow_field_component_type (workflow_component_type_id),
    CONSTRAINT fk_workflow_field_block FOREIGN KEY (workflow_block_id)
        REFERENCES workflow_block (workflow_block_id),
    CONSTRAINT fk_workflow_field_component_type FOREIGN KEY (workflow_component_type_id)
        REFERENCES workflow_component_type (workflow_component_type_id),
    CONSTRAINT fk_workflow_field_datasource FOREIGN KEY (workflow_datasource_id)
        REFERENCES workflow_datasource (workflow_datasource_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS workflow_validation (
    workflow_validation_id INT UNSIGNED NOT NULL AUTO_INCREMENT,
    workflow_field_id INT UNSIGNED NOT NULL,
    validation_type VARCHAR(30) NOT NULL,
    rule_value LONGTEXT NULL,
    error_message VARCHAR(500) NULL,
    status TINYINT UNSIGNED NOT NULL DEFAULT 1,
    sort_order INT UNSIGNED NOT NULL DEFAULT 0,
    properties_json LONGTEXT NULL,
    created_by INT UNSIGNED NULL,
    created_at DATETIME NOT NULL,
    updated_by INT UNSIGNED NULL,
    updated_at DATETIME NULL,
    PRIMARY KEY (workflow_validation_id),
    KEY idx_workflow_validation_field (workflow_field_id, status, sort_order),
    CONSTRAINT fk_workflow_validation_field FOREIGN KEY (workflow_field_id)
        REFERENCES workflow_field (workflow_field_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS workflow_condition (
    workflow_condition_id INT UNSIGNED NOT NULL AUTO_INCREMENT,
    workflow_version_id INT UNSIGNED NOT NULL,
    target_type VARCHAR(20) NOT NULL,
    target_id INT UNSIGNED NOT NULL,
    action_type VARCHAR(30) NOT NULL,
    condition_group VARCHAR(100) NOT NULL DEFAULT 'default',
    logical_operator VARCHAR(3) NOT NULL DEFAULT 'AND',
    source_field_code VARCHAR(100) NULL,
    comparison_operator VARCHAR(30) NULL,
    comparison_value LONGTEXT NULL,
    expression_json LONGTEXT NULL,
    status TINYINT UNSIGNED NOT NULL DEFAULT 1,
    sort_order INT UNSIGNED NOT NULL DEFAULT 0,
    properties_json LONGTEXT NULL,
    created_by INT UNSIGNED NULL,
    created_at DATETIME NOT NULL,
    updated_by INT UNSIGNED NULL,
    updated_at DATETIME NULL,
    PRIMARY KEY (workflow_condition_id),
    KEY idx_workflow_condition_version_target (workflow_version_id, target_type, target_id, status, sort_order),
    KEY idx_workflow_condition_source_field (workflow_version_id, source_field_code),
    CONSTRAINT fk_workflow_condition_version FOREIGN KEY (workflow_version_id)
        REFERENCES workflow_version (workflow_version_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS workflow_calculation (
    workflow_calculation_id INT UNSIGNED NOT NULL AUTO_INCREMENT,
    workflow_field_id INT UNSIGNED NOT NULL,
    calculation_type VARCHAR(20) NOT NULL,
    expression LONGTEXT NOT NULL,
    trigger_event VARCHAR(30) NOT NULL DEFAULT 'CHANGE',
    status TINYINT UNSIGNED NOT NULL DEFAULT 1,
    sort_order INT UNSIGNED NOT NULL DEFAULT 0,
    properties_json LONGTEXT NULL,
    created_by INT UNSIGNED NULL,
    created_at DATETIME NOT NULL,
    updated_by INT UNSIGNED NULL,
    updated_at DATETIME NULL,
    PRIMARY KEY (workflow_calculation_id),
    KEY idx_workflow_calculation_field (workflow_field_id, status, sort_order),
    CONSTRAINT fk_workflow_calculation_field FOREIGN KEY (workflow_field_id)
        REFERENCES workflow_field (workflow_field_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS workflow_permission (
    workflow_permission_id INT UNSIGNED NOT NULL AUTO_INCREMENT,
    workflow_version_id INT UNSIGNED NOT NULL,
    target_type VARCHAR(20) NOT NULL,
    target_id INT UNSIGNED NOT NULL,
    role_code VARCHAR(100) NOT NULL,
    can_view TINYINT UNSIGNED NOT NULL DEFAULT 1,
    can_edit TINYINT UNSIGNED NOT NULL DEFAULT 0,
    can_require TINYINT UNSIGNED NOT NULL DEFAULT 0,
    status TINYINT UNSIGNED NOT NULL DEFAULT 1,
    created_by INT UNSIGNED NULL,
    created_at DATETIME NOT NULL,
    updated_by INT UNSIGNED NULL,
    updated_at DATETIME NULL,
    PRIMARY KEY (workflow_permission_id),
    UNIQUE KEY uq_workflow_permission_scope_role (workflow_version_id, target_type, target_id, role_code),
    KEY idx_workflow_permission_role (role_code, status),
    CONSTRAINT fk_workflow_permission_version FOREIGN KEY (workflow_version_id)
        REFERENCES workflow_version (workflow_version_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS workflow_event (
    workflow_event_id INT UNSIGNED NOT NULL AUTO_INCREMENT,
    workflow_version_id INT UNSIGNED NOT NULL,
    target_type VARCHAR(20) NOT NULL DEFAULT 'WORKFLOW',
    target_id INT UNSIGNED NOT NULL DEFAULT 0,
    event_name VARCHAR(30) NOT NULL,
    handler_type VARCHAR(20) NOT NULL,
    handler_value LONGTEXT NOT NULL,
    status TINYINT UNSIGNED NOT NULL DEFAULT 1,
    sort_order INT UNSIGNED NOT NULL DEFAULT 0,
    properties_json LONGTEXT NULL,
    created_by INT UNSIGNED NULL,
    created_at DATETIME NOT NULL,
    updated_by INT UNSIGNED NULL,
    updated_at DATETIME NULL,
    PRIMARY KEY (workflow_event_id),
    KEY idx_workflow_event_version_event (workflow_version_id, event_name, status, sort_order),
    CONSTRAINT fk_workflow_event_version FOREIGN KEY (workflow_version_id)
        REFERENCES workflow_version (workflow_version_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS workflow_translation (
    workflow_translation_id INT UNSIGNED NOT NULL AUTO_INCREMENT,
    workflow_version_id INT UNSIGNED NOT NULL,
    target_type VARCHAR(20) NOT NULL,
    target_id INT UNSIGNED NOT NULL,
    locale VARCHAR(10) NOT NULL,
    label VARCHAR(255) NULL,
    placeholder VARCHAR(255) NULL,
    help_text TEXT NULL,
    created_by INT UNSIGNED NULL,
    created_at DATETIME NOT NULL,
    updated_by INT UNSIGNED NULL,
    updated_at DATETIME NULL,
    PRIMARY KEY (workflow_translation_id),
    UNIQUE KEY uq_workflow_translation_target_locale (workflow_version_id, target_type, target_id, locale),
    CONSTRAINT fk_workflow_translation_version FOREIGN KEY (workflow_version_id)
        REFERENCES workflow_version (workflow_version_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS workflow_instance (
    workflow_instance_id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
    workflow_id INT UNSIGNED NOT NULL,
    workflow_version_id INT UNSIGNED NOT NULL,
    uuid CHAR(36) NOT NULL,
    reference_no VARCHAR(100) NULL,
    status VARCHAR(30) NOT NULL DEFAULT 'DRAFT',
    revision INT UNSIGNED NOT NULL DEFAULT 1,
    current_workflow_group_id INT UNSIGNED NULL,
    submitted_at DATETIME NULL,
    submitted_by INT UNSIGNED NULL,
    completed_at DATETIME NULL,
    completed_by INT UNSIGNED NULL,
    properties_json LONGTEXT NULL,
    created_by INT UNSIGNED NULL,
    created_at DATETIME NOT NULL,
    updated_by INT UNSIGNED NULL,
    updated_at DATETIME NULL,
    deleted_at DATETIME NULL,
    PRIMARY KEY (workflow_instance_id),
    UNIQUE KEY uq_workflow_instance_uuid (uuid),
    KEY idx_workflow_instance_workflow_status (workflow_id, status, created_at),
    KEY idx_workflow_instance_version_status (workflow_version_id, status),
    KEY idx_workflow_instance_reference_no (reference_no),
    KEY idx_workflow_instance_current_group (current_workflow_group_id),
    CONSTRAINT fk_workflow_instance_workflow FOREIGN KEY (workflow_id)
        REFERENCES workflow (workflow_id),
    CONSTRAINT fk_workflow_instance_version FOREIGN KEY (workflow_version_id)
        REFERENCES workflow_version (workflow_version_id),
    CONSTRAINT fk_workflow_instance_current_group FOREIGN KEY (current_workflow_group_id)
        REFERENCES workflow_group (workflow_group_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS workflow_instance_group (
    workflow_instance_group_id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
    workflow_instance_id BIGINT UNSIGNED NOT NULL,
    workflow_group_id INT UNSIGNED NOT NULL,
    status VARCHAR(30) NOT NULL DEFAULT 'PENDING',
    started_at DATETIME NULL,
    completed_at DATETIME NULL,
    completed_by INT UNSIGNED NULL,
    properties_json LONGTEXT NULL,
    created_at DATETIME NOT NULL,
    updated_at DATETIME NULL,
    PRIMARY KEY (workflow_instance_group_id),
    UNIQUE KEY uq_workflow_instance_group (workflow_instance_id, workflow_group_id),
    KEY idx_workflow_instance_group_status (workflow_instance_id, status),
    CONSTRAINT fk_workflow_instance_group_instance FOREIGN KEY (workflow_instance_id)
        REFERENCES workflow_instance (workflow_instance_id),
    CONSTRAINT fk_workflow_instance_group_group FOREIGN KEY (workflow_group_id)
        REFERENCES workflow_group (workflow_group_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS workflow_instance_value (
    workflow_instance_value_id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
    workflow_instance_id BIGINT UNSIGNED NOT NULL,
    workflow_field_id INT UNSIGNED NOT NULL,
    row_uuid CHAR(36) NOT NULL DEFAULT '',
    value_text LONGTEXT NULL,
    value_number DECIMAL(20,6) NULL,
    value_date DATETIME NULL,
    value_json LONGTEXT NULL,
    created_at DATETIME NOT NULL,
    updated_at DATETIME NULL,
    PRIMARY KEY (workflow_instance_value_id),
    UNIQUE KEY uq_workflow_instance_value_field_row (workflow_instance_id, workflow_field_id, row_uuid),
    KEY idx_workflow_instance_value_field (workflow_field_id),
    KEY idx_workflow_instance_value_number (workflow_field_id, value_number),
    CONSTRAINT fk_workflow_instance_value_instance FOREIGN KEY (workflow_instance_id)
        REFERENCES workflow_instance (workflow_instance_id),
    CONSTRAINT fk_workflow_instance_value_field FOREIGN KEY (workflow_field_id)
        REFERENCES workflow_field (workflow_field_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS workflow_file (
    workflow_file_id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
    workflow_instance_id BIGINT UNSIGNED NOT NULL,
    workflow_field_id INT UNSIGNED NULL,
    row_uuid CHAR(36) NOT NULL DEFAULT '',
    original_name VARCHAR(255) NOT NULL,
    stored_name VARCHAR(255) NOT NULL,
    storage_path VARCHAR(1000) NOT NULL,
    mime_type VARCHAR(150) NULL,
    extension VARCHAR(20) NULL,
    file_size BIGINT UNSIGNED NOT NULL DEFAULT 0,
    checksum_sha256 CHAR(64) NULL,
    uploaded_by INT UNSIGNED NULL,
    uploaded_at DATETIME NOT NULL,
    deleted_at DATETIME NULL,
    PRIMARY KEY (workflow_file_id),
    KEY idx_workflow_file_instance_field (workflow_instance_id, workflow_field_id),
    KEY idx_workflow_file_checksum (checksum_sha256),
    CONSTRAINT fk_workflow_file_instance FOREIGN KEY (workflow_instance_id)
        REFERENCES workflow_instance (workflow_instance_id),
    CONSTRAINT fk_workflow_file_field FOREIGN KEY (workflow_field_id)
        REFERENCES workflow_field (workflow_field_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS workflow_log (
    workflow_log_id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
    workflow_instance_id BIGINT UNSIGNED NOT NULL,
    workflow_instance_group_id BIGINT UNSIGNED NULL,
    event_type VARCHAR(50) NOT NULL,
    message VARCHAR(1000) NULL,
    old_value_json LONGTEXT NULL,
    new_value_json LONGTEXT NULL,
    ip_address VARCHAR(45) NULL,
    user_agent VARCHAR(1000) NULL,
    created_by INT UNSIGNED NULL,
    created_at DATETIME NOT NULL,
    PRIMARY KEY (workflow_log_id),
    KEY idx_workflow_log_instance_created (workflow_instance_id, created_at),
    KEY idx_workflow_log_event (event_type, created_at),
    CONSTRAINT fk_workflow_log_instance FOREIGN KEY (workflow_instance_id)
        REFERENCES workflow_instance (workflow_instance_id),
    CONSTRAINT fk_workflow_log_instance_group FOREIGN KEY (workflow_instance_group_id)
        REFERENCES workflow_instance_group (workflow_instance_group_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

SET FOREIGN_KEY_CHECKS = 1;
