<?php

namespace App\Database\Migrations;

use CodeIgniter\Database\Migration;

class CreateInspectionAnswersTable extends Migration
{
    public function up()
    {
        $this->forge->addField([
            'id' => ['type' => 'INT', 'constraint' => 11, 'unsigned' => true, 'auto_increment' => true],
            'inspection_id' => ['type' => 'INT', 'constraint' => 11, 'unsigned' => true],
            'item_id' => ['type' => 'INT', 'constraint' => 11, 'unsigned' => true],
            'value_bool' => ['type' => 'TINYINT', 'constraint' => 1, 'null' => true, 'comment' => '1=S 0=N 2=N/A'],
            'value_text' => ['type' => 'TEXT', 'null' => true],
            'value_number' => ['type' => 'DECIMAL', 'constraint' => '12,3', 'null' => true],
            'value_json' => ['type' => 'JSON', 'null' => true],
            'comentarios' => ['type' => 'TEXT', 'null' => true],
            'lecturas' => ['type' => 'VARCHAR', 'constraint' => 100, 'null' => true],
            'updated_at' => ['type' => 'DATETIME', 'null' => true],
        ]);
        $this->forge->addKey('id', true);
        $this->forge->addUniqueKey(['inspection_id', 'item_id']);
        $this->forge->addForeignKey('inspection_id', 'inspections', 'id', 'CASCADE', 'CASCADE');
        $this->forge->addForeignKey('item_id', 'inspection_template_items', 'id', 'CASCADE', 'CASCADE');
        $this->forge->createTable('inspection_answers');
    }

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