from template_project.application.common.data_structure import to_data_structure
from template_project.application.common.enums import EducationGrade, ExperienceType
from template_project.application.common.identity_provider import IdentityProvider
from template_project.application.common.interactor import to_interactor
from template_project.application.common.unit_of_work import UnitOfWork
from template_project.application.resume.entity import (
    Resume,
    ResumeEducation,
    ResumeExperience,
    ResumeId,
    ResumeProject,
)


@to_data_structure
class ExperienceInput:
    place: str
    description: str
    months_duration: int


@to_data_structure
class EducationInput:
    place: str
    grade: EducationGrade
    specialization: str
    description: str | None = None


@to_data_structure
class ProjectInput:
    name: str
    description: str


@to_interactor
class AddResumeInteractor:
    unit_of_work: UnitOfWork
    identity_provider: IdentityProvider

    async def execute(
        self,
        position: str,
        location: str,
        about_me: str,
        key_skills: list[str],
        experience_type: ExperienceType,
        experience: list[ExperienceInput] | None = None,
        education: list[EducationInput] | None = None,
        projects: list[ProjectInput] | None = None,
    ) -> ResumeId:
        user = await self.identity_provider.get_current_user()

        resume = Resume.factory(
            user_id=user.id,
            position=position,
            location=location,
            about_me=about_me,
            key_skills=key_skills,
            experience_type=experience_type,
            down_resume_id=None,
            up_resume_id=None,
        )

        await self.unit_of_work.add(resume)

        if experience:
            for exp in experience:
                resume_experience = ResumeExperience.factory(
                    resume_id=resume.id,
                    place=exp.place,
                    description=exp.description,
                    months_duration=exp.months_duration,
                )
                await self.unit_of_work.add(resume_experience)

        if education:
            for edu in education:
                resume_education = ResumeEducation.factory(
                    resume_id=resume.id,
                    place=edu.place,
                    grade=edu.grade,
                    specialization=edu.specialization,
                    description=edu.description,
                )
                await self.unit_of_work.add(resume_education)

        if projects:
            for proj in projects:
                resume_project = ResumeProject.factory(
                    resume_id=resume.id,
                    name=proj.name,
                    description=proj.description,
                )
                await self.unit_of_work.add(resume_project)

        await self.unit_of_work.commit()

        return resume.id
