""" Superadmin endpoints for organization management. """ from typing import Annotated from fastapi import APIRouter, Depends, HTTPException, Query, status from sqlalchemy.ext.asyncio import AsyncSession from app.api.deps import get_current_superadmin from app.core.database import get_db from app.models.user import User from app.schemas.organization import ( OrganizationCreate, OrganizationListResponse, OrganizationResponse, OrganizationUpdate, ) from app.services import organization_service router = APIRouter() @router.get("", response_model=OrganizationListResponse) async def list_organizations( db: Annotated[AsyncSession, Depends(get_db)], current_user: Annotated[User, Depends(get_current_superadmin)], skip: int = Query(0, ge=0, description="Number of records to skip"), limit: int = Query(100, ge=1, le=1000, description="Max records to return"), status: str | None = Query(None, description="Filter by status"), ): """ List all organizations (superadmin only). Returns paginated list of organizations with optional status filter. """ organizations, total = await organization_service.list_organizations( db, skip=skip, limit=limit, status=status ) return OrganizationListResponse( organizations=organizations, total=total, ) @router.get("/{organization_id}", response_model=OrganizationResponse) async def get_organization( organization_id: int, db: Annotated[AsyncSession, Depends(get_db)], current_user: Annotated[User, Depends(get_current_superadmin)], ): """ Get organization by ID (superadmin only). """ org = await organization_service.get_organization(db, organization_id) if not org: raise HTTPException( status_code=status.HTTP_404_NOT_FOUND, detail="Organization not found", ) return org @router.post("", response_model=OrganizationResponse, status_code=status.HTTP_201_CREATED) async def create_organization( data: OrganizationCreate, db: Annotated[AsyncSession, Depends(get_db)], current_user: Annotated[User, Depends(get_current_superadmin)], ): """ Create a new organization (superadmin only). Creates an organization with active status and specified product modules. """ org = await organization_service.create_organization(db, data) return org @router.patch("/{organization_id}", response_model=OrganizationResponse) async def update_organization( organization_id: int, data: OrganizationUpdate, db: Annotated[AsyncSession, Depends(get_db)], current_user: Annotated[User, Depends(get_current_superadmin)], ): """ Update organization (superadmin only). Can update organization details, status, and enable/disable product modules. """ org = await organization_service.update_organization(db, organization_id, data) if not org: raise HTTPException( status_code=status.HTTP_404_NOT_FOUND, detail="Organization not found", ) return org @router.delete("/{organization_id}", status_code=status.HTTP_204_NO_CONTENT) async def delete_organization( organization_id: int, db: Annotated[AsyncSession, Depends(get_db)], current_user: Annotated[User, Depends(get_current_superadmin)], ): """ Delete organization (superadmin only). Warning: This will cascade delete all users and devices in the organization. """ deleted = await organization_service.delete_organization(db, organization_id) if not deleted: raise HTTPException( status_code=status.HTTP_404_NOT_FOUND, detail="Organization not found", )