-- +goose Up
-- +goose StatementBegin
DECLARE
    v_count NUMBER;
BEGIN
    SELECT COUNT(*) INTO v_count FROM user_tables WHERE table_name = 'TB_FLASH_NODO_RESUMO_CONTRIBUICAO';
    IF v_count = 0 THEN
        EXECUTE IMMEDIATE '
            CREATE TABLE TB_FLASH_NODO_RESUMO_CONTRIBUICAO (
                ID_RESUMO               NUMBER      NOT NULL,
                ID_NODO                 NUMBER      NOT NULL,
                DATA_REFERENCIA         DATE        NOT NULL,
                ANO_REFERENCIA          NUMBER(4,0) NOT NULL,
                MES_REFERENCIA          NUMBER(2,0) NOT NULL,
                DIA_REFERENCIA          NUMBER(2,0) NOT NULL,
                NRO_TICKETS             NUMBER(18,6),
                VLR_IMP_CONTRIBUICAO    NUMBER(18,6),
                QTDE_CONTRIBUICAO       NUMBER(18,6),
                VLR_CONTRIBUICAO        NUMBER(18,6),
                VL_TICKET_MEDIO         NUMBER(18,6),
                VALOR_META_PROPRIA      NUMBER(18,6),
                VALOR_META_FILHOS       NUMBER(18,6),
                VALOR_META_EFETIVA      NUMBER(18,6),
                PERCENTUAL_META_PROPRIA NUMBER(10,4),
                PERCENTUAL_META_FILHOS  NUMBER(10,4),
                PERCENTUAL_META_EFETIVA NUMBER(10,4),
                DESVIO                  NUMBER(18,6),
                PERCENTUAL_ATINGIMENTO  NUMBER(18,6),
                DATA_APURACAO           DATE DEFAULT SYSDATE NOT NULL,
                NRO_SEGMENTO            NUMBER(24,0),
                NOME_SEGMENTO           VARCHAR2(300),
                CONSTRAINT CK_TB_FLASH_NODO_RES_CONTRIB_MES CHECK (MES_REFERENCIA BETWEEN 1 AND 12),
                CONSTRAINT CK_TB_FLASH_NODO_RES_CONTRIB_DIA CHECK (DIA_REFERENCIA BETWEEN 1 AND 31),
                CONSTRAINT PK_TB_FLASH_NODO_RES_CONTRIB PRIMARY KEY (ID_RESUMO),
                CONSTRAINT UK_TB_FLASH_NODO_RES_CONTRIB UNIQUE (ID_NODO, DATA_REFERENCIA, NRO_SEGMENTO)
            )';
    END IF;
END;
-- +goose StatementEnd
-- +goose StatementBegin
DECLARE
    v_count NUMBER;
BEGIN
    SELECT COUNT(*) INTO v_count FROM user_constraints WHERE constraint_name = 'FK_TB_FLASH_NODO_RES_CONTRIB_NODO';
    IF v_count = 0 THEN
        EXECUTE IMMEDIATE 'ALTER TABLE TB_FLASH_NODO_RESUMO_CONTRIBUICAO ADD CONSTRAINT FK_TB_FLASH_NODO_RES_CONTRIB_NODO FOREIGN KEY (ID_NODO) REFERENCES TB_FLASH_NODO (ID_NODO)';
    END IF;
END;
-- +goose StatementEnd
-- +goose StatementBegin
DECLARE
    v_count NUMBER;
BEGIN
    SELECT COUNT(*) INTO v_count FROM user_indexes WHERE index_name = 'IX_TB_FLASH_NODO_RES_CONTRIB_NODO';
    IF v_count = 0 THEN
        EXECUTE IMMEDIATE 'CREATE INDEX IX_TB_FLASH_NODO_RES_CONTRIB_NODO ON TB_FLASH_NODO_RESUMO_CONTRIBUICAO (ID_NODO)';
    END IF;
END;
-- +goose StatementEnd
-- +goose StatementBegin
DECLARE
    v_count NUMBER;
BEGIN
    SELECT COUNT(*) INTO v_count FROM user_indexes WHERE index_name = 'IX_TB_FLASH_NODO_RES_CONTRIB_DATA';
    IF v_count = 0 THEN
        EXECUTE IMMEDIATE 'CREATE INDEX IX_TB_FLASH_NODO_RES_CONTRIB_DATA ON TB_FLASH_NODO_RESUMO_CONTRIBUICAO (DATA_REFERENCIA)';
    END IF;
END;
-- +goose StatementEnd
-- +goose StatementBegin
DECLARE
    v_count NUMBER;
BEGIN
    SELECT COUNT(*) INTO v_count FROM user_indexes WHERE index_name = 'IX_TB_FLASH_NODO_RES_CONTRIB_ANOMESDIA';
    IF v_count = 0 THEN
        EXECUTE IMMEDIATE 'CREATE INDEX IX_TB_FLASH_NODO_RES_CONTRIB_ANOMESDIA ON TB_FLASH_NODO_RESUMO_CONTRIBUICAO (ANO_REFERENCIA, MES_REFERENCIA, DIA_REFERENCIA)';
    END IF;
END;
-- +goose StatementEnd

-- +goose Down
-- +goose StatementBegin
DECLARE
    v_count NUMBER;
BEGIN
    SELECT COUNT(*) INTO v_count FROM user_tables WHERE table_name = 'TB_FLASH_NODO_RESUMO_CONTRIBUICAO';
    IF v_count > 0 THEN
        EXECUTE IMMEDIATE 'DROP TABLE TB_FLASH_NODO_RESUMO_CONTRIBUICAO CASCADE CONSTRAINTS PURGE';
    END IF;
END;
-- +goose StatementEnd
