<?php

namespace App\Database\Migrations;

use CodeIgniter\Database\Migration;

class CreateStudentViolationsTable extends Migration
{
    public function up()
    {
        $this->forge->addField([
            'id' => [
                'type'           => 'INT',
                'constraint'     => 11,
                'unsigned'       => true,
                'auto_increment' => true,
            ],
            'student_id' => [
                'type'       => 'INT',
                'constraint' => 11,
                'unsigned'   => true,
            ],
            'class_id' => [
                'type'       => 'INT',
                'constraint' => 11,
                'unsigned'   => true,
                'null'       => true,
            ],
            'violation_id' => [
                'type'       => 'INT',
                'constraint' => 11,
                'unsigned'   => true,
            ],
            'reported_by_user_id' => [
                'type'       => 'INT',
                'constraint' => 11,
                'unsigned'   => true,
            ],
            'occurred_at' => [
                'type' => 'DATETIME',
                'null' => true,
            ],
            'notes' => [
                'type' => 'TEXT',
                'null' => true,
            ],
            'created_at' => [
                'type' => 'DATETIME',
                'null' => true,
            ],
            'updated_at' => [
                'type' => 'DATETIME',
                'null' => true,
            ],
        ]);

        $this->forge->addKey('id', true);
        $this->forge->addKey('student_id');
        $this->forge->addKey('class_id');
        $this->forge->addKey('occurred_at');
        $this->forge->addForeignKey('student_id', 'students', 'id', 'CASCADE', 'CASCADE', 'student_violations_student_fk');
        $this->forge->addForeignKey('class_id', 'classes', 'id', 'SET NULL', 'CASCADE', 'student_violations_class_fk');
        $this->forge->addForeignKey('violation_id', 'violations', 'id', 'CASCADE', 'CASCADE', 'student_violations_violation_fk');
        $this->forge->addForeignKey('reported_by_user_id', 'users', 'id', 'CASCADE', 'CASCADE', 'student_violations_reporter_fk');
        $this->forge->createTable('student_violations');
    }

    public function down()
    {
        $this->forge->dropForeignKey('student_violations', 'student_violations_student_fk');
        $this->forge->dropForeignKey('student_violations', 'student_violations_class_fk');
        $this->forge->dropForeignKey('student_violations', 'student_violations_violation_fk');
        $this->forge->dropForeignKey('student_violations', 'student_violations_reporter_fk');
        $this->forge->dropTable('student_violations');
    }
}

