<?php

namespace App\Database\Migrations;

use CodeIgniter\Database\Migration;

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

        $this->forge->addField([
            'id' => [
                'type' => 'INT',
                'constraint' => 11,
                'unsigned' => true,
                'auto_increment' => true,
            ],
            'class_section_id' => [
                'type' => 'INT',
                'constraint' => 11,
                'unsigned' => true,
            ],
            'semester' => [
                'type' => 'VARCHAR',
                'constraint' => 20,
            ],
            'school_year' => [
                'type' => 'VARCHAR',
                'constraint' => 9,
            ],
            'is_locked' => [
                'type' => 'TINYINT',
                'constraint' => 1,
                'default' => 0,
            ],
            'locked_by' => [
                'type' => 'INT',
                'constraint' => 11,
                'null' => true,
            ],
            'locked_at' => [
                'type' => 'DATETIME',
                'null' => true,
            ],
            'created_at' => [
                'type' => 'DATETIME',
                'null' => true,
            ],
            'updated_at' => [
                'type' => 'DATETIME',
                'null' => true,
            ],
        ]);

        $this->forge->addKey('id', true);
        $this->forge->addKey(['class_section_id', 'semester', 'school_year'], false, false, 'idx_grading_locks_section_term');
        $this->forge->addUniqueKey(['class_section_id', 'semester', 'school_year'], 'uniq_grading_locks_section_term');
        $this->forge->createTable('grading_locks', true);
    }

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