"""APIهای مدیریت قابلیت‌ها (Features)."""
from __future__ import annotations

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

from app.api.deps import get_db, get_pagination
from app.core.security import require_authenticated, require_platform_admin
from app.schemas.plan import FeatureCreate, FeatureRead
from app.services.plan_service import PlanService
from shared.pagination import Page, PaginationParams

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


@router.post("", response_model=FeatureRead, status_code=status.HTTP_201_CREATED)
async def create_feature(
    payload: FeatureCreate,
    db: AsyncSession = Depends(get_db),
    _user=Depends(require_platform_admin),
) -> FeatureRead:
    feature = await PlanService(db).create_feature(payload)
    return FeatureRead.model_validate(feature)


@router.get("", response_model=Page[FeatureRead])
async def list_features(
    pagination: PaginationParams = Depends(get_pagination),
    db: AsyncSession = Depends(get_db),
    _user=Depends(require_authenticated),
) -> Page[FeatureRead]:
    items, total = await PlanService(db).list_features(
        offset=pagination.offset, limit=pagination.limit
    )
    return Page.create(
        [FeatureRead.model_validate(f) for f in items], total, pagination
    )
