import os
import pytest

from wurker.settings import conf
from wurker import AddCommand
from wurker.core import Wurkman
from wurker.models import WurkerCommand, WurkerCron, WurkerBee


@pytest.fixture
def etc_root():
    return os.path.join(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))), "etc")


def setup_module(module):
    """Module-level setup that cleans up any pre-existing dummy entries."""

    wurkman = Wurkman()

    with wurkman.session() as db:
        db.query(WurkerCommand).filter(
            WurkerCommand.slug == "dummy_command",
            WurkerCommand.app_id == conf.app_id
        ).delete(synchronize_session=False)

        db.query(WurkerCron).filter(
            WurkerCron.slug == "dummy_cron",
            WurkerCron.app_id == conf.app_id
        ).delete(synchronize_session=False)

        db.query(WurkerBee).filter(
            WurkerBee.slug == "dummy_bee",
            WurkerBee.app_id == conf.app_id
        ).delete(synchronize_session=False)

        db.commit()


def teardown_module(module):
    """Clean up any dummy entries after the tests complete."""

    wurkman = Wurkman()

    with wurkman.session() as db:
        db.query(WurkerCommand).filter(
            WurkerCommand.slug == "dummy_command",
            WurkerCommand.app_id == conf.app_id
        ).delete(synchronize_session=False)

        db.query(WurkerCron).filter(
            WurkerCron.slug == "dummy_cron",
            WurkerCron.app_id == conf.app_id
        ).delete(synchronize_session=False)

        db.query(WurkerBee).filter(
            WurkerBee.slug == "dummy_bee",
            WurkerBee.app_id == conf.app_id
        ).delete(synchronize_session=False)

        db.commit()


def test_01_command(etc_root):
    data = {
        "name": "Dummy Command",
        "slug": "dummy_command",
        "description": "A dummy command",
        "command": os.path.join(etc_root, "scripts", "test_bee.sh"),
        "is_module": False,
    }

    cmd = AddCommand()
    cmd.run("command", data)

    wurkman = Wurkman()
    with wurkman.session() as db:
        command = db.query(WurkerCommand).filter(
            WurkerCommand.slug == "dummy_command",
            WurkerCommand.app_id == conf.app_id
        ).first()

        assert command is not None
        assert str(command.app_id) == conf.app_id
        assert command.name == data["name"]
        assert command.slug == data["slug"]
        assert command.description == data["description"]
        assert command.command == data["command"]
        assert command.is_module == data["is_module"]


def test_02_cron():
    data = {
        "name": "Dummy Cron",
        "slug": "dummy_cron",
        "cron": "*/5 * * * *",
        "overrun": 2,
    }

    cmd = AddCommand()
    cmd.run("cron", data)

    wurkman = Wurkman()
    with wurkman.session() as db:
        cron = db.query(WurkerCron).filter(
            WurkerCron.slug == "dummy_cron",
            WurkerCron.app_id == conf.app_id
        ).first()

        assert cron is not None
        assert str(cron.app_id) == conf.app_id
        assert cron.name == data["name"]
        assert cron.slug == data["slug"]
        assert cron.cron == data["cron"]
        assert cron.overrun == data["overrun"]


def test_03_bee():
    data = {
        "name": "Dummy Bee",
        "slug": "dummy_bee",
        "description": "A dummy bee",
        "command_slug": "dummy_command",
        "cron_slug": "dummy_cron",
        "args": "-l /tmp/dummy_bee.log -i 10 -d 40",
        "quiet_restart": False,
        "enabled": True,
    }

    cmd = AddCommand()
    cmd.run("bee", data)

    wurkman = Wurkman()
    with wurkman.session() as db:
        bee = db.query(WurkerBee).filter(
            WurkerBee.slug == "dummy_bee",
            WurkerBee.app_id == conf.app_id
        ).first()

        assert bee is not None
        assert str(bee.app_id) == conf.app_id
        assert bee.name == data["name"]
        assert bee.slug == data["slug"]
        assert bee.description == data["description"]
        assert bee.command.slug == data["command_slug"]
        assert bee.crons[0].slug == data["cron_slug"]
        assert bee.args == data["args"]
        assert bee.quiet_restart == False
        assert bee.enabled == True
