from __future__ import annotations

from dataclasses import dataclass

from app.core.api.application.filesystem_snapshot_resolver import FilesystemSnapshotResolver
from app.core.api.application.session_service import SessionService
from app.core.api.domain.models.agent_session import AgentSession
from app.core.rag.indexing import IndexJob
from app.core.rag.module import RagModule


@dataclass(slots=True)
class BootstrappedAgentSession:
    session: AgentSession
    index_job: IndexJob


class SessionBootstrapService:
    def __init__(
        self,
        sessions: SessionService,
        rag: RagModule,
        snapshot_resolver: FilesystemSnapshotResolver | None = None,
    ) -> None:
        self._sessions = sessions
        self._rag = rag
        self._snapshot_resolver = snapshot_resolver or FilesystemSnapshotResolver()

    async def create(self, project_id: str, files: list[dict]) -> BootstrappedAgentSession:
        resolved_files = self._snapshot_resolver.augment(project_id=project_id, files=files)
        rag_session, index_job = await self._rag.create_session(project_id=project_id, files=resolved_files)
        session = self._sessions.create(rag_session_id=rag_session.rag_session_id)
        return BootstrappedAgentSession(session=session, index_job=index_job)
