#!/usr/bin/env python3
import asyncio
import json
from pathlib import Path

from template_project.adapters.data_gateways.key_skills import KeySkillsDataGateway
from template_project.adapters.unit_of_work import DefaultUnitOfWork
from template_project.web_api.configuration import load_configuration
from template_project.web_api.ioc.make import make_ioc


async def main() -> None:
    config_path = Path("config.toml")
    configuration = load_configuration(config_path)

    container = make_ioc(configuration)

    try:
        async with container() as request_container:
            data_gateway = await request_container.get(KeySkillsDataGateway)
            unit_of_work = await request_container.get(DefaultUnitOfWork)

            json_path = Path("full_skills_unique.json")
            with json_path.open("r", encoding="utf-8") as f:
                all_skills: list[str] = json.load(f)

            skills_to_load = all_skills[:50000]

            print(f"Загружаю {len(skills_to_load)} скиллов в БД...")

            batch_size = 100
            for i in range(0, len(skills_to_load), batch_size):
                batch = skills_to_load[i : i + batch_size]
                await data_gateway.add_skills(batch)
                await unit_of_work.commit()
                print(f"Загружено {min(i + batch_size, len(skills_to_load))} / {len(skills_to_load)}")

            print("Готово!")
    finally:
        await container.close()


if __name__ == "__main__":
    asyncio.run(main())
