<?php

namespace App\Database\Migrations;

use CodeIgniter\Database\Migration;

class CreateCompetitionScores extends Migration
{
    public function up()
    {
        $this->forge->addField([
            'id'             => ['type' => 'INT', 'constraint' => 11, 'unsigned' => true, 'auto_increment' => true],
            'competition_id' => ['type' => 'INT', 'constraint' => 11, 'unsigned' => true],
            'student_id'     => ['type' => 'INT', 'constraint' => 11, 'unsigned' => true],
            'class_section_id' => ['type' => 'INT', 'constraint' => 11, 'null' => true],
            'score'          => ['type' => 'DECIMAL', 'constraint' => '6,2', 'default' => 0],
            'notes'          => ['type' => 'VARCHAR', 'constraint' => 255, 'null' => true],

            'created_at'     => ['type' => 'DATETIME', 'null' => true],
            'updated_at'     => ['type' => 'DATETIME', 'null' => true],
        ]);

        $this->forge->addKey('id', true);
        $this->forge->addKey(['competition_id']);
        $this->forge->addKey(['student_id']);
        $this->forge->addKey(['class_section_id']);
        $this->forge->addUniqueKey(['competition_id', 'student_id', 'class_section_id']);
        $this->forge->createTable('competition_scores', true);
    }

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