"""Reward catalog and redemption APIs — Phase 7.0 shell + Phase 7.3 rewards engine."""
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_REDEMPTIONS_VIEW,
    LOYALTY_REWARDS_CREATE,
    LOYALTY_REWARDS_DELETE,
    LOYALTY_REWARDS_REDEEM,
    LOYALTY_REWARDS_UPDATE,
    LOYALTY_REWARDS_VIEW,
)
from app.schemas.foundation import RewardCreate, RewardRead, RewardUpdate
from app.schemas.rewards import RedeemRequest, RewardRedemptionPage, RewardRedemptionRead
from app.services.foundation import RewardService
from app.services.rewards_engine import RewardsEngineService
from shared.pagination import PaginationParams
from shared.security import CurrentUser

router = APIRouter()


@router.post("", response_model=RewardRead, status_code=status.HTTP_201_CREATED)
async def create_reward(
    body: RewardCreate,
    tenant_id: UUID = Depends(require_tenant),
    db: AsyncSession = Depends(get_db),
    user: CurrentUser = Depends(require_permissions(LOYALTY_REWARDS_CREATE)),
):
    return await RewardService(db).create(tenant_id, body, actor=user)


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


@router.get("/{reward_id}", response_model=RewardRead)
async def get_reward(
    reward_id: UUID,
    tenant_id: UUID = Depends(require_tenant),
    db: AsyncSession = Depends(get_db),
    _user: CurrentUser = Depends(require_permissions(LOYALTY_REWARDS_VIEW)),
):
    return await RewardService(db).get(tenant_id, reward_id)


@router.patch("/{reward_id}", response_model=RewardRead)
async def update_reward(
    reward_id: UUID,
    body: RewardUpdate,
    tenant_id: UUID = Depends(require_tenant),
    db: AsyncSession = Depends(get_db),
    user: CurrentUser = Depends(require_permissions(LOYALTY_REWARDS_UPDATE)),
):
    return await RewardService(db).update(tenant_id, reward_id, body, actor=user)


@router.post("/{reward_id}/delete", response_model=RewardRead)
async def soft_delete_reward(
    reward_id: UUID,
    tenant_id: UUID = Depends(require_tenant),
    db: AsyncSession = Depends(get_db),
    user: CurrentUser = Depends(require_permissions(LOYALTY_REWARDS_DELETE)),
):
    return await RewardService(db).soft_delete(tenant_id, reward_id, actor=user)


@router.post(
    "/{reward_id}/redeem",
    response_model=RewardRedemptionRead,
    status_code=status.HTTP_201_CREATED,
)
async def redeem_reward(
    reward_id: UUID,
    body: RedeemRequest,
    tenant_id: UUID = Depends(require_tenant),
    db: AsyncSession = Depends(get_db),
    user: CurrentUser = Depends(require_permissions(LOYALTY_REWARDS_REDEEM)),
):
    return await RewardsEngineService(db).redeem(tenant_id, reward_id, body, actor=user)


@router.get("/{reward_id}/redemptions", response_model=RewardRedemptionPage)
async def list_reward_redemptions(
    reward_id: UUID,
    pagination: PaginationParams = Depends(get_pagination),
    tenant_id: UUID = Depends(require_tenant),
    db: AsyncSession = Depends(get_db),
    _user: CurrentUser = Depends(require_permissions(LOYALTY_REDEMPTIONS_VIEW)),
):
    rows, total = await RewardsEngineService(db).list(
        tenant_id,
        reward_id=reward_id,
        offset=pagination.offset,
        limit=pagination.page_size,
    )
    return RewardRedemptionPage(
        items=rows, total=total, offset=pagination.offset, limit=pagination.page_size
    )
