<?php

namespace App\Database\Migrations;

use CodeIgniter\Database\Migration;

class CreateFamiliesTables extends Migration
{
    public function up()
    {
        // families
        $this->forge->addField([
            'id'                        => ['type' => 'INT', 'unsigned' => true, 'auto_increment' => true],
            'family_code'               => ['type' => 'VARCHAR', 'constraint' => 64, 'null' => false],
            'household_name'            => ['type' => 'VARCHAR', 'constraint' => 128, 'null' => true],
            'address_line1'             => ['type' => 'VARCHAR', 'constraint' => 128, 'null' => true],
            'address_line2'             => ['type' => 'VARCHAR', 'constraint' => 128, 'null' => true],
            'city'                      => ['type' => 'VARCHAR', 'constraint' => 64, 'null' => true],
            'state'                     => ['type' => 'VARCHAR', 'constraint' => 32, 'null' => true],
            'postal_code'               => ['type' => 'VARCHAR', 'constraint' => 16, 'null' => true],
            'country'                   => ['type' => 'VARCHAR', 'constraint' => 64, 'null' => true],
            'primary_phone'             => ['type' => 'VARCHAR', 'constraint' => 32, 'null' => true],
            'preferred_lang'            => ['type' => 'VARCHAR', 'constraint' => 16, 'null' => true],
            'preferred_contact_method'  => ['type' => 'VARCHAR', 'constraint' => 32, 'null' => true],
            'is_active'                 => ['type' => 'TINYINT', 'constraint' => 1, 'default' => 1, 'null' => false],
            'created_at'                => ['type' => 'DATETIME', 'null' => true],
            'updated_at'                => ['type' => 'DATETIME', 'null' => true],
        ]);
        $this->forge->addKey('id', true);
        $this->forge->addUniqueKey('family_code');
        $this->forge->createTable('families', true);

        // family_students
        $this->forge->addField([
            'id'              => ['type' => 'INT', 'unsigned' => true, 'auto_increment' => true],
            'family_id'       => ['type' => 'INT', 'unsigned' => true, 'null' => false],
            'student_id'      => ['type' => 'INT', 'unsigned' => true, 'null' => false],
            'is_primary_home' => ['type' => 'TINYINT', 'constraint' => 1, 'default' => 0, 'null' => false],
            'notes'           => ['type' => 'TEXT', 'null' => true],
            'created_at'      => ['type' => 'DATETIME', 'null' => true],
            'updated_at'      => ['type' => 'DATETIME', 'null' => true],
        ]);
        $this->forge->addKey('id', true);
        $this->forge->addUniqueKey(['family_id', 'student_id']);
        $this->forge->addForeignKey('family_id', 'families', 'id', 'CASCADE', 'CASCADE');
        $this->forge->addForeignKey('student_id', 'students', 'id', 'CASCADE', 'CASCADE');
        $this->forge->createTable('family_students', true);

        // family_guardians
        $this->forge->addField([
            'id'             => ['type' => 'INT', 'unsigned' => true, 'auto_increment' => true],
            'family_id'      => ['type' => 'INT', 'unsigned' => true, 'null' => false],
            'user_id'        => ['type' => 'INT', 'unsigned' => true, 'null' => false],
            'relation'       => ['type' => 'VARCHAR', 'constraint' => 32, 'null' => false, 'default' => 'guardian'],
            'is_primary'     => ['type' => 'TINYINT', 'constraint' => 1, 'default' => 0, 'null' => false],
            'receive_emails' => ['type' => 'TINYINT', 'constraint' => 1, 'default' => 1, 'null' => false],
            'receive_sms'    => ['type' => 'TINYINT', 'constraint' => 1, 'default' => 0, 'null' => false],
            'custody_notes'  => ['type' => 'TEXT', 'null' => true],
            'created_at'     => ['type' => 'DATETIME', 'null' => true],
            'updated_at'     => ['type' => 'DATETIME', 'null' => true],
        ]);
        $this->forge->addKey('id', true);
        $this->forge->addUniqueKey(['family_id', 'user_id']);
        $this->forge->addForeignKey('family_id', 'families', 'id', 'CASCADE', 'CASCADE');
        $this->forge->addForeignKey('user_id', 'users', 'id', 'CASCADE', 'CASCADE');
        $this->forge->createTable('family_guardians', true);

        // family_comm_prefs
        $this->forge->addField([
            'id'                => ['type' => 'INT', 'unsigned' => true, 'auto_increment' => true],
            'family_id'         => ['type' => 'INT', 'unsigned' => true, 'null' => false],
            'category'          => ['type' => 'VARCHAR', 'constraint' => 32, 'null' => false],
            'via_email'         => ['type' => 'TINYINT', 'constraint' => 1, 'default' => 1, 'null' => false],
            'via_sms'           => ['type' => 'TINYINT', 'constraint' => 1, 'default' => 0, 'null' => false],
            'cc_all_guardians'  => ['type' => 'TINYINT', 'constraint' => 1, 'default' => 1, 'null' => false],
            'created_at'        => ['type' => 'DATETIME', 'null' => true],
            'updated_at'        => ['type' => 'DATETIME', 'null' => true],
        ]);
        $this->forge->addKey('id', true);
        $this->forge->addUniqueKey(['family_id', 'category']);
        $this->forge->addForeignKey('family_id', 'families', 'id', 'CASCADE', 'CASCADE');
        $this->forge->createTable('family_comm_prefs', true);
    }

    public function down()
    {
        $this->forge->dropTable('family_comm_prefs', true);
        $this->forge->dropTable('family_guardians', true);
        $this->forge->dropTable('family_students', true);
        $this->forge->dropTable('families', true);
    }
}

