<?php

namespace App\Database\Migrations;

use CodeIgniter\Database\Migration;

class CreateLegerImportsTable extends Migration
{
    public function up()
    {
        $this->forge->addField([
            'id'                 => ['type' => 'BIGINT', 'unsigned' => true, 'auto_increment' => true],
            'batch_code'         => ['type' => 'VARCHAR', 'constraint' => 50],
            'classroom_id'       => ['type' => 'BIGINT', 'unsigned' => true, 'null' => true],
            'academic_period_id' => ['type' => 'BIGINT', 'unsigned' => true, 'null' => true],
            'file_name'          => ['type' => 'VARCHAR', 'constraint' => 255],
            'class_name_raw'     => ['type' => 'VARCHAR', 'constraint' => 100, 'null' => true],
            'semester_raw'       => ['type' => 'VARCHAR', 'constraint' => 50,  'null' => true],
            'year_raw'           => ['type' => 'VARCHAR', 'constraint' => 20,  'null' => true],
            'total_students'     => ['type' => 'INT', 'default' => 0],
            'total_subjects'     => ['type' => 'INT', 'default' => 0],
            'imported_by'        => ['type' => 'BIGINT', 'unsigned' => true, 'null' => true],
            'created_at'         => ['type' => 'DATETIME', 'null' => true],
            'updated_at'         => ['type' => 'DATETIME', 'null' => true],
        ]);

        $this->forge->addPrimaryKey('id');
        $this->forge->addForeignKey('classroom_id',       'classrooms',       'id', 'SET NULL', 'CASCADE');
        $this->forge->addForeignKey('academic_period_id', 'academic_periods', 'id', 'SET NULL', 'CASCADE');
        $this->forge->addForeignKey('imported_by',        'users',            'id', 'SET NULL', 'CASCADE');

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

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