-- CreateTable
CREATE TABLE "sessoes" (
    "id" SERIAL NOT NULL,
    "uuid" TEXT NOT NULL,
    "ativa" BOOLEAN NOT NULL DEFAULT true,
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

    CONSTRAINT "sessoes_pkey" PRIMARY KEY ("id")
);

-- CreateTable
CREATE TABLE "tokens_acessos" (
    "id" SERIAL NOT NULL,
    "token" TEXT NOT NULL,
    "descricao" TEXT NOT NULL,
    "ativo" BOOLEAN NOT NULL DEFAULT true,
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

    CONSTRAINT "tokens_acessos_pkey" PRIMARY KEY ("id")
);

-- CreateTable
CREATE TABLE "bots" (
    "id" SERIAL NOT NULL,
    "chave" TEXT NOT NULL,
    "nome" TEXT NOT NULL,
    "menu_id" INTEGER,
    "mensagens_iniciais" TEXT[],
    "mensagens_pede_cpf_cnpj" TEXT[],
    "mensagem_cpf_invalido" TEXT NOT NULL,
    "mensagens_pede_nome" TEXT[],
    "mensagem_nome_invalido" TEXT NOT NULL,
    "mensagem_continuar_atendimento" TEXT NOT NULL,
    "mensagens_encerramento" TEXT[],
    "ativo" BOOLEAN NOT NULL DEFAULT true,
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

    CONSTRAINT "bots_pkey" PRIMARY KEY ("id")
);

-- CreateTable
CREATE TABLE "menus" (
    "id" SERIAL NOT NULL,
    "item_id" INTEGER,
    "cabecalho" TEXT NOT NULL,
    "mensagens" TEXT[],
    "ativo" BOOLEAN NOT NULL DEFAULT true,
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

    CONSTRAINT "menus_pkey" PRIMARY KEY ("id")
);

-- CreateTable
CREATE TABLE "itens_menus" (
    "id" SERIAL NOT NULL,
    "menu_id" INTEGER NOT NULL,
    "ordem" INTEGER NOT NULL,
    "titulo" TEXT NOT NULL,
    "mensagens" TEXT[],
    "acao" TEXT NOT NULL,
    "ativo" BOOLEAN NOT NULL DEFAULT true,
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

    CONSTRAINT "itens_menus_pkey" PRIMARY KEY ("id")
);

-- CreateTable
CREATE TABLE "mensagens" (
    "id" SERIAL NOT NULL,
    "sessao_id" INTEGER NOT NULL,
    "mensagem" TEXT NOT NULL,
    "origem" TEXT NOT NULL,
    "acao" TEXT NOT NULL,
    "opcoes" JSONB,
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

    CONSTRAINT "mensagens_pkey" PRIMARY KEY ("id")
);

-- CreateTable
CREATE TABLE "atendimentos" (
    "id" SERIAL NOT NULL,
    "sessao_id" INTEGER NOT NULL,
    "uuid" TEXT NOT NULL,
    "usuario_nome" TEXT,
    "usuario_cpf_cnpj" TEXT,
    "usuario_telefone" TEXT,
    "usuario_whatsapp" TEXT,
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

    CONSTRAINT "atendimentos_pkey" PRIMARY KEY ("id")
);

-- CreateTable
CREATE TABLE "pesquisa_satisfacao_perguntas" (
    "id" SERIAL NOT NULL,
    "bot_id" INTEGER NOT NULL,
    "chave" TEXT NOT NULL,
    "pergunta" TEXT NOT NULL,
    "ativo" BOOLEAN NOT NULL DEFAULT true,
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

    CONSTRAINT "pesquisa_satisfacao_perguntas_pkey" PRIMARY KEY ("id")
);

-- CreateTable
CREATE TABLE "pesquisa_satisfacao_opcoes_respostas" (
    "id" SERIAL NOT NULL,
    "pergunta_id" INTEGER NOT NULL,
    "chave" TEXT NOT NULL,
    "label" TEXT NOT NULL,
    "value" INTEGER NOT NULL,
    "ativo" BOOLEAN NOT NULL DEFAULT true,
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

    CONSTRAINT "pesquisa_satisfacao_opcoes_respostas_pkey" PRIMARY KEY ("id")
);

-- CreateTable
CREATE TABLE "pesquisa_satisfacao_respostas" (
    "sessao_id" INTEGER NOT NULL,
    "pergunta_id" INTEGER NOT NULL,
    "opcao_resposta_id" INTEGER NOT NULL,
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

    CONSTRAINT "pesquisa_satisfacao_respostas_pkey" PRIMARY KEY ("sessao_id","pergunta_id")
);

-- CreateTable
CREATE TABLE "bot_setup" (
    "id" SERIAL NOT NULL,
    "bot_id" INTEGER NOT NULL,
    "quantidade_tentativas_validar_cpf" INTEGER NOT NULL,
    "tempo_encerrar_atendimento" INTEGER NOT NULL,
    "tempo_alerta_inatividade" INTEGER NOT NULL,
    "mensagem_encerramento_automatico" TEXT NOT NULL,
    "mensagem_alerta_inatividade" TEXT NOT NULL,
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

    CONSTRAINT "bot_setup_pkey" PRIMARY KEY ("id")
);

-- CreateIndex
CREATE UNIQUE INDEX "sessoes_uuid_key" ON "sessoes"("uuid");

-- CreateIndex
CREATE UNIQUE INDEX "tokens_acessos_token_key" ON "tokens_acessos"("token");

-- CreateIndex
CREATE UNIQUE INDEX "bots_chave_key" ON "bots"("chave");

-- CreateIndex
CREATE UNIQUE INDEX "bots_nome_key" ON "bots"("nome");

-- CreateIndex
CREATE UNIQUE INDEX "atendimentos_sessao_id_key" ON "atendimentos"("sessao_id");

-- CreateIndex
CREATE UNIQUE INDEX "atendimentos_uuid_key" ON "atendimentos"("uuid");

-- CreateIndex
CREATE UNIQUE INDEX "pesquisa_satisfacao_perguntas_chave_key" ON "pesquisa_satisfacao_perguntas"("chave");

-- CreateIndex
CREATE UNIQUE INDEX "pesquisa_satisfacao_opcoes_respostas_chave_key" ON "pesquisa_satisfacao_opcoes_respostas"("chave");

-- CreateIndex
CREATE UNIQUE INDEX "bot_setup_bot_id_key" ON "bot_setup"("bot_id");

-- AddForeignKey
ALTER TABLE "bots" ADD CONSTRAINT "bots_menu_id_fkey" FOREIGN KEY ("menu_id") REFERENCES "menus"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "menus" ADD CONSTRAINT "menus_item_id_fkey" FOREIGN KEY ("item_id") REFERENCES "itens_menus"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "itens_menus" ADD CONSTRAINT "itens_menus_menu_id_fkey" FOREIGN KEY ("menu_id") REFERENCES "menus"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "mensagens" ADD CONSTRAINT "mensagens_sessao_id_fkey" FOREIGN KEY ("sessao_id") REFERENCES "sessoes"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "atendimentos" ADD CONSTRAINT "atendimentos_sessao_id_fkey" FOREIGN KEY ("sessao_id") REFERENCES "sessoes"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "pesquisa_satisfacao_perguntas" ADD CONSTRAINT "pesquisa_satisfacao_perguntas_bot_id_fkey" FOREIGN KEY ("bot_id") REFERENCES "bots"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "pesquisa_satisfacao_opcoes_respostas" ADD CONSTRAINT "pesquisa_satisfacao_opcoes_respostas_pergunta_id_fkey" FOREIGN KEY ("pergunta_id") REFERENCES "pesquisa_satisfacao_perguntas"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "pesquisa_satisfacao_respostas" ADD CONSTRAINT "pesquisa_satisfacao_respostas_pergunta_id_fkey" FOREIGN KEY ("pergunta_id") REFERENCES "pesquisa_satisfacao_perguntas"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "pesquisa_satisfacao_respostas" ADD CONSTRAINT "pesquisa_satisfacao_respostas_opcao_resposta_id_fkey" FOREIGN KEY ("opcao_resposta_id") REFERENCES "pesquisa_satisfacao_opcoes_respostas"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "pesquisa_satisfacao_respostas" ADD CONSTRAINT "pesquisa_satisfacao_respostas_sessao_id_fkey" FOREIGN KEY ("sessao_id") REFERENCES "sessoes"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "bot_setup" ADD CONSTRAINT "bot_setup_bot_id_fkey" FOREIGN KEY ("bot_id") REFERENCES "bots"("id") ON DELETE RESTRICT ON UPDATE CASCADE;
