<?php

/**
 * Migration 0012 — Local library tables (per-user Sections + Scenes)
 *
 * Schema mirrors Platform's `{prefix}sections` table on platform.instinctor.com
 * (see platform_v2/migrations/0003_library.php) so local items can be promoted
 * to the global library by an admin without shape translation.
 *
 * Scope is per-user via `created_by`. A designer's sections follow them across
 * every page they build on this install. Scenes are a separate table because
 * they're surfaced in a different UI (Page Grid, not editor palette).
 *
 * @since 2.1.159
 */

if (!defined('ABSPATH')) exit;
return [
    'description' => 'Local per-user library tables (sections + scenes)',

    'up' => [
        "CREATE TABLE IF NOT EXISTS {prefix}instinctor_library_sections (
            id           int unsigned NOT NULL AUTO_INCREMENT,
            created_by   int unsigned NOT NULL,
            name         varchar(255) NOT NULL,
            category     varchar(100) NOT NULL DEFAULT 'other',
            tags         varchar(500) NOT NULL DEFAULT '',
            element_tree longtext     NOT NULL,
            html_preview longtext     NOT NULL,
            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 created_by (created_by),
            KEY sort_order (sort_order)
        ) {charset}",

        "CREATE TABLE IF NOT EXISTS {prefix}instinctor_library_scenes (
            id           int unsigned NOT NULL AUTO_INCREMENT,
            created_by   int unsigned NOT NULL,
            name         varchar(255) NOT NULL,
            category     varchar(100) NOT NULL DEFAULT 'other',
            tags         varchar(500) NOT NULL DEFAULT '',
            element_tree longtext     NOT NULL,
            html_preview longtext     NOT NULL,
            page_settings longtext    NOT NULL,
            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 created_by (created_by),
            KEY sort_order (sort_order)
        ) {charset}",
    ],

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