<?xml version="1.1" encoding="UTF-8" standalone="no"?>
<databaseChangeLog xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
                     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                     xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog https://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-4.26.xsd" >
    <changeSet author="Sheldan" id="poll-table">
        <createTable tableName="poll">
            <column name="id" type="BIGINT" autoIncrement="true">
                <constraints nullable="false" primaryKey="true"/>
            </column>
            <column name="poll_id" type="BIGINT">
                <constraints nullable="false"/>
            </column>
            <column name="message_id" type="BIGINT">
                <constraints nullable="false"/>
            </column>
            <column name="type" type="VARCHAR(255)">
                <constraints nullable="false"/>
            </column>
            <column name="created" type="TIMESTAMP WITHOUT TIME ZONE">
                <constraints nullable="false"/>
            </column>
            <column name="updated" type="TIMESTAMP WITHOUT TIME ZONE"/>
            <column name="channel_id" type="BIGINT">
                <constraints nullable="false"/>
            </column>
            <column name="server_id" type="BIGINT">
                <constraints nullable="false"/>
            </column>
            <column name="state" type="VARCHAR(255)">
                <constraints nullable="false"/>
            </column>
            <column name="selection_menu_id" type="VARCHAR(100)">
                <constraints nullable="false"/>
            </column>
            <column name="add_option_button_id" type="VARCHAR(100)">
                <constraints nullable="true"/>
            </column>
            <column name="creator_user_in_server_id" type="BIGINT">
                <constraints nullable="false"/>
            </column>
            <column name="description" type="VARCHAR(2000)">
                <constraints nullable="false"/>
            </column>
            <column name="evaluation_job_trigger_key" type="varchar(255)"/>
            <column name="reminder_job_trigger_key" type="varchar(255)"/>
            <column name="target_date" type="TIMESTAMP WITHOUT TIME ZONE">
                <constraints nullable="false"/>
            </column>
            <column name="allow_multiple" type="BOOLEAN">
                <constraints nullable="false"/>
            </column>
            <column name="show_decisions" type="BOOLEAN">
                <constraints nullable="false"/>
            </column>
            <column name="allow_addition" type="BOOLEAN">
                <constraints nullable="false"/>
            </column>
        </createTable>
        <addUniqueConstraint
                columnNames="poll_id, server_id, type"
                constraintName="uq_poll_id"
                tableName="poll"
        />
        <addForeignKeyConstraint baseColumnNames="channel_id" baseTableName="poll" constraintName="fk_poll_channel"
                                 deferrable="false" initiallyDeferred="false" onDelete="NO ACTION" onUpdate="NO ACTION"
                                 referencedColumnNames="id" referencedTableName="channel" validate="true"/>
        <addForeignKeyConstraint baseColumnNames="creator_user_in_server_id" baseTableName="poll" constraintName="fk_poll_creator"
                                 deferrable="false" initiallyDeferred="false" onDelete="NO ACTION" onUpdate="NO ACTION"
                                 referencedColumnNames="user_in_server_id" referencedTableName="user_in_server" validate="true"/>
        <addForeignKeyConstraint baseColumnNames="server_id" baseTableName="poll" constraintName="fk_poll_server"
                                 deferrable="false" initiallyDeferred="false" onDelete="NO ACTION" onUpdate="NO ACTION"
                                 referencedColumnNames="id" referencedTableName="server" validate="true"/>
        <sql>
            DROP TRIGGER IF EXISTS poll_update_trigger ON poll;
            CREATE TRIGGER poll_update_trigger BEFORE UPDATE ON poll FOR EACH ROW EXECUTE PROCEDURE update_trigger_procedure();
        </sql>
        <sql>
            DROP TRIGGER IF EXISTS poll_insert_trigger ON poll;
            CREATE TRIGGER poll_insert_trigger BEFORE INSERT ON poll FOR EACH ROW EXECUTE PROCEDURE insert_trigger_procedure();
        </sql>
        <sql>
            ALTER TABLE poll ADD CONSTRAINT check_poll_state CHECK (state IN ('NEW', 'FINISHED','CANCELLED', 'VETOED'));
        </sql>
    </changeSet>
</databaseChangeLog>