<?php
/**
 * ITSWEBER CMS – Migration 030: Section Revisions
 *
 * Creates the iw_section_revisions table for content versioning
 * in the page builder. Stores up to 10 revisions per section (FIFO).
 *
 * @author    Benny Weber (ITSWEBER)
 * @copyright © ITSWEBER – www.itsweber.de
 * @contact   kontakt@itsweber.de
 */

use App\Database\Migration;

class CreateSectionRevisions extends Migration
{
    public function up(): void
    {
        $this->db->query("
            CREATE TABLE IF NOT EXISTS iw_section_revisions (
                id              INT AUTO_INCREMENT PRIMARY KEY,
                section_id      INT          NOT NULL,
                content         LONGTEXT     NOT NULL,
                changed_by      INT          DEFAULT NULL,
                created_at      DATETIME     DEFAULT CURRENT_TIMESTAMP,
                INDEX idx_section_id (section_id),
                INDEX idx_created (section_id, created_at),
                CONSTRAINT fk_revision_section FOREIGN KEY (section_id) REFERENCES iw_page_sections(id) ON DELETE CASCADE,
                CONSTRAINT fk_revision_user FOREIGN KEY (changed_by) REFERENCES iw_users(id) ON DELETE SET NULL
            ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci
        ");
    }

    public function down(): void
    {
        $this->db->query("DROP TABLE IF EXISTS iw_section_revisions");
    }
}
