<?php

namespace App\Database\Migrations;

use CodeIgniter\Database\Migration;

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

        $this->forge->addField([
            'id' => [
                'type'           => 'INT',
                'constraint'     => 11,
                'unsigned'       => true,
                'auto_increment' => true,
            ],
            'certificate_number' => [
                'type'       => 'VARCHAR',
                'constraint' => 30,
            ],
            'student_id' => [
                'type'       => 'INT',
                'constraint' => 11,
                'unsigned'   => true,
            ],
            'student_name' => [
                'type'       => 'VARCHAR',
                'constraint' => 200,
            ],
            'grade' => [
                'type'       => 'VARCHAR',
                'constraint' => 100,
                'null'       => true,
            ],
            'cert_date' => [
                'type' => 'DATE',
                'null' => true,
            ],
            'school_year' => [
                'type'       => 'VARCHAR',
                'constraint' => 20,
                'null'       => true,
            ],
            'class_section_id' => [
                'type'       => 'INT',
                'constraint' => 11,
                'unsigned'   => true,
                'null'       => true,
            ],
            'issued_by' => [
                'type'       => 'INT',
                'constraint' => 11,
                'unsigned'   => true,
                'null'       => true,
            ],
            'issued_at' => [
                'type' => 'DATETIME',
                'null' => true,
            ],
            'created_at' => [
                'type' => 'DATETIME',
                'null' => true,
            ],
            'updated_at' => [
                'type' => 'DATETIME',
                'null' => true,
            ],
        ]);

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

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