"""Sites 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 (
    SITES_CREATE,
    SITES_DELETE,
    SITES_MANAGE,
    SITES_UPDATE,
    SITES_VIEW,
)
from app.schemas.sites import ExperienceSiteCreate, ExperienceSiteRead, ExperienceSiteUpdate
from app.services.sites import ExperienceSiteService
from shared.pagination import PaginationParams
from shared.security import CurrentUser

router = APIRouter()


@router.post("", response_model=ExperienceSiteRead, status_code=status.HTTP_201_CREATED)
async def create_site(
    body: ExperienceSiteCreate,
    tenant_id: UUID = Depends(require_tenant),
    db: AsyncSession = Depends(get_db),
    user: CurrentUser = Depends(require_permissions(SITES_CREATE)),
):
    return await ExperienceSiteService(db).create(tenant_id, body, actor=user)


@router.get("", response_model=list[ExperienceSiteRead])
async def list_sites(
    workspace_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(SITES_VIEW)),
):
    svc = ExperienceSiteService(db)
    if workspace_id is not None:
        return await svc.list_by_workspace(
            tenant_id,
            workspace_id,
            offset=pagination.offset,
            limit=pagination.page_size,
        )
    return await svc.list(
        tenant_id, offset=pagination.offset, limit=pagination.page_size
    )


@router.get("/{site_id}", response_model=ExperienceSiteRead)
async def get_site(
    site_id: UUID,
    tenant_id: UUID = Depends(require_tenant),
    db: AsyncSession = Depends(get_db),
    _user: CurrentUser = Depends(require_permissions(SITES_VIEW)),
):
    return await ExperienceSiteService(db).get(tenant_id, site_id)


@router.patch("/{site_id}", response_model=ExperienceSiteRead)
async def update_site(
    site_id: UUID,
    body: ExperienceSiteUpdate,
    tenant_id: UUID = Depends(require_tenant),
    db: AsyncSession = Depends(get_db),
    user: CurrentUser = Depends(require_permissions(SITES_UPDATE)),
):
    return await ExperienceSiteService(db).update(tenant_id, site_id, body, actor=user)


@router.post("/{site_id}/publish", response_model=ExperienceSiteRead)
async def publish_site(
    site_id: UUID,
    tenant_id: UUID = Depends(require_tenant),
    db: AsyncSession = Depends(get_db),
    user: CurrentUser = Depends(require_permissions(SITES_MANAGE)),
):
    return await ExperienceSiteService(db).publish(tenant_id, site_id, actor=user)


@router.post("/{site_id}/unpublish", response_model=ExperienceSiteRead)
async def unpublish_site(
    site_id: UUID,
    tenant_id: UUID = Depends(require_tenant),
    db: AsyncSession = Depends(get_db),
    user: CurrentUser = Depends(require_permissions(SITES_MANAGE)),
):
    return await ExperienceSiteService(db).unpublish(tenant_id, site_id, actor=user)


@router.post("/{site_id}/archive", response_model=ExperienceSiteRead)
async def archive_site(
    site_id: UUID,
    tenant_id: UUID = Depends(require_tenant),
    db: AsyncSession = Depends(get_db),
    user: CurrentUser = Depends(require_permissions(SITES_MANAGE)),
):
    return await ExperienceSiteService(db).archive(tenant_id, site_id, actor=user)


@router.post("/{site_id}/delete", response_model=ExperienceSiteRead)
async def soft_delete_site(
    site_id: UUID,
    tenant_id: UUID = Depends(require_tenant),
    db: AsyncSession = Depends(get_db),
    user: CurrentUser = Depends(require_permissions(SITES_DELETE)),
):
    return await ExperienceSiteService(db).soft_delete(tenant_id, site_id, actor=user)
