<?php

/**
 * Migration 021 – Add page_type column to iw_pages
 *
 * Introduces a page_type column to distinguish between regular pages
 * and system/module pages (gallery, blog, events, downloads, contact).
 * Also creates page entries for existing system/module pages so they
 * become editable in /admin/pages.
 *
 * Backward-compatible: existing rows get page_type='page' (default),
 * except contact pages which get page_type='contact'.
 */

use App\Database\Migration;

class AddPageTypeToPages extends Migration
{
    public function up(): void
    {
        // 1. Add page_type column if it doesn't exist
        $columns = $this->db->fetchAll("SHOW COLUMNS FROM iw_pages LIKE 'page_type'");
        if (empty($columns)) {
            $this->db->query("ALTER TABLE iw_pages ADD COLUMN page_type VARCHAR(50) NOT NULL DEFAULT 'page' AFTER show_title");
            $this->db->query("ALTER TABLE iw_pages ADD INDEX idx_page_type (page_type)");
        }

        // 2. Set page_type='contact' for existing contact pages (identified by template='contact')
        $this->db->query("UPDATE iw_pages SET page_type = 'contact' WHERE template = 'contact' AND page_type = 'page'");

        // 3. Set page_type='home' for existing homepage pages (identified by template='home')
        $this->db->query("UPDATE iw_pages SET page_type = 'home' WHERE template = 'home' AND page_type = 'page'");

        // 4. Insert system/module page entries if they don't already exist
        $systemPages = [
            [
                'page_type'  => 'gallery',
                'slug'       => 'gallery',
                'title'      => 'Gallery',
                'template'   => 'gallery',
                'sort_order' => 20,
            ],
            [
                'page_type'  => 'blog',
                'slug'       => 'blog',
                'title'      => 'Blog',
                'template'   => 'blog',
                'sort_order' => 21,
            ],
            [
                'page_type'  => 'events',
                'slug'       => 'events',
                'title'      => 'Events',
                'template'   => 'events',
                'sort_order' => 22,
            ],
            [
                'page_type'  => 'downloads',
                'slug'       => 'download',
                'title'      => 'Downloads',
                'template'   => 'downloads',
                'sort_order' => 23,
            ],
        ];

        foreach ($systemPages as $sp) {
            // Check if a page with this slug already exists
            $existing = $this->db->fetch("SELECT id FROM iw_pages WHERE slug = :slug LIMIT 1", ['slug' => $sp['slug']]);
            if (!$existing) {
                $this->db->insert('iw_pages', [
                    'title'           => $sp['title'],
                    'slug'            => $sp['slug'],
                    'content'         => '',
                    'template'        => $sp['template'],
                    'status'          => 'published',
                    'sort_order'      => $sp['sort_order'],
                    'is_homepage'     => 0,
                    'show_title'      => 1,
                    'page_type'       => $sp['page_type'],
                    'seo_title'       => '',
                    'seo_description' => '',
                    'created_at'      => date('Y-m-d H:i:s'),
                    'updated_at'      => date('Y-m-d H:i:s'),
                ]);
            } else {
                // Update existing page to set the correct page_type if still 'page'
                $this->db->query(
                    "UPDATE iw_pages SET page_type = :type WHERE id = :id AND page_type = 'page'",
                    ['type' => $sp['page_type'], 'id' => $existing['id']]
                );
            }
        }
    }

    public function down(): void
    {
        $this->db->query("DROP INDEX idx_page_type ON iw_pages");
        $this->db->query("ALTER TABLE iw_pages DROP COLUMN page_type");
    }
}
