# generated by datamodel-codegen:
#   filename:  account/sync_accounts_response.json
#   timestamp: 2026-02-27T02:10:10+00:00

from __future__ import annotations

from enum import Enum
from typing import Annotated

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

from ..core import brand_ref
from ..core import context as context_1
from ..core import error
from ..core import ext as ext_1


class AccountScope(Enum):
    operator = 'operator'
    brand = 'brand'
    operator_brand = 'operator_brand'
    agent = 'agent'


class Action(Enum):
    created = 'created'
    updated = 'updated'
    unchanged = 'unchanged'
    failed = 'failed'


class Billing(Enum):
    operator = 'operator'
    agent = 'agent'


class CreditLimit(AdCPBaseModel):
    amount: Annotated[float, Field(ge=0.0)]
    currency: Annotated[str, Field(pattern='^[A-Z]{3}$')]


class Setup(AdCPBaseModel):
    model_config = ConfigDict(
        extra='allow',
    )
    expires_at: Annotated[
        AwareDatetime | None, Field(description='When this setup link expires')
    ] = None
    message: Annotated[str, Field(description="Human-readable description of what's needed")]
    url: Annotated[
        AnyUrl | None,
        Field(
            description='URL where the human can complete the required action (credit application, legal agreement, add funds)'
        ),
    ] = None


class Status(Enum):
    active = 'active'
    pending_approval = 'pending_approval'
    rejected = 'rejected'
    payment_required = 'payment_required'
    suspended = 'suspended'
    closed = 'closed'


class Account(AdCPBaseModel):
    model_config = ConfigDict(
        extra='allow',
    )
    account_scope: Annotated[
        AccountScope | None,
        Field(
            description="How the seller scoped this account. operator: shared across all brands for this operator. brand: shared across all operators for this brand. operator_brand: dedicated to this operator+brand pair. agent: the agent's default account."
        ),
    ] = None
    action: Annotated[
        Action,
        Field(
            description='Action taken for this account. created: new account provisioned. updated: existing account modified. unchanged: no changes needed. failed: could not process (see errors).'
        ),
    ]
    billing: Annotated[
        Billing | None,
        Field(description='Who is invoiced on this account. Matches the requested billing model.'),
    ] = None
    brand: Annotated[
        brand_ref.BrandReference, Field(description='Brand reference, echoed from the request')
    ]
    credit_limit: CreditLimit | None = None
    errors: Annotated[
        list[error.Error] | None,
        Field(description="Per-account errors (only present when action is 'failed')"),
    ] = None
    name: Annotated[
        str | None, Field(description='Human-readable account name assigned by the seller')
    ] = None
    operator: Annotated[str, Field(description='Operator domain, echoed from request')]
    payment_terms: Annotated[
        str | None, Field(description="Payment terms (e.g., 'net_30', 'prepay')")
    ] = None
    rate_card: Annotated[str | None, Field(description='Rate card applied to this account')] = None
    sandbox: Annotated[
        bool | None,
        Field(description='Whether this is a sandbox account, echoed from the request.'),
    ] = None
    setup: Annotated[
        Setup | None,
        Field(
            description='Setup information for pending accounts. Provides the agent (or human) with next steps to complete account activation.'
        ),
    ] = None
    status: Annotated[
        Status,
        Field(
            description='Account status. active: ready for use. pending_approval: seller reviewing (credit, legal). rejected: seller declined the account request. payment_required: credit limit reached or funds depleted. suspended: was active, now paused. closed: was active, now terminated.'
        ),
    ]
    warnings: Annotated[
        list[str] | None, Field(description='Non-fatal warnings about this account')
    ] = None


class SyncAccountsResponse1(AdCPBaseModel):
    model_config = ConfigDict(
        extra='allow',
    )
    accounts: Annotated[list[Account], Field(description='Results for each account processed')]
    context: context_1.ContextObject | None = None
    dry_run: Annotated[
        bool | None, Field(description='Whether this was a dry run (no actual changes made)')
    ] = None
    ext: ext_1.ExtensionObject | None = None


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


class SyncAccountsResponse(RootModel[SyncAccountsResponse1 | SyncAccountsResponse2]):
    root: Annotated[
        SyncAccountsResponse1 | SyncAccountsResponse2,
        Field(
            description='Response from account sync operation. Returns per-account results with status and billing, or operation-level errors on complete failure.',
            examples=[
                {
                    'data': {
                        'accounts': [
                            {
                                'account_scope': 'operator_brand',
                                'action': 'created',
                                'billing': 'operator',
                                'brand': {'brand_id': 'spark', 'domain': 'nova-brands.com'},
                                'name': 'Spark (via Pinnacle)',
                                'operator': 'pinnacle-media.com',
                                'status': 'active',
                            },
                            {
                                'account_scope': 'operator_brand',
                                'action': 'created',
                                'billing': 'operator',
                                'brand': {'brand_id': 'glow', 'domain': 'nova-brands.com'},
                                'name': 'Glow',
                                'operator': 'pinnacle-media.com',
                                'setup': {
                                    'expires_at': '2026-03-10T00:00:00Z',
                                    'message': 'Complete advertiser registration and credit application',
                                    'url': 'https://seller.example.com/advertiser-onboard',
                                },
                                'status': 'pending_approval',
                            },
                        ]
                    },
                    'description': 'Mixed results — one active, one pending approval',
                },
                {
                    'data': {
                        'accounts': [
                            {
                                'action': 'created',
                                'brand': {'brand_id': 'clearance', 'domain': 'acme-corp.com'},
                                'operator': 'acme-corp.com',
                                'status': 'rejected',
                                'warnings': [
                                    'Account request declined: advertiser category not accepted on this platform.'
                                ],
                            }
                        ]
                    },
                    'description': 'Rejected account — no account_id assigned',
                },
                {
                    'data': {
                        'accounts': [
                            {
                                'action': 'failed',
                                'brand': {'domain': 'acme-corp.com'},
                                'errors': [
                                    {
                                        'code': 'BILLING_NOT_SUPPORTED',
                                        'message': 'Operator billing is not supported. This seller only accepts agent billing.',
                                    }
                                ],
                                'operator': 'acme-corp.com',
                                'status': 'rejected',
                            }
                        ]
                    },
                    'description': 'Unsupported billing — seller rejects the request',
                },
            ],
            title='Sync Accounts Response',
        ),
    ]
