<?php

namespace App\Database\Migrations;

use CodeIgniter\Database\Migration;

class AddNotificationKindUnique extends Migration
{
    public function up(): void
    {
        $this->forge->addColumn('notifications', [
            'kind' => [
                'type'       => 'VARCHAR',
                'constraint' => 32,
                'null'       => false,
                'default'    => 'due',
                'after'      => 'occurrence_id',
            ],
        ]);

        $this->db->query(
            'ALTER TABLE notifications ADD UNIQUE INDEX notifications_user_occurrence_kind_unique (user_id, occurrence_id, kind)',
        );
    }

    public function down(): void
    {
        $this->db->query('ALTER TABLE notifications DROP INDEX notifications_user_occurrence_kind_unique');
        $this->forge->dropColumn('notifications', 'kind');
    }
}
