ALTER TABLE integrations
    ADD COLUMN order_status_sync_direction VARCHAR(32) NOT NULL DEFAULT 'shoppro_to_orderpro' AFTER orders_fetch_start_date;

CREATE TABLE IF NOT EXISTS integration_order_status_sync_state (
    integration_id INT UNSIGNED NOT NULL,
    direction VARCHAR(32) NOT NULL,
    last_synced_at DATETIME NULL,
    last_synced_order_ref VARCHAR(64) NULL,
    last_run_at DATETIME NULL,
    last_error VARCHAR(500) NULL,
    created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    PRIMARY KEY (integration_id, direction),
    KEY integration_order_status_sync_state_direction_idx (direction),
    CONSTRAINT integration_order_status_sync_state_integration_fk
        FOREIGN KEY (integration_id) REFERENCES integrations(id)
        ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

INSERT INTO cron_schedules (
    job_type, interval_seconds, priority, max_attempts, payload, enabled, last_run_at, next_run_at, created_at, updated_at
) VALUES (
    'shoppro_order_status_sync',
    3600,
    95,
    3,
    NULL,
    1,
    NULL,
    NOW(),
    NOW(),
    NOW()
)
ON DUPLICATE KEY UPDATE
    interval_seconds = VALUES(interval_seconds),
    priority = VALUES(priority),
    max_attempts = VALUES(max_attempts),
    payload = VALUES(payload),
    enabled = VALUES(enabled),
    updated_at = VALUES(updated_at);
