"""Referral Program APIs — CRUD (Phase 7.5)."""
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 (
    LOYALTY_REFERRAL_PROGRAMS_CREATE,
    LOYALTY_REFERRAL_PROGRAMS_DELETE,
    LOYALTY_REFERRAL_PROGRAMS_UPDATE,
    LOYALTY_REFERRAL_PROGRAMS_VIEW,
)
from app.schemas.referral import (
    ReferralProgramCreate,
    ReferralProgramRead,
    ReferralProgramUpdate,
)
from app.services.referral_engine import ReferralEngineService
from shared.pagination import PaginationParams
from shared.security import CurrentUser

router = APIRouter()


@router.post("", response_model=ReferralProgramRead, status_code=status.HTTP_201_CREATED)
async def create_referral_program(
    body: ReferralProgramCreate,
    tenant_id: UUID = Depends(require_tenant),
    db: AsyncSession = Depends(get_db),
    user: CurrentUser = Depends(require_permissions(LOYALTY_REFERRAL_PROGRAMS_CREATE)),
):
    return await ReferralEngineService(db).create_program(tenant_id, body, actor=user)


@router.get("", response_model=list[ReferralProgramRead])
async def list_referral_programs(
    tenant_id: UUID = Depends(require_tenant),
    pagination: PaginationParams = Depends(get_pagination),
    db: AsyncSession = Depends(get_db),
    _user: CurrentUser = Depends(require_permissions(LOYALTY_REFERRAL_PROGRAMS_VIEW)),
):
    return await ReferralEngineService(db).list_programs(
        tenant_id, offset=pagination.offset, limit=pagination.page_size
    )


@router.get("/{referral_program_id}", response_model=ReferralProgramRead)
async def get_referral_program(
    referral_program_id: UUID,
    tenant_id: UUID = Depends(require_tenant),
    db: AsyncSession = Depends(get_db),
    _user: CurrentUser = Depends(require_permissions(LOYALTY_REFERRAL_PROGRAMS_VIEW)),
):
    return await ReferralEngineService(db).get_program(tenant_id, referral_program_id)


@router.patch("/{referral_program_id}", response_model=ReferralProgramRead)
async def update_referral_program(
    referral_program_id: UUID,
    body: ReferralProgramUpdate,
    tenant_id: UUID = Depends(require_tenant),
    db: AsyncSession = Depends(get_db),
    user: CurrentUser = Depends(require_permissions(LOYALTY_REFERRAL_PROGRAMS_UPDATE)),
):
    return await ReferralEngineService(db).update_program(
        tenant_id, referral_program_id, body, actor=user
    )


@router.post("/{referral_program_id}/delete", response_model=ReferralProgramRead)
async def soft_delete_referral_program(
    referral_program_id: UUID,
    tenant_id: UUID = Depends(require_tenant),
    db: AsyncSession = Depends(get_db),
    user: CurrentUser = Depends(require_permissions(LOYALTY_REFERRAL_PROGRAMS_DELETE)),
):
    return await ReferralEngineService(db).soft_delete_program(
        tenant_id, referral_program_id, actor=user
    )
