# generated by datamodel-codegen:
#   filename:  account/get_account_financials_response.json
#   timestamp: 2026-02-28T17:39:50+00:00

from __future__ import annotations

from datetime import date as date_aliased
from enum import Enum
from typing import Annotated

from adcp.types.base import AdCPBaseModel
from pydantic import ConfigDict, Field, RootModel

from ..core import account_ref
from ..core import context as context_1
from ..core import date_range, error
from ..core import ext as ext_1


class LastTopUp(AdCPBaseModel):
    amount: Annotated[float, Field(description='Top-up amount', ge=0.0)]
    date: Annotated[date_aliased, Field(description='Date of top-up')]


class Balance(AdCPBaseModel):
    available: Annotated[float, Field(description='Remaining prepaid balance', ge=0.0)]
    last_top_up: Annotated[LastTopUp | None, Field(description='Most recent balance top-up')] = None


class Credit(AdCPBaseModel):
    available_credit: Annotated[
        float,
        Field(description='Remaining credit available (credit_limit minus outstanding balance)'),
    ]
    credit_limit: Annotated[float, Field(description='Maximum outstanding balance allowed', ge=0.0)]
    utilization_percent: Annotated[
        float | None,
        Field(description='Credit utilization as a percentage (0-100)', ge=0.0, le=100.0),
    ] = None


class Status(Enum):
    draft = 'draft'
    issued = 'issued'
    paid = 'paid'
    past_due = 'past_due'
    void = 'void'


class Invoice(AdCPBaseModel):
    amount: Annotated[float, Field(description='Invoice total in currency', ge=0.0)]
    due_date: Annotated[date_aliased | None, Field(description='Payment due date')] = None
    invoice_id: Annotated[str, Field(description='Seller-assigned invoice identifier')]
    paid_date: Annotated[
        date_aliased | None,
        Field(description="Date payment was received. Present when status is 'paid'."),
    ] = None
    period: Annotated[
        date_range.DateRange | None, Field(description='Billing period covered by this invoice')
    ] = None
    status: Annotated[Status, Field(description='Invoice status')]


class PaymentStatus(Enum):
    current = 'current'
    past_due = 'past_due'
    suspended = 'suspended'


class Spend(AdCPBaseModel):
    media_buy_count: Annotated[
        int | None, Field(description='Number of active media buys in the period', ge=0)
    ] = None
    total_spend: Annotated[
        float, Field(description='Total spend in the period, in currency', ge=0.0)
    ]


class GetAccountFinancialsResponse2(AdCPBaseModel):
    model_config = ConfigDict(
        extra='allow',
    )
    context: context_1.ContextObject | None = None
    errors: Annotated[list[error.Error], Field(description='Operation-level errors', min_length=1)]
    ext: ext_1.ExtensionObject | None = None


class GetAccountFinancialsResponse1(AdCPBaseModel):
    model_config = ConfigDict(
        extra='allow',
    )
    account: Annotated[
        account_ref.AccountReference,
        Field(description='Account reference, echoed from the request'),
    ]
    balance: Annotated[
        Balance | None, Field(description='Prepay balance. Present for prepay accounts.')
    ] = None
    context: context_1.ContextObject | None = None
    credit: Annotated[
        Credit | None,
        Field(
            description='Credit status. Present for credit-based accounts (payment_terms like net_30).'
        ),
    ] = None
    currency: Annotated[
        str,
        Field(
            description='ISO 4217 currency code for all monetary amounts in this response',
            pattern='^[A-Z]{3}$',
        ),
    ]
    ext: ext_1.ExtensionObject | None = None
    invoices: Annotated[
        list[Invoice] | None,
        Field(description='Recent invoices. Sellers may limit the number returned.'),
    ] = None
    payment_status: Annotated[
        PaymentStatus | None,
        Field(
            description='Overall payment status. current: all obligations met. past_due: one or more invoices overdue. suspended: account suspended due to payment issues.'
        ),
    ] = None
    payment_terms: Annotated[
        str | None, Field(description="Payment terms in effect (e.g., 'net_30', 'prepay')")
    ] = None
    period: Annotated[
        date_range.DateRange,
        Field(
            description='The actual period covered by spend data. May differ from the requested period if the seller adjusts to billing cycle boundaries.'
        ),
    ]
    spend: Annotated[Spend | None, Field(description='Spend summary for the period')] = None
    timezone: Annotated[
        str,
        Field(
            description="IANA timezone of the seller's billing day boundaries (e.g., 'America/New_York'). All dates in this response — period, invoice periods, due dates — are calendar dates in this timezone. Buyers in a different timezone should expect spend boundaries to differ from their own calendar day."
        ),
    ]


class GetAccountFinancialsResponse(
    RootModel[GetAccountFinancialsResponse1 | GetAccountFinancialsResponse2]
):
    root: Annotated[
        GetAccountFinancialsResponse1 | GetAccountFinancialsResponse2,
        Field(
            description='Financial status for an operator-billed account. Returns spend summary, credit/balance status, payment status, and invoice history. The level of detail varies by seller — only account, currency, and period are guaranteed on success.',
            examples=[
                {
                    'data': {
                        'account': {'account_id': 'acc_acme_001'},
                        'credit': {
                            'available_credit': 54770.0,
                            'credit_limit': 100000.0,
                            'utilization_percent': 45.23,
                        },
                        'currency': 'USD',
                        'invoices': [
                            {
                                'amount': 38500.0,
                                'due_date': '2026-02-28',
                                'invoice_id': 'inv_2026_01',
                                'paid_date': '2026-02-15',
                                'period': {'end': '2026-01-31', 'start': '2026-01-01'},
                                'status': 'paid',
                            }
                        ],
                        'payment_status': 'current',
                        'payment_terms': 'net_30',
                        'period': {'end': '2026-02-28', 'start': '2026-02-01'},
                        'spend': {'media_buy_count': 3, 'total_spend': 45230.0},
                        'timezone': 'America/New_York',
                    },
                    'description': 'Credit account — current, with invoice history',
                },
                {
                    'data': {
                        'account': {
                            'brand': {'domain': 'acme-corp.com'},
                            'operator': 'acme-corp.com',
                        },
                        'balance': {
                            'available': 1800.0,
                            'last_top_up': {'amount': 10000.0, 'date': '2026-02-01'},
                        },
                        'currency': 'USD',
                        'payment_status': 'current',
                        'payment_terms': 'prepay',
                        'period': {'end': '2026-02-28', 'start': '2026-02-01'},
                        'spend': {'media_buy_count': 2, 'total_spend': 8200.0},
                        'timezone': 'America/Los_Angeles',
                    },
                    'description': 'Prepay account with low balance',
                },
                {
                    'data': {
                        'errors': [
                            {
                                'code': 'UNSUPPORTED_FEATURE',
                                'message': "Financial data is not available for agent-billed accounts. The agent's own billing system is the source of truth.",
                            }
                        ]
                    },
                    'description': 'Agent-billed account — not supported',
                },
            ],
            title='Get Account Financials Response',
        ),
    ]
