<?php

namespace App\Database\Migrations;

use CodeIgniter\Database\Migration;

class CreateParentAttendanceReports extends Migration
{
    public function up()
    {
        if ($this->db->tableExists('parent_attendance_reports')) {
            return; // already created
        }

        $this->forge->addField([
            'id' => [
                'type'           => 'INT',
                'unsigned'       => true,
                'auto_increment' => true,
            ],
            'parent_id' => [
                'type'     => 'INT',
                'unsigned' => true,
                'null'     => false,
                'comment'  => 'users.id of the reporting parent (primary parent id)'
            ],
            'student_id' => [
                'type'     => 'INT',
                'unsigned' => true,
                'null'     => false,
            ],
            'class_section_id' => [
                'type'     => 'INT',
                'unsigned' => true,
                'null'     => true,
            ],
            'report_date' => [
                'type' => 'DATE',
                'null' => false,
            ],
            'type' => [
                'type'       => 'ENUM',
                'constraint' => ['absent', 'late', 'early_dismissal'],
                'default'    => 'absent',
                'null'       => false,
            ],
            'arrival_time' => [
                'type' => 'TIME', // for late
                'null' => true,
            ],
            'dismiss_time' => [
                'type' => 'TIME', // for early dismissal
                'null' => true,
            ],
            'reason' => [
                'type'       => 'TEXT',
                'null'       => true,
            ],
            'semester' => [
                'type'       => 'VARCHAR',
                'constraint' => 32,
                'null'       => true,
            ],
            'school_year' => [
                'type'       => 'VARCHAR',
                'constraint' => 16,
                'null'       => true,
            ],
            'status' => [
                'type'       => 'ENUM',
                'constraint' => ['new','seen','processed'],
                'default'    => 'new',
                'null'       => false,
            ],
            'created_at' => [
                'type' => 'DATETIME',
                'null' => true,
            ],
            'updated_at' => [
                'type' => 'DATETIME',
                'null' => true,
            ],
        ]);

        $this->forge->addKey('id', true);
        $this->forge->addKey(['report_date']);
        $this->forge->addKey(['student_id', 'report_date']);
        $this->forge->addForeignKey('parent_id', 'users', 'id', 'CASCADE', 'CASCADE');
        $this->forge->addForeignKey('student_id', 'students', 'id', 'CASCADE', 'CASCADE');
        $this->forge->createTable('parent_attendance_reports', true);
    }

    public function down()
    {
        if ($this->db->tableExists('parent_attendance_reports')) {
            $this->forge->dropTable('parent_attendance_reports', true);
        }
    }
}

