<?php

use App\Database\Migration;

class AddShowTitleToPages extends Migration
{
    public function up(): void
    {
        // Idempotency check: only add column if it doesn't already exist
        $exists = (int) $this->db->fetchColumn(
            "SELECT COUNT(*)
             FROM   information_schema.COLUMNS
             WHERE  TABLE_SCHEMA = DATABASE()
               AND  TABLE_NAME   = 'iw_pages'
               AND  COLUMN_NAME  = 'show_title'"
        );

        if ($exists === 0) {
            $this->db->query("
                ALTER TABLE iw_pages
                ADD COLUMN show_title TINYINT(1) NOT NULL DEFAULT 1
                AFTER is_homepage
            ");
        }
    }

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