import unittest

from lib.mu_whatsapp_api import (
    VOLUMES_MU_WHATSAPP_API,
    apagar_mu_whatsapp_api,
    preparar_ambiente_mu_whatsapp_api,
)


class ResultadoFake:
    def __init__(self, codigo_retorno=0, saida=""):
        self.codigo_retorno = codigo_retorno
        self.saida = saida


class ClienteLinuxFake:
    def __init__(self, respostas=None):
        self.respostas = list(respostas or [])
        self.comandos = []
        self.copias = []

    def executar_comando(self, comando, disparar_excecao_se_retorno_nao_zero=True, echoar=True, descricao=""):
        self.comandos.append(comando)

        if self.respostas:
            return self.respostas.pop(0)

        return ResultadoFake()

    def copiar_arquivo(self, origem, destino):
        self.copias.append((origem, destino))


class TestMuWhatsappApi(unittest.TestCase):
    def test_preparar_ambiente_mu_whatsapp_api_copia_compose_e_ajusta_placeholders(self):
        cliente_linux = ClienteLinuxFake()
        configuracoes = {
            "acessos": {
                "servidorLinux": {
                    "usuario": "deploy",
                },
                "registroDocker": {
                    "usuario": "oauth2",
                    "senha": "senha-token",
                },
            },
            "muWhatsapp": {
                "versao": "1.2.3",
                "porta": 21465,
            },
        }

        preparar_ambiente_mu_whatsapp_api(
            cliente_linux,
            configuracoes,
            r"c:\repos\scripts_implantacao"
        )

        self.assertEqual(
            cliente_linux.copias,
            [
                (
                    r"c:\repos\scripts_implantacao\muWhatsapp\docker-compose-mu-whatsapp.yml",
                    "/home/deploy/muWhatsapp/docker-compose.yml"
                )
            ]
        )
        self.assertEqual(
            cliente_linux.comandos,
            [
                "mkdir -p /home/deploy/muWhatsapp",
                "docker login gitlab.microuniverso.com.br:5050 -u oauth2 -p senha-token",
                "sed -i 's,\\$VERSAO_MU_WHATSAPP,1.2.3,g' /home/deploy/muWhatsapp/docker-compose.yml",
                "sed -i 's,\\$PORTA,21465,g' /home/deploy/muWhatsapp/docker-compose.yml",
                "sed -i 's,\\$NOME_CONTAINER_MU_WHATSAPP,muwhatsapp,g' /home/deploy/muWhatsapp/docker-compose.yml",
            ]
        )

    def test_apagar_mu_whatsapp_api_remove_container_volumes_e_diretorio(self):
        cliente_linux = ClienteLinuxFake(
            respostas=[
                ResultadoFake(codigo_retorno=0),
                ResultadoFake(codigo_retorno=0),
            ]
        )
        configuracoes = {
            "acessos": {
                "servidorLinux": {
                    "usuario": "deploy",
                },
            },
            "muWhatsapp": {},
        }

        apagar_mu_whatsapp_api(cliente_linux, configuracoes, remover_volumes=True)

        self.assertEqual(
            cliente_linux.comandos,
            [
                "docker inspect muwhatsapp >/dev/null 2>&1",
                "docker network disconnect shared_net muwhatsapp",
                "docker compose -f /home/deploy/muWhatsapp/docker-compose.yml down --remove-orphans -v",
                "docker rm -f muwhatsapp",
                f"docker volume rm -f {VOLUMES_MU_WHATSAPP_API[0]}",
                f"docker volume rm -f {VOLUMES_MU_WHATSAPP_API[1]}",
                "rm -rf /home/deploy/muWhatsapp",
            ]
        )


if __name__ == "__main__":
    unittest.main()
