package com.cameleer.server.app.alerting.storage;

import com.cameleer.server.app.AbstractPostgresIT;
import org.junit.jupiter.api.Test;

import static org.assertj.core.api.Assertions.assertThat;

class V17MigrationIT extends AbstractPostgresIT {

    @Test
    void alert_state_enum_drops_acknowledged() {
        var values = jdbcTemplate.queryForList("""
            SELECT unnest(enum_range(NULL::alert_state_enum))::text AS v
            """, String.class);
        assertThat(values).containsExactlyInAnyOrder("PENDING", "FIRING", "RESOLVED");
    }

    @Test
    void read_at_and_deleted_at_columns_exist() {
        var cols = jdbcTemplate.queryForList("""
            SELECT column_name FROM information_schema.columns
             WHERE table_name = 'alert_instances'
               AND column_name IN ('read_at','deleted_at')
            """, String.class);
        assertThat(cols).containsExactlyInAnyOrder("read_at", "deleted_at");
    }

    @Test
    void alert_reads_table_is_gone() {
        Integer count = jdbcTemplate.queryForObject("""
            SELECT COUNT(*)::int FROM information_schema.tables
             WHERE table_name = 'alert_reads'
            """, Integer.class);
        assertThat(count).isZero();
    }

    @Test
    void open_rule_index_predicate_is_reworked() {
        String def = jdbcTemplate.queryForObject("""
            SELECT pg_get_indexdef(indexrelid)
              FROM pg_index
              JOIN pg_class ON pg_class.oid = pg_index.indexrelid
             WHERE pg_class.relname = 'alert_instances_open_rule_uq'
            """, String.class);
        assertThat(def).contains("state = ANY (ARRAY['PENDING'::alert_state_enum, 'FIRING'::alert_state_enum])");
        assertThat(def).contains("deleted_at IS NULL");
    }
}
