"""Interview creation workflow APIs (setup → persona → questions)."""

from __future__ import annotations

from fastapi import APIRouter, Depends, status

from app.deps import PlatformActorContext, get_platform_actor_context, verify_internal_token
from app.schemas.interview_workflows import (
    InterviewWorkflowCreateRequest,
    InterviewWorkflowPatchRequest,
    InterviewWorkflowResponse,
    WorkflowQuestionGenerateRequest,
    WorkflowQuestionsBatchRequest,
)
from app.services.interviews.workflows import InterviewWorkflowService

router = APIRouter(prefix="/interview-workflows")
_service = InterviewWorkflowService()


@router.post(
    "",
    response_model=InterviewWorkflowResponse,
    response_model_by_alias=True,
    status_code=status.HTTP_200_OK,
    summary="Create interview workflow draft",
    description=(
        "Creates and persists Interview Setup (title, role, skills, duration, "
        "language, difficulty, mode). Returns workflowId + interviewId. "
        "Subsequent AI steps load this saved config — do not resend full setup."
    ),
    dependencies=[Depends(verify_internal_token)],
)
async def create_interview_workflow(
    payload: InterviewWorkflowCreateRequest,
    actor: PlatformActorContext = Depends(get_platform_actor_context),
) -> InterviewWorkflowResponse:
    return _service.create(payload, actor=actor)


@router.get(
    "/{workflow_id}",
    response_model=InterviewWorkflowResponse,
    response_model_by_alias=True,
    summary="Get interview workflow",
    description="Returns the saved setup, persona, and questions for a workflow.",
    dependencies=[Depends(verify_internal_token)],
)
async def get_interview_workflow(
    workflow_id: str,
    actor: PlatformActorContext = Depends(get_platform_actor_context),
) -> InterviewWorkflowResponse:
    return _service.get(workflow_id, actor=actor)


@router.patch(
    "/{workflow_id}",
    response_model=InterviewWorkflowResponse,
    response_model_by_alias=True,
    summary="Update interview workflow / persona",
    description=(
        "Partial update of setup fields and/or personaId. Only supplied fields change."
    ),
    dependencies=[Depends(verify_internal_token)],
)
async def patch_interview_workflow(
    workflow_id: str,
    payload: InterviewWorkflowPatchRequest,
    actor: PlatformActorContext = Depends(get_platform_actor_context),
) -> InterviewWorkflowResponse:
    return _service.patch(workflow_id, payload, actor=actor)


@router.post(
    "/{workflow_id}/questions/generate",
    response_model=InterviewWorkflowResponse,
    response_model_by_alias=True,
    summary="Generate AI questions for workflow",
    description=(
        "Requires `personaId` (built-in catalog id or custom id from POST /personas). "
        "Persists the persona on the workflow, generates questions via "
        "`QuestionsService.generate_from_role` in the workflow language using that "
        "persona, and marks the workflow `ready_for_scheduling` when questions exist. "
        "Custom questions are kept when replaceExisting is true for AI rows only. "
        "Preview endpoint removed — this call completes the design workflow."
    ),
    dependencies=[Depends(verify_internal_token)],
)
async def generate_workflow_questions(
    workflow_id: str,
    payload: WorkflowQuestionGenerateRequest,
    actor: PlatformActorContext = Depends(get_platform_actor_context),
) -> InterviewWorkflowResponse:
    return await _service.generate_questions(workflow_id, payload, actor=actor)


@router.post(
    "/{workflow_id}/questions/batch",
    response_model=InterviewWorkflowResponse,
    response_model_by_alias=True,
    status_code=status.HTTP_200_OK,
    summary="Batch create / update / delete / reorder questions",
    description=(
        "Single transactional endpoint for custom question edits. "
        "Body may include `create`, `update` (with questionId), `delete` "
        "(questionId list), and/or `reorder` (full questionId order). "
        "Applied as delete → update → create → reorder. "
        "Replaces former POST/PATCH/DELETE `/questions` and PUT `/questions/reorder`."
    ),
    dependencies=[Depends(verify_internal_token)],
)
async def batch_workflow_questions(
    workflow_id: str,
    payload: WorkflowQuestionsBatchRequest,
    actor: PlatformActorContext = Depends(get_platform_actor_context),
) -> InterviewWorkflowResponse:
    return _service.batch_questions(workflow_id, payload, actor=actor)
