"""Pages APIs — Phase 11.1."""
from __future__ import annotations

from uuid import UUID

from fastapi import APIRouter, Depends, Query, status
from sqlalchemy.ext.asyncio import AsyncSession

from app.api.deps import get_db, get_pagination, require_tenant
from app.api.permissions import require_permissions
from app.permissions.definitions import (
    PAGES_CREATE,
    PAGES_DELETE,
    PAGES_MANAGE,
    PAGES_UPDATE,
    PAGES_VIEW,
)
from app.schemas.sites import ExperiencePageCreate, ExperiencePageRead, ExperiencePageUpdate
from app.services.sites import ExperiencePageService
from shared.pagination import PaginationParams
from shared.security import CurrentUser

router = APIRouter()


@router.post("", response_model=ExperiencePageRead, status_code=status.HTTP_201_CREATED)
async def create_page(
    body: ExperiencePageCreate,
    tenant_id: UUID = Depends(require_tenant),
    db: AsyncSession = Depends(get_db),
    user: CurrentUser = Depends(require_permissions(PAGES_CREATE)),
):
    return await ExperiencePageService(db).create(tenant_id, body, actor=user)


@router.get("", response_model=list[ExperiencePageRead])
async def list_pages(
    site_id: UUID | None = Query(default=None),
    tenant_id: UUID = Depends(require_tenant),
    pagination: PaginationParams = Depends(get_pagination),
    db: AsyncSession = Depends(get_db),
    _user: CurrentUser = Depends(require_permissions(PAGES_VIEW)),
):
    svc = ExperiencePageService(db)
    if site_id is not None:
        return await svc.list_by_site(
            tenant_id, site_id, offset=pagination.offset, limit=pagination.page_size
        )
    return await svc.list(
        tenant_id, offset=pagination.offset, limit=pagination.page_size
    )


@router.get("/{page_id}", response_model=ExperiencePageRead)
async def get_page(
    page_id: UUID,
    tenant_id: UUID = Depends(require_tenant),
    db: AsyncSession = Depends(get_db),
    _user: CurrentUser = Depends(require_permissions(PAGES_VIEW)),
):
    return await ExperiencePageService(db).get(tenant_id, page_id)


@router.patch("/{page_id}", response_model=ExperiencePageRead)
async def update_page(
    page_id: UUID,
    body: ExperiencePageUpdate,
    tenant_id: UUID = Depends(require_tenant),
    db: AsyncSession = Depends(get_db),
    user: CurrentUser = Depends(require_permissions(PAGES_UPDATE)),
):
    return await ExperiencePageService(db).update(tenant_id, page_id, body, actor=user)


@router.post("/{page_id}/publish", response_model=ExperiencePageRead)
async def publish_page(
    page_id: UUID,
    tenant_id: UUID = Depends(require_tenant),
    db: AsyncSession = Depends(get_db),
    user: CurrentUser = Depends(require_permissions(PAGES_MANAGE)),
):
    return await ExperiencePageService(db).publish(tenant_id, page_id, actor=user)


@router.post("/{page_id}/unpublish", response_model=ExperiencePageRead)
async def unpublish_page(
    page_id: UUID,
    tenant_id: UUID = Depends(require_tenant),
    db: AsyncSession = Depends(get_db),
    user: CurrentUser = Depends(require_permissions(PAGES_MANAGE)),
):
    return await ExperiencePageService(db).unpublish(tenant_id, page_id, actor=user)


@router.post("/{page_id}/archive", response_model=ExperiencePageRead)
async def archive_page(
    page_id: UUID,
    tenant_id: UUID = Depends(require_tenant),
    db: AsyncSession = Depends(get_db),
    user: CurrentUser = Depends(require_permissions(PAGES_MANAGE)),
):
    return await ExperiencePageService(db).archive(tenant_id, page_id, actor=user)


@router.post("/{page_id}/delete", response_model=ExperiencePageRead)
async def soft_delete_page(
    page_id: UUID,
    tenant_id: UUID = Depends(require_tenant),
    db: AsyncSession = Depends(get_db),
    user: CurrentUser = Depends(require_permissions(PAGES_DELETE)),
):
    return await ExperiencePageService(db).soft_delete(tenant_id, page_id, actor=user)
