<?php
// 2025-09-10-031030_CreateFamilyCommPrefs.php
namespace App\Database\Migrations;

use CodeIgniter\Database\Migration;

class CreateFamilyCommPrefs extends Migration
{
    protected $DBGroup = 'default';

    public function up()
    {
        if (! $this->db->tableExists('families')) {
            throw new \RuntimeException('CreateFamilyCommPrefs requires families table to exist.');
        }

        $this->forge->addField([
            'id'               => ['type'=>'INT','unsigned'=>true,'auto_increment'=>true],
            'family_id'        => ['type'=>'INT','unsigned'=>true,'null'=>false],
            'category'         => ['type'=>'ENUM','constraint'=>['attendance','grade','behavior','general'],'null'=>false],
            'via_email'        => ['type'=>'TINYINT','constraint'=>1,'default'=>1],
            'via_sms'          => ['type'=>'TINYINT','constraint'=>1,'default'=>0],
            'cc_all_guardians' => ['type'=>'TINYINT','constraint'=>1,'default'=>1],
        ]);
        $this->forge->addKey('id', true);
        $this->forge->addUniqueKey(['family_id','category'], 'uq_family_category');
        $this->forge->addForeignKey('family_id','families','id','CASCADE','CASCADE','fk_fcp_family');

        $this->forge->createTable('family_comm_prefs', true, [
            'ENGINE'=>'InnoDB','DEFAULT CHARSET'=>'utf8mb4','COLLATE'=>'utf8mb4_unicode_ci',
        ]);
    }

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