"""Membership APIs — Phase 9.0 + Phase 9.2 assignment/status."""
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.core.security import get_current_user
from app.models.types import MembershipStatus
from app.schemas.foundation import (
    MembershipCreate,
    MembershipRead,
    MembershipUpdate,
)
from app.schemas.members import (
    MembershipAssignMemberRequest,
    MembershipFreezeRequest,
    MembershipStatusChangeRequest,
)
from app.services.foundation import MembershipService
from shared.pagination import PaginationParams
from shared.security import CurrentUser

router = APIRouter()


@router.post("", response_model=MembershipRead, status_code=status.HTTP_201_CREATED)
async def create_item(
    body: MembershipCreate,
    tenant_id: UUID = Depends(require_tenant),
    db: AsyncSession = Depends(get_db),
    user: CurrentUser = Depends(get_current_user),
):
    return await MembershipService(db).create(tenant_id, body, actor=user)


@router.get("", response_model=list[MembershipRead])
async def list_items(
    tenant_id: UUID = Depends(require_tenant),
    pagination: PaginationParams = Depends(get_pagination),
    db: AsyncSession = Depends(get_db),
    _user: CurrentUser = Depends(get_current_user),
):
    return await MembershipService(db).list(tenant_id, offset=pagination.offset, limit=pagination.page_size)


@router.get("/{membership_id}", response_model=MembershipRead)
async def get_item(
    membership_id: UUID,
    tenant_id: UUID = Depends(require_tenant),
    db: AsyncSession = Depends(get_db),
    _user: CurrentUser = Depends(get_current_user),
):
    return await MembershipService(db).get(tenant_id, membership_id)


@router.patch("/{membership_id}", response_model=MembershipRead)
async def update_item(
    membership_id: UUID,
    body: MembershipUpdate,
    tenant_id: UUID = Depends(require_tenant),
    db: AsyncSession = Depends(get_db),
    user: CurrentUser = Depends(get_current_user),
):
    return await MembershipService(db).update(tenant_id, membership_id, body, actor=user)


@router.post("/{membership_id}/assign-member", response_model=MembershipRead)
async def assign_member(
    membership_id: UUID,
    body: MembershipAssignMemberRequest,
    tenant_id: UUID = Depends(require_tenant),
    db: AsyncSession = Depends(get_db),
    user: CurrentUser = Depends(get_current_user),
):
    return await MembershipService(db).assign_member(
        tenant_id, membership_id, body.member_id, version=body.version, actor=user
    )


@router.post("/{membership_id}/activate", response_model=MembershipRead)
async def activate_membership(
    membership_id: UUID,
    body: MembershipStatusChangeRequest | None = None,
    tenant_id: UUID = Depends(require_tenant),
    db: AsyncSession = Depends(get_db),
    user: CurrentUser = Depends(get_current_user),
):
    req = body or MembershipStatusChangeRequest()
    return await MembershipService(db).change_status(
        tenant_id, membership_id, MembershipStatus.ACTIVE, version=req.version, actor=user
    )


@router.post("/{membership_id}/suspend", response_model=MembershipRead)
async def suspend_membership(
    membership_id: UUID,
    body: MembershipStatusChangeRequest | None = None,
    tenant_id: UUID = Depends(require_tenant),
    db: AsyncSession = Depends(get_db),
    user: CurrentUser = Depends(get_current_user),
):
    req = body or MembershipStatusChangeRequest()
    return await MembershipService(db).change_status(
        tenant_id, membership_id, MembershipStatus.SUSPENDED, version=req.version, actor=user
    )


@router.post("/{membership_id}/cancel", response_model=MembershipRead)
async def cancel_membership(
    membership_id: UUID,
    body: MembershipStatusChangeRequest | None = None,
    tenant_id: UUID = Depends(require_tenant),
    db: AsyncSession = Depends(get_db),
    user: CurrentUser = Depends(get_current_user),
):
    req = body or MembershipStatusChangeRequest()
    return await MembershipService(db).change_status(
        tenant_id, membership_id, MembershipStatus.CANCELLED, version=req.version, actor=user
    )


@router.post("/{membership_id}/freeze", response_model=MembershipRead)
async def freeze_membership(
    membership_id: UUID,
    body: MembershipFreezeRequest,
    tenant_id: UUID = Depends(require_tenant),
    db: AsyncSession = Depends(get_db),
    user: CurrentUser = Depends(get_current_user),
):
    return await MembershipService(db).change_status(
        tenant_id,
        membership_id,
        MembershipStatus.FROZEN,
        freeze_starts_on=body.freeze_starts_on,
        freeze_ends_on=body.freeze_ends_on,
        version=body.version,
        actor=user,
    )


@router.post("/{membership_id}/unfreeze", response_model=MembershipRead)
async def unfreeze_membership(
    membership_id: UUID,
    body: MembershipStatusChangeRequest | None = None,
    tenant_id: UUID = Depends(require_tenant),
    db: AsyncSession = Depends(get_db),
    user: CurrentUser = Depends(get_current_user),
):
    req = body or MembershipStatusChangeRequest()
    return await MembershipService(db).change_status(
        tenant_id, membership_id, MembershipStatus.ACTIVE, version=req.version, actor=user
    )


@router.post("/{membership_id}/delete", response_model=MembershipRead)
async def soft_delete_item(
    membership_id: UUID,
    tenant_id: UUID = Depends(require_tenant),
    db: AsyncSession = Depends(get_db),
    user: CurrentUser = Depends(get_current_user),
):
    return await MembershipService(db).soft_delete(tenant_id, membership_id, actor=user)
