"""Archive / unarchive routes. Thin: cascades + audit live in
services/archive.py.

v1 equivalents: POST /archive/{type}/{id} + POST /unarchive/{type}/{id}
(main.py ≈7270-7297). v1 redirected to the originating page (server-rendered
UI); v2 is an API — the archived-detail redirect behavior is the frontend's
concern.
"""
from __future__ import annotations

from fastapi import APIRouter, Depends
from sqlalchemy.orm import Session

from ..db import get_db
from ..deps import current_garden_id
from ..services.archive import archive_entity, unarchive_entity

router = APIRouter(prefix="/api", tags=["archive"])


@router.post("/archive/{entity_type}/{entity_id}")
def archive(
    entity_type: str,
    entity_id: int,
    db: Session = Depends(get_db),
    garden_id: int = Depends(current_garden_id),
) -> dict:
    """Archive an entity (with the v1 down-cascades: planting→plants,
    area→descendant areas, species→varieties; plant archive may up-cascade
    to its planting)."""
    archive_entity(db, garden_id, entity_type, entity_id)
    db.commit()
    return {"ok": True, "entity_type": entity_type, "entity_id": entity_id,
            "is_archived": True}


@router.post("/unarchive/{entity_type}/{entity_id}")
def unarchive(
    entity_type: str,
    entity_id: int,
    db: Session = Depends(get_db),
    garden_id: int = Depends(current_garden_id),
) -> dict:
    """Unarchive a single entity (no cascades — v1 semantics)."""
    unarchive_entity(db, garden_id, entity_type, entity_id)
    db.commit()
    return {"ok": True, "entity_type": entity_type, "entity_id": entity_id,
            "is_archived": False}
