"""Experience hub APIs — Phase 11.0."""
from __future__ import annotations

from uuid import UUID

from fastapi import APIRouter, Depends, 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 LOCALE_PROFILES_CREATE, LOCALE_PROFILES_DELETE, LOCALE_PROFILES_UPDATE, LOCALE_PROFILES_VIEW
from app.schemas.foundation import ExperienceLocaleProfileCreate, ExperienceLocaleProfileRead, ExperienceLocaleProfileUpdate
from app.services.foundation import ExperienceLocaleProfileService
from shared.pagination import PaginationParams
from shared.security import CurrentUser

router = APIRouter()


@router.post("", response_model=ExperienceLocaleProfileRead, status_code=status.HTTP_201_CREATED)
async def create_locale_profile(
    body: ExperienceLocaleProfileCreate,
    tenant_id: UUID = Depends(require_tenant),
    db: AsyncSession = Depends(get_db),
    user: CurrentUser = Depends(require_permissions(LOCALE_PROFILES_CREATE)),
):
    return await ExperienceLocaleProfileService(db).create(tenant_id, body, actor=user)


@router.get("", response_model=list[ExperienceLocaleProfileRead])
async def list_locale_profiles(
    tenant_id: UUID = Depends(require_tenant),
    pagination: PaginationParams = Depends(get_pagination),
    db: AsyncSession = Depends(get_db),
    _user: CurrentUser = Depends(require_permissions(LOCALE_PROFILES_VIEW)),
):
    return await ExperienceLocaleProfileService(db).list(
        tenant_id, offset=pagination.offset, limit=pagination.page_size
    )


@router.get("/{locale_profile_id}", response_model=ExperienceLocaleProfileRead)
async def get_locale_profile(
    locale_profile_id: UUID,
    tenant_id: UUID = Depends(require_tenant),
    db: AsyncSession = Depends(get_db),
    _user: CurrentUser = Depends(require_permissions(LOCALE_PROFILES_VIEW)),
):
    return await ExperienceLocaleProfileService(db).get(tenant_id, locale_profile_id)


@router.patch("/{locale_profile_id}", response_model=ExperienceLocaleProfileRead)
async def update_locale_profile(
    locale_profile_id: UUID,
    body: ExperienceLocaleProfileUpdate,
    tenant_id: UUID = Depends(require_tenant),
    db: AsyncSession = Depends(get_db),
    user: CurrentUser = Depends(require_permissions(LOCALE_PROFILES_UPDATE)),
):
    return await ExperienceLocaleProfileService(db).update(tenant_id, locale_profile_id, body, actor=user)


@router.post("/{locale_profile_id}/delete", response_model=ExperienceLocaleProfileRead)
async def soft_delete_locale_profile(
    locale_profile_id: UUID,
    tenant_id: UUID = Depends(require_tenant),
    db: AsyncSession = Depends(get_db),
    user: CurrentUser = Depends(require_permissions(LOCALE_PROFILES_DELETE)),
):
    return await ExperienceLocaleProfileService(db).soft_delete(tenant_id, locale_profile_id, actor=user)
