from __future__ import annotations

from dataclasses import dataclass, field
from uuid import uuid4

from packages.domain import GenerationTask
from packages.ports.generation_task_repository import GenerationTaskRepository


@dataclass(slots=True)
class CreateGenerationTaskCommand:
    project_id: str = ""
    asset_library_id: str = ""
    strategy_id: str = ""
    voice_library_id: str = ""
    template_id: str = ""
    asset_ids: list[str] = field(default_factory=list)
    title_ids: list[str] = field(default_factory=list)
    voice_ids: list[str] = field(default_factory=list)
    created_by_user_id: str = ""
    source_edit_plan_id: str = ""
    asset_select_mode: str = ""
    batch_id: str = ""
    is_preview: bool = True
    source_task_id: str = ""
    output_width: int = 1280
    output_height: int = 720
    cover_url: str = ""
    custom_title: str = ""


class CreateGenerationTaskUseCase:
    def __init__(self, generation_task_repository: GenerationTaskRepository):
        self.generation_task_repository = generation_task_repository

    def execute(self, command: CreateGenerationTaskCommand) -> GenerationTask:
        task = GenerationTask(
            id=uuid4().hex,
            project_id=command.project_id,
            asset_library_id=command.asset_library_id,
            strategy_id=command.strategy_id,
            voice_library_id=command.voice_library_id,
            template_id=command.template_id,
            asset_ids=command.asset_ids,
            title_ids=command.title_ids,
            voice_ids=command.voice_ids,
            status="pending",
            progress=0.0,
            result_count=0,
            error_message="",
            started_at=None,
            completed_at=None,
            created_by_user_id=command.created_by_user_id,
            source_edit_plan_id=command.source_edit_plan_id,
            asset_select_mode=command.asset_select_mode,
            batch_id=command.batch_id,
            is_preview=command.is_preview,
            source_task_id=command.source_task_id,
            output_width=command.output_width,
            output_height=command.output_height,
            cover_url=command.cover_url,
            custom_title=command.custom_title,
        )
        return self.generation_task_repository.create(task)


class GetGenerationTaskUseCase:
    def __init__(self, generation_task_repository: GenerationTaskRepository):
        self.generation_task_repository = generation_task_repository

    def execute(self, task_id: str) -> GenerationTask | None:
        return self.generation_task_repository.get(task_id)
