-- +goose Up
-- +goose StatementBegin
DECLARE
    v_count NUMBER;
BEGIN
    SELECT COUNT(*) INTO v_count FROM user_tables WHERE table_name = 'TB_FLASH_NODO';
    IF v_count = 0 THEN
        EXECUTE IMMEDIATE '
            CREATE TABLE TB_FLASH_NODO (
                ID_NODO          NUMBER        NOT NULL,
                ID_NODO_PAI      NUMBER,
                TIPO_NODO        VARCHAR2(30)  NOT NULL,
                CODIGO           VARCHAR2(60)  NOT NULL,
                NOME             VARCHAR2(300) NOT NULL,
                NIVEL            NUMBER        NOT NULL,
                ORDEM_EXIBICAO   NUMBER,
                ATIVO            CHAR(1) DEFAULT ''S'' NOT NULL,
                COD_EMPRESA      NUMBER,
                COD_PRODUTO      NUMBER,
                SEQPRODUTOBASE   NUMBER,
                DATA_CADASTRO    DATE DEFAULT SYSDATE NOT NULL,
                DATA_ATUALIZACAO DATE,
                CONSTRAINT CK_TB_FLASH_NODO_TIPO  CHECK (TIPO_NODO IN (''EMPRESA'', ''CATEGORIA'', ''PRODUTO'')),
                CONSTRAINT CK_TB_FLASH_NODO_ATIVO CHECK (ATIVO IN (''S'', ''N'')),
                CONSTRAINT CK_TB_FLASH_NODO_NIVEL CHECK (NIVEL >= 1),
                CONSTRAINT PK_TB_FLASH_NODO PRIMARY KEY (ID_NODO),
                CONSTRAINT UK_TB_FLASH_NODO_PAI_TIPO_COD UNIQUE (ID_NODO_PAI, TIPO_NODO, CODIGO)
            )';
    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_PAI';
    IF v_count = 0 THEN
        EXECUTE IMMEDIATE 'ALTER TABLE TB_FLASH_NODO ADD CONSTRAINT FK_TB_FLASH_NODO_PAI FOREIGN KEY (ID_NODO_PAI) 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_PAI';
    IF v_count = 0 THEN
        EXECUTE IMMEDIATE 'CREATE INDEX IX_TB_FLASH_NODO_PAI ON TB_FLASH_NODO (ID_NODO_PAI)';
    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_TIPO';
    IF v_count = 0 THEN
        EXECUTE IMMEDIATE 'CREATE INDEX IX_TB_FLASH_NODO_TIPO ON TB_FLASH_NODO (TIPO_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_EMPRESA';
    IF v_count = 0 THEN
        EXECUTE IMMEDIATE 'CREATE INDEX IX_TB_FLASH_NODO_EMPRESA ON TB_FLASH_NODO (COD_EMPRESA)';
    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_PRODUTO';
    IF v_count = 0 THEN
        EXECUTE IMMEDIATE 'CREATE INDEX IX_TB_FLASH_NODO_PRODUTO ON TB_FLASH_NODO (COD_PRODUTO)';
    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_EMP_PROD';
    IF v_count = 0 THEN
        EXECUTE IMMEDIATE 'CREATE INDEX IX_TB_FLASH_NODO_EMP_PROD ON TB_FLASH_NODO (TIPO_NODO, COD_EMPRESA, COD_PRODUTO)';
    END IF;
END;
-- +goose StatementEnd
-- +goose StatementBegin
DECLARE
    v_count NUMBER;
BEGIN
    SELECT COUNT(*) INTO v_count FROM user_indexes WHERE index_name = 'TB_FLASH_NODO_ID_NODO_PAI_IDX';
    IF v_count = 0 THEN
        EXECUTE IMMEDIATE 'CREATE INDEX TB_FLASH_NODO_ID_NODO_PAI_IDX ON TB_FLASH_NODO (ID_NODO_PAI, TIPO_NODO, CODIGO, COD_EMPRESA)';
    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';
    IF v_count > 0 THEN
        EXECUTE IMMEDIATE 'DROP TABLE TB_FLASH_NODO CASCADE CONSTRAINTS PURGE';
    END IF;
END;
-- +goose StatementEnd
