import os
import pytest

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


def setup_module(module):
    """Create records for 'dummy_command', 'dummy_cron', and 'dummy_bee'"""

    # Compute etc_root similarly to your etc_root fixture.
    etc_root = conf.etc_root

    # Create a module-level Wurkman instance so tests can reference it
    global wurkman
    wurkman = Wurkman()

    # Open a session and insert dummy records directly
    with wurkman.session() as db:

        # First make sure no artifacts remain
        dummy_bee_id = db.query(WurkerBee.id).filter(
            WurkerBee.slug == "dummy_bee",
            WurkerBee.app_id == conf.app_id
        ).scalar()

        if dummy_bee_id:
            db.execute(
                wurker_bee_cron.delete().where(wurker_bee_cron.c.wurker_bee_id == dummy_bee_id)
            )

        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()

        # Create dummy_command record
        dummy_command = WurkerCommand(
            name="Dummy Command",
            slug="dummy_command",
            description="A dummy command",
            command=os.path.join(etc_root, "scripts", "test_bee.sh"),
            is_module=False,
            app_id=wurkman.app_id,
        )
        db.add(dummy_command)
        db.flush()

        # Create dummy_cron record
        dummy_cron = WurkerCron(
            name="Dummy Cron",
            slug="dummy_cron",
            cron="*/5 * * * *",
            overrun=2,
            app_id=wurkman.app_id,
        )
        db.add(dummy_cron)
        db.flush()

        # Create dummy_bee record
        dummy_bee = WurkerBee(
            name="Dummy Bee",
            slug="dummy_bee",
            description="A dummy bee",
            command_id=dummy_command.id,
            args="-l /tmp/dummy_bee.log -i 10 -d 40",
            quiet_restart=False,
            enabled=True,
            app_id=wurkman.app_id,
        )

        db.add(dummy_bee)

        db.commit()


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

    with wurkman.session() as db:
        dummy_bee_id = db.query(WurkerBee.id).filter(
            WurkerBee.slug == "dummy_bee",
            WurkerBee.app_id == conf.app_id
        ).scalar()

        if dummy_bee_id:
            db.execute(
                wurker_bee_cron.delete().where(wurker_bee_cron.c.wurker_bee_id == dummy_bee_id)
            )

        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():
    data = {"name": "Edited Command"}

    cmd = EditCommand()
    cmd.run("command", "dummy_command", data)

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

        assert command is not None
        assert command.name == "Edited Command"


def test_02_cron():
    data = {"name": "Edited Cron"}

    cmd = EditCommand()
    cmd.run("cron", "dummy_cron", data)

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

        assert cron is not None
        assert cron.name == "Edited Cron"


def test_03_bee():
    data = {"name": "Edited Bee", "cron_slug": "minutes_1_overrun"}

    cmd = EditCommand()
    cmd.run("bee", "dummy_bee", data)

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

        assert bee is not None
        assert bee.name == "Edited Bee"
        assert bee.crons[0].slug == "minutes_1_overrun"
