from app.core.orchestration.migration.dual_run import DualRunMode
from app.core.orchestration.migration.repository import PostgresMigrationStore


class Connection:
    def __init__(self):
        self.parameters = []

    def execute(self, _statement, parameters):
        self.parameters.append(parameters)


def test_cutover_disables_n8n_binding_before_enabling_kestra_primary():
    connection = Connection()

    PostgresMigrationStore._sync_engine_bindings(
        connection,
        DualRunMode.KESTRA_PRIMARY_N8N_STANDBY.value,
        n8n_definition_id="n8n-orders",
        kestra_definition_id="kestra-orders",
    )

    assert connection.parameters == [
        {
            "role": "standby",
            "status": "disabled",
            "engine_type": "n8n",
            "definition_id": "n8n-orders",
        },
        {
            "role": "primary",
            "status": "enabled",
            "engine_type": "kestra",
            "definition_id": "kestra-orders",
        },
    ]


def test_rollback_disables_kestra_binding_before_restoring_n8n_primary():
    connection = Connection()

    PostgresMigrationStore._sync_engine_bindings(
        connection,
        DualRunMode.N8N_PRIMARY_KESTRA_SHADOW.value,
        n8n_definition_id="n8n-orders",
        kestra_definition_id="kestra-orders",
    )

    assert [item["engine_type"] for item in connection.parameters] == [
        "kestra",
        "n8n",
    ]
    assert connection.parameters[0]["status"] == "disabled"
    assert connection.parameters[1]["role"] == "primary"
