<?php

namespace App\Database\Migrations;

use CodeIgniter\Database\Migration;

class CreatePlacementLevels extends Migration
{
    public function up()
    {
        if ($this->db->tableExists('placement_levels')) {
            return;
        }

        $this->forge->addField([
            'id' => ['type' => 'INT', 'constraint' => 11, 'unsigned' => true, 'auto_increment' => true],
            'student_id' => ['type' => 'INT', 'constraint' => 11, 'unsigned' => true],
            'level' => ['type' => 'SMALLINT', 'constraint' => 3, 'unsigned' => true],
            'school_year' => ['type' => 'VARCHAR', 'constraint' => 20],
            'created_by' => ['type' => 'INT', 'constraint' => 11, 'unsigned' => true, 'null' => true],
            'updated_by' => ['type' => 'INT', 'constraint' => 11, 'unsigned' => true, 'null' => true],
            'created_at' => ['type' => 'DATETIME', 'null' => true],
            'updated_at' => ['type' => 'DATETIME', 'null' => true],
        ]);

        $this->forge->addKey('id', true);
        $this->forge->addUniqueKey(['student_id', 'school_year'], 'unique_student_school_year');
        $this->forge->addKey('school_year');
        $this->forge->createTable('placement_levels');
    }

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