<?php

namespace App\Database\Migrations;

use CodeIgniter\Database\Migration;

class CreateAssessmentDetailScoresTable extends Migration
{
    public function up()
    {
        $this->forge->addField([
            'id'            => ['type' => 'BIGINT', 'unsigned' => true, 'auto_increment' => true],
            'score_id'      => ['type' => 'BIGINT', 'unsigned' => true],         // FK ke assessment_scores
            'jenis'         => ['type' => 'ENUM', 'constraint' => ['isian', 'essay']], // jenis soal
            'nomor_soal'    => ['type' => 'SMALLINT', 'unsigned' => true],       // nomor soal ke-berapa
            'skor'          => ['type' => 'DECIMAL', 'constraint' => '5,2', 'null' => true], // skor per nomor
            'created_at'    => ['type' => 'DATETIME', 'null' => true],
            'updated_at'    => ['type' => 'DATETIME', 'null' => true],
        ]);

        $this->forge->addPrimaryKey('id');
        $this->forge->addForeignKey('score_id', 'assessment_scores', 'id', 'CASCADE', 'CASCADE');

        // 1 score + 1 jenis + 1 nomor soal = 1 record
        $this->forge->addUniqueKey(['score_id', 'jenis', 'nomor_soal']);

        $this->forge->createTable('assessment_detail_scores');
    }

    public function down()
    {
        $this->forge->dropTable('assessment_detail_scores');
    }
}