<?php

namespace App\Database\Migrations;

use CodeIgniter\Database\Migration;

class RebuildPdfTemplatesTable extends Migration
{
    public function up()
    {
        // Reemplaza por completo la tabla de la v1 (PDF de fondo + rectángulos).
        $this->forge->dropTable('pdf_templates', true);

        $this->forge->addField([
            'id' => [
                'type'           => 'INT',
                'constraint'     => 11,
                'unsigned'       => true,
                'auto_increment' => true,
            ],
            'name' => [
                'type'       => 'VARCHAR',
                'constraint' => 160,
                'default'    => 'Mi plantilla',
            ],
            'page_width_mm' => [
                'type'       => 'DECIMAL',
                'constraint' => '7,2',
                'default'    => 215.90,
            ],
            'page_height_mm' => [
                'type'       => 'DECIMAL',
                'constraint' => '7,2',
                'default'    => 279.40,
            ],
            'page_count' => [
                'type'       => 'INT',
                'constraint' => 5,
                'unsigned'   => true,
                'default'    => 1,
            ],
            'schema_json' => [
                'type' => 'LONGTEXT',
                'null' => true,
            ],
            'is_active' => [
                'type'       => 'TINYINT',
                'constraint' => 1,
                'default'    => 0,
            ],
            'created_at' => [
                'type' => 'DATETIME',
                'null' => true,
            ],
            'updated_at' => [
                'type' => 'DATETIME',
                'null' => true,
            ],
        ]);

        $this->forge->addKey('id', true);
        $this->forge->addKey('is_active');
        $this->forge->createTable('pdf_templates');
    }

    public function down()
    {
        $this->forge->dropTable('pdf_templates', true);
    }
}
