"""Request/response shapes for app/routers/payments.py — the direct,
no-Task-in-between counterpart to Task Designer's "Payment — Initiate" node,
called by the Page Designer "Payment Initiator"/"Payment Callback" blocks."""

import uuid
from datetime import datetime

from pydantic import BaseModel, ConfigDict, Field


class PaymentInitiateRequest(BaseModel):
    connector_id: uuid.UUID
    amount: int = Field(gt=0)  # minor currency units, e.g. paise/cents
    currency: str = "INR"
    # The caller's own label for the payment (e.g. an application number) —
    # FlowFabric generates the order id actually sent to the gateway.
    reference: str = ""
    # Custom REST gateways only: overrides the connector's create-payment path.
    checkout_path: str | None = None
    # Where the browser goes after the gateway, with ?payment_reference=…&
    # payment_status=paid|pending|failed appended. Omitted = a simple
    # FlowFabric result page.
    return_url: str | None = None


class PaymentInitiateResult(BaseModel):
    checkout_url: str | None = None
    payment_reference: str | None = None
    transaction_id: str | None = None


class PaymentVerifyRequest(BaseModel):
    connector_id: uuid.UUID
    reference: str
    # Custom REST gateways only: overrides the connector's status-check path.
    status_path: str | None = None


class PaymentVerifyResult(BaseModel):
    # Normalised: "paid" | "pending" | "failed". provider_status is the
    # gateway's own value (e.g. Paytm "TXN_SUCCESS").
    status: str | None = None
    provider_status: str | None = None
    reference: str | None = None
    label: str | None = None
    amount: int | None = None
    currency: str | None = None
    raw: dict = {}


class PaymentProviderField(BaseModel):
    key: str
    label: str
    secret: bool = False
    required: bool = True
    placeholder: str = ""
    help: str = ""
    options: list[list[str]] | None = None
    advanced: bool = False
    multiline: bool = False


class PaymentProvider(BaseModel):
    id: str
    name: str
    tagline: str
    environments: list[str]
    docs_url: str | None = None
    fields: list[PaymentProviderField]
    notes: str = ""


class PaymentTransactionRead(BaseModel):
    model_config = ConfigDict(from_attributes=True)
    id: uuid.UUID
    connector_id: uuid.UUID
    provider: str
    reference: str
    label: str | None
    gateway_reference: str | None
    amount: int
    currency: str
    status: str
    provider_status: str | None
    created_at: datetime
    updated_at: datetime
