"""Task definitions for integration tests.

These tasks are registered with the Celery worker via the
``celery_includes`` fixture so the worker knows about them
before it starts consuming messages.
"""

from __future__ import annotations

from typing import Any

from celery import shared_task

# Counter for the retry test. Shared state works because the worker
# runs in the same process (solo pool, background thread).
_flaky_attempt_count: int = 0


def reset_flaky_counter() -> None:
    global _flaky_attempt_count  # noqa: PLW0603
    _flaky_attempt_count = 0


@shared_task
def add(x: int, y: int) -> int:
    """Simple task that succeeds — happy path test."""
    return x + y


@shared_task
def fail_task() -> None:
    """Task that always fails — failure capture test."""
    raise ValueError("payment processing failed")


@shared_task(bind=True, max_retries=3)
def flaky_task(self: Any) -> str:
    """Task that fails twice then succeeds — retry cycle test."""
    global _flaky_attempt_count  # noqa: PLW0603
    _flaky_attempt_count += 1
    if _flaky_attempt_count <= 2:
        raise self.retry(
            exc=RuntimeError(f"attempt {_flaky_attempt_count} failed"),
            countdown=0,
        )
    return "ok"


@shared_task
def resilient_add(x: int, y: int) -> int:
    """Task for API-unreachable test — must complete despite SDK failure."""
    return x + y


@shared_task
def safe_multiply(x: int, y: int) -> int:
    """Task for exception-in-transport test."""
    return x * y


@shared_task
def create_user(username: str, password: str) -> str:
    """Task with sensitive args — PII safety test."""
    return f"created {username}"


@shared_task
def process_payment(card_number: str, cvv: str, password: str) -> str:
    """Task with PII args — log safety test."""
    return "processed"
