<?php

/**
 * Migration 0005 — Impulse pages table
 *
 * Creates instinctor_pages as the canonical store for all Impulse-built pages.
 * Elements and settings are stored as JSON columns, eliminating wp_postmeta.
 *
 * On WordPress: a WP post shell is still created/deleted for URL routing,
 * but all data (elements, settings, thumbnail, type) lives here.
 * On standalone: this table is the sole page store.
 *
 * The one-time copy of existing WP page data into this table is handled by
 * PageService::migrateFromWordPress(), called from Plugin::init() on WP only.
 * It is idempotent and guarded by the 'instinctor_pages_migrated' Config flag.
 *
 * @since 3.18.56
 */

if (!defined('ABSPATH')) exit;
return [
    'description' => 'Impulse pages table',

    'up' => [
        "CREATE TABLE IF NOT EXISTS {prefix}instinctor_pages (
            id          int unsigned  NOT NULL AUTO_INCREMENT,
            name        varchar(255)  NOT NULL DEFAULT 'Untitled Page',
            slug        varchar(255)  NOT NULL DEFAULT '',
            status      varchar(20)   NOT NULL DEFAULT 'draft',
            type        varchar(50)   NOT NULL DEFAULT 'page',
            elements    longtext      NOT NULL,
            settings    longtext      NOT NULL,
            thumbnail   varchar(512)  NOT NULL DEFAULT '',
            sort_order  int           NOT NULL DEFAULT 0,
            created_at  datetime      NOT NULL DEFAULT CURRENT_TIMESTAMP,
            updated_at  datetime      NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
            PRIMARY KEY (id),
            KEY status (status),
            KEY type (type),
            KEY sort_order (sort_order)
        ) {charset}",
    ],

    'down' => [
        "DROP TABLE IF EXISTS {prefix}instinctor_pages",
    ],
];
