import asyncio
from collections.abc import Sequence
from pathlib import Path

from csirtmu.self_service_backend import models
from csirtmu.self_service_backend.config import AppConfig
from csirtmu.self_service_backend.repository import StepRepository


class Exporter:
    def _export(
        self,
        step_config_path: str,
        data: Sequence[models.Step],
    ) -> None:
        for step in data:
            filename = f"{step.id}"
            if isinstance(step, models.TextStep):
                filename += "_text_step.yaml"
            elif isinstance(step, models.LinkStep):
                filename += "_link_step.yaml"
            elif isinstance(step, models.FormStep):
                filename += "_form_step.yaml"
            elif isinstance(step, models.NetboxSearchStep):
                filename += "_netbox_step.yaml"
            else:
                filename += "_outline_step.yaml"
            with (Path(step_config_path) / filename).open("w") as file:
                file.write(str(step))

    def export_all(self) -> None:
        c = AppConfig.get()
        steps = asyncio.run(StepRepository(c.database).get_all_steps(None))
        self._export("config/steps/out", steps)


if __name__ == "__main__":
    Exporter().export_all()
