<?php

namespace App\Database\Migrations;

use CodeIgniter\Database\Migration;

class CreateNotifications extends Migration
{
    public function up(): void
    {
        $this->forge->addField([
            'id'          => ['type' => 'BIGINT', 'unsigned' => true, 'auto_increment' => true],
            'trigger_key' => ['type' => 'VARCHAR', 'constraint' => 100],
            'channel'     => ['type' => 'ENUM', 'constraint' => ['email', 'sms', 'in_app', 'whatsapp']],
            'subject'     => ['type' => 'VARCHAR', 'constraint' => 200, 'null' => true],
            'body'        => ['type' => 'TEXT'],
            'is_active'   => ['type' => 'TINYINT', 'constraint' => 1, 'default' => 1],
        ]);
        $this->forge->addKey('id', true);
        $this->forge->addUniqueKey(['trigger_key', 'channel']);
        $this->forge->createTable('notification_templates', true, ['ENGINE' => 'InnoDB']);

        $this->forge->addField([
            'id'         => ['type' => 'BIGINT', 'unsigned' => true, 'auto_increment' => true],
            'user_id'    => ['type' => 'BIGINT', 'unsigned' => true],
            'type'       => ['type' => 'VARCHAR', 'constraint' => 100],
            'payload'    => ['type' => 'JSON', 'null' => true],
            'channel'    => ['type' => 'ENUM', 'constraint' => ['email', 'sms', 'in_app', 'whatsapp']],
            'is_read'    => ['type' => 'TINYINT', 'constraint' => 1, 'default' => 0],
            'sent_at'    => ['type' => 'TIMESTAMP', 'null' => true],
            'created_at' => ['type' => 'TIMESTAMP', 'null' => true],
        ]);
        $this->forge->addKey('id', true);
        $this->forge->addKey(['user_id', 'is_read']);
        $this->forge->addForeignKey('user_id', 'users', 'id', 'CASCADE', 'CASCADE');
        $this->forge->createTable('notifications', true, ['ENGINE' => 'InnoDB']);

        // Per-user channel preferences — referenced by NotificationService
        // when resolving which channels a trigger should actually fire on.
        $this->forge->addField([
            'id'                => ['type' => 'BIGINT', 'unsigned' => true, 'auto_increment' => true],
            'user_id'           => ['type' => 'BIGINT', 'unsigned' => true],
            'email_enabled'     => ['type' => 'TINYINT', 'constraint' => 1, 'default' => 1],
            'sms_enabled'       => ['type' => 'TINYINT', 'constraint' => 1, 'default' => 0],
            'in_app_enabled'    => ['type' => 'TINYINT', 'constraint' => 1, 'default' => 1],
            'whatsapp_enabled'  => ['type' => 'TINYINT', 'constraint' => 1, 'default' => 0],
        ]);
        $this->forge->addKey('id', true);
        $this->forge->addUniqueKey('user_id');
        $this->forge->addForeignKey('user_id', 'users', 'id', 'CASCADE', 'CASCADE');
        $this->forge->createTable('notification_preferences', true, ['ENGINE' => 'InnoDB']);
    }

    public function down(): void
    {
        $this->forge->dropTable('notification_preferences', true);
        $this->forge->dropTable('notifications', true);
        $this->forge->dropTable('notification_templates', true);
    }
}
