from __future__ import annotations

from fastapi import APIRouter, Depends
from sqlmodel import Session

from app.api.deps import get_session
from app.services.production_queue import (
    create_queue_item,
    ensure_recommendation_queue,
    list_queue,
    lock_next as lock_next_item,
    move_item,
    next_for_agent,
    replace_locked_with,
    unlock_next,
    update_queue_item,
)

router = APIRouter(prefix="/production-queue", tags=["production-queue"])


@router.get("")
def production_queue(include_archived: bool = False, session: Session = Depends(get_session)):
    return list_queue(session, include_archived=include_archived)


@router.post("")
def create_item(payload: dict, session: Session = Depends(get_session)):
    return create_queue_item(session, payload)


@router.post("/seed-recommendations")
def seed_recommendations(payload: dict | None = None, session: Session = Depends(get_session)):
    payload = payload or {}
    minimum = int(payload.get("minimum", 12))
    replace = bool(payload.get("replace_existing_suggestions", False))
    return ensure_recommendation_queue(session, minimum=minimum, replace_existing_suggestions=replace)


@router.post("/{item_id}")
def update_item(item_id: str, payload: dict, session: Session = Depends(get_session)):
    return update_queue_item(session, item_id, payload)


@router.post("/{item_id}/set-next")
def set_next(item_id: str, session: Session = Depends(get_session)):
    return update_queue_item(session, item_id, {"status": "approved_next", "position": 0})


@router.post("/{item_id}/lock-next")
def lock_next(item_id: str, session: Session = Depends(get_session)):
    return lock_next_item(session, item_id)


@router.post("/{item_id}/unlock")
def unlock(item_id: str, session: Session = Depends(get_session)):
    return unlock_next(session, item_id)


@router.post("/unlock")
def unlock_any(session: Session = Depends(get_session)):
    return unlock_next(session)


@router.post("/{item_id}/replace-locked")
def replace_locked(item_id: str, session: Session = Depends(get_session)):
    return replace_locked_with(session, item_id)


@router.post("/{item_id}/move-up")
def move_up(item_id: str, session: Session = Depends(get_session)):
    return move_item(session, item_id, "up")


@router.post("/{item_id}/move-down")
def move_down(item_id: str, session: Session = Depends(get_session)):
    return move_item(session, item_id, "down")


@router.post("/{item_id}/send-to-concept")
def send_to_concept(item_id: str, session: Session = Depends(get_session)):
    return update_queue_item(session, item_id, {"status": "concept_proposed"})


@router.post("/{item_id}/send-to-production")
def send_to_production(item_id: str, session: Session = Depends(get_session)):
    """Legacy UI/API affordance kept safe: it may propose a concept, never start production."""
    return update_queue_item(session, item_id, {"status": "concept_proposed"})


@router.post("/{item_id}/hold")
def hold(item_id: str, session: Session = Depends(get_session)):
    return update_queue_item(session, item_id, {"status": "held"})


@router.post("/{item_id}/archive")
def archive(item_id: str, session: Session = Depends(get_session)):
    return update_queue_item(session, item_id, {"status": "archived"})


@router.get("/next")
def queue_next(session: Session = Depends(get_session)):
    return next_for_agent(session)
