<?php
declare(strict_types=1);

return [
    'up' => "
        CREATE TABLE IF NOT EXISTS `workflow_instances` (
            `id`                     BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
            `workflow_definition_id` BIGINT UNSIGNED NOT NULL,
            `workflow_code`          VARCHAR(50) NOT NULL,
            `entity_type`            VARCHAR(100) NOT NULL,
            `entity_id`              BIGINT UNSIGNED NOT NULL,
            `current_state`          VARCHAR(50) NOT NULL,
            `state_entered_at`       TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
            `state_data_json`        JSON NULL,
            `is_completed`           TINYINT(1) NOT NULL DEFAULT 0,
            `completed_at`           TIMESTAMP NULL DEFAULT NULL,
            `created_at`             TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
            `updated_at`             TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
            INDEX `idx_workflow_inst_def` (`workflow_definition_id`),
            INDEX `idx_workflow_inst_entity` (`entity_type`, `entity_id`),
            INDEX `idx_workflow_inst_state` (`current_state`),
            INDEX `idx_workflow_inst_completed` (`is_completed`),
            CONSTRAINT `fk_workflow_inst_def` FOREIGN KEY (`workflow_definition_id`) REFERENCES `workflow_definitions`(`id`)
        ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci
    ",
    'down' => "DROP TABLE IF EXISTS `workflow_instances`",
];