<?php

namespace App\Database\Migrations;

use CodeIgniter\Database\Forge; // optional
use CodeIgniter\Database\RawSql;
use CodeIgniter\Database\Migration;

class CreateWhatsappGroupMemberships extends Migration
{
    public function up()
    {
        $this->forge->addField([
            'id' => [
                'type'           => 'INT',
                'constraint'     => 11,
                'unsigned'       => true,
                'auto_increment' => true,
            ],
            'class_section_id' => [
                'type'       => 'INT',
                'constraint' => 11,
                'null'       => false,
            ],
            'school_year' => [
                'type'       => 'VARCHAR',
                'constraint' => 25,
                'null'       => false,
            ],
            'semester' => [
                'type'       => 'VARCHAR',
                'constraint' => 20,
                'null'       => false,
            ],
            'subject_type' => [
                'type'       => 'ENUM',
                'constraint' => ['primary', 'second'],
                'null'       => false,
                'default'    => 'primary',
            ],
            'subject_id' => [
                'type'       => 'INT',
                'constraint' => 11,
                'null'       => false,
                'comment'    => 'users.id for primary; parents.id for second',
            ],
            'is_member' => [
                'type'       => 'TINYINT',
                'constraint' => 1,
                'null'       => false,
                'default'    => 0,
            ],
            'verified_by' => [
                'type'       => 'INT',
                'constraint' => 11,
                'null'       => true,
            ],
            'verified_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', 'school_year', 'semester']);
        $this->forge->addUniqueKey(['class_section_id', 'school_year', 'semester', 'subject_type', 'subject_id'], 'uniq_whatsapp_membership');

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

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