<?php

/**
 * Migration 049 – Add 404 system page.
 *
 * Creates a CMS-editable 404 page so site owners can customize the "not found" message.
 */

use App\Database\Migration;

class Add404SystemPage extends Migration
{
    public function up(): void
    {
        // Only insert if no page with slug '404' or page_type '404' exists yet
        $existing = $this->db->fetch(
            "SELECT id FROM iw_pages WHERE slug = '404' OR page_type = '404' LIMIT 1"
        );

        if (!$existing) {
            $this->db->insert('iw_pages', [
                'title'      => '404 – Page Not Found',
                'slug'       => '404',
                'content'    => '<p>The page you are looking for does not exist or has been moved.</p>',
                'template'   => '404',
                'status'     => 'published',
                'sort_order' => 999,
                'is_homepage' => 0,
                'show_title' => 1,
                'page_type'  => '404',
                'created_at' => date('Y-m-d H:i:s'),
                'updated_at' => date('Y-m-d H:i:s'),
            ]);
        }
    }

    public function down(): void
    {
        $this->db->query("DELETE FROM iw_pages WHERE page_type = '404'");
    }
}
