"""Bounded Stripe I/O for worker-only agent purchases."""

import asyncio

import stripe


class AgentStripe:
    def __init__(self, api_key):
        self.client = stripe.StripeClient(
            api_key,
            max_network_retries=0,
            http_client=stripe.RequestsClient(timeout=10),
        )

    async def customer(self, customer_id):
        return await asyncio.to_thread(self.client.v1.customers.retrieve, customer_id)

    async def create_intent(self, snapshot, key):
        return await asyncio.to_thread(
            self.client.v1.payment_intents.create,
            {
                "amount": snapshot["amount_minor"],
                "currency": snapshot["currency"],
                "customer": snapshot["customer_id"],
                "payment_method": snapshot["payment_method"],
                "payment_method_types": ["card"],
                "metadata": {"agent_operation_id": key, "purchase_type": "agent_credit_purchase"},
            },
            {"idempotency_key": f"agent-intent:{key}"},
        )

    async def retrieve_intent(self, intent_id):
        return await asyncio.to_thread(self.client.v1.payment_intents.retrieve, intent_id)

    async def confirm_intent(self, intent_id, key):
        return await asyncio.to_thread(
            self.client.v1.payment_intents.confirm,
            intent_id,
            {"off_session": True},
            {"idempotency_key": f"agent-confirm:{key}"},
        )

    async def create_checkout(self, snapshot, key, dashboard):
        return await asyncio.to_thread(
            self.client.v1.checkout.sessions.create,
            {
                "mode": "payment",
                "customer": snapshot["customer_id"],
                "payment_method_types": ["card"],
                "line_items": [
                    {
                        "quantity": 1,
                        "price_data": {
                            "currency": snapshot["currency"],
                            "unit_amount": snapshot["amount_minor"],
                            "product_data": {"name": f"P2HS credits: {snapshot['package_slug']}"},
                        },
                    }
                ],
                "success_url": f"{dashboard}/settings/agents?purchase=complete",
                "cancel_url": f"{dashboard}/settings/agents?purchase=canceled",
                "metadata": {"agent_operation_id": key, "purchase_type": "agent_credit_purchase"},
            },
            {"idempotency_key": f"agent-checkout:{key}"},
        )

    async def retrieve_checkout(self, session_id):
        return await asyncio.to_thread(self.client.v1.checkout.sessions.retrieve, session_id)

    async def expire_checkout(self, session_id, key):
        return await asyncio.to_thread(
            self.client.v1.checkout.sessions.expire,
            session_id,
            {},
            {"idempotency_key": f"agent-expire:{key}"},
        )

    async def cancel_intent(self, intent_id, key):
        return await asyncio.to_thread(
            self.client.v1.payment_intents.cancel,
            intent_id,
            {},
            {"idempotency_key": f"agent-cancel:{key}"},
        )
