from collections.abc import Sequence
from typing import Annotated

from dishka import FromDishka
from dishka.integrations.fastapi import DishkaRoute
from fastapi import APIRouter, Query
from pydantic import BaseModel, Field

from template_project.adapters.data_gateways.key_skills import KeySkillsDataGateway
from template_project.application.common.unit_of_work import UnitOfWork

router = APIRouter(
    route_class=DishkaRoute,
    tags=["Key Skills"],
)


class KeySkill(BaseModel):
    name: str = Field(description="The name of the key skill")


@router.get("/key_skills")
async def query_key_skills(
    query: Annotated[str, Query()],
    data_gateway: FromDishka[KeySkillsDataGateway],
) -> Sequence[KeySkill]:
    skills = await data_gateway.query(query)
    return [KeySkill(name=skill) for skill in skills]


class AddKeySkillsRequest(BaseModel):
    key_skills: list[str]


@router.post("/key_skills")
async def add_key_skill(
    request: AddKeySkillsRequest,
    unit_of_work: FromDishka[UnitOfWork],
    data_gateway: FromDishka[KeySkillsDataGateway],
) -> None:
    print(request.key_skills)
    await data_gateway.add_skills(request.key_skills)
    await unit_of_work.commit()
