<?php

namespace App\Database\Migrations;

use CodeIgniter\Database\Migration;

class CreateInspectionTemplateItemsTable extends Migration
{
    public function up()
    {
        $this->forge->addField([
            'id' => ['type' => 'INT', 'constraint' => 11, 'unsigned' => true, 'auto_increment' => true],
            'template_id' => ['type' => 'INT', 'constraint' => 11, 'unsigned' => true],
            'section_id' => ['type' => 'INT', 'constraint' => 11, 'unsigned' => true, 'null' => true],
            'numero' => ['type' => 'VARCHAR', 'constraint' => 20, 'null' => true],
            'pregunta' => ['type' => 'TEXT'],
            'frecuencia' => ['type' => 'VARCHAR', 'constraint' => 2, 'null' => true],
            'tipo_campo' => [
                'type' => "ENUM('yes_no_na','text','numeric','checkbox','switch','attachment','signature')",
                'default' => 'yes_no_na',
            ],
            'opciones' => ['type' => 'JSON', 'null' => true],
            'orden' => ['type' => 'INT', 'constraint' => 5, 'default' => 0],
            'obligatorio' => ['type' => 'TINYINT', 'constraint' => 1, 'default' => 0],
        ]);
        $this->forge->addKey('id', true);
        $this->forge->addKey('template_id');
        $this->forge->addKey('section_id');
        $this->forge->addForeignKey('template_id', 'inspection_templates', 'id', 'CASCADE', 'CASCADE');
        $this->forge->addForeignKey('section_id', 'inspection_template_sections', 'id', 'SET NULL', 'CASCADE');
        $this->forge->createTable('inspection_template_items');
    }

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