import type { CashDrawerImpactMode, PaymentMethod, PaymentProvider } from "../enums.js"
import type {
    PaymentProviderDefinitionDto,
    PaymentProviderInputMap
} from "../payment-provider.definitions.js"

export interface PaymentProviderAccountDto {
    id: string
    provider: PaymentProvider
    label: string | null
    config: PaymentProviderInputMap
    configuredSecrets: string[]
    metadata: PaymentProviderAccountMetadataDto | null
    summary: PaymentProviderAccountSummaryDto | null
    active: boolean
    createdAt: string
    updatedAt: string
}

export type PaymentProviderAccountEnvironment = "test" | "live"

export interface PaymentProviderAccountMetadataDto {
    provider: PaymentProvider
    schemaVersion: 1
    refreshedAt: string
    environment: PaymentProviderAccountEnvironment | null
    companyName: string | null
    email: string | null
    remoteMerchantCode: string | null
    website: string | null
    city: string | null
    country: string | null
}

export interface PaymentProviderAccountSummaryDto {
    title: string | null
    subtitle: string | null
    details: string[]
}

export interface PaymentProviderDeviceDto {
    id: string
    accountPaymentProviderId: string
    accountPaymentProviderLabel: string | null
    provider: PaymentProvider
    externalId: string
    label: string | null
    active: boolean
}

export interface PaymentProviderRemoteDeviceDto {
    externalId: string
    label: string | null
    model: string | null
    serialNumber: string | null
    status: string | null
}

export type PaymentProviderDeviceSyncState = "synced" | "local_only" | "remote_only"

export interface PaymentProviderDeviceCatalogItemDto {
    providerDeviceId: string | null
    accountPaymentProviderId: string
    provider: PaymentProvider
    externalId: string
    label: string | null
    remoteLabel: string | null
    displayLabel: string
    active: boolean | null
    configured: boolean
    remoteDiscovered: boolean
    syncState: PaymentProviderDeviceSyncState
    model: string | null
    serialNumber: string | null
    remoteStatus: string | null
    usageCounts: {
        defaultShopPaymentConfigs: number
        stationPaymentConfigs: number
        payments: number
        paymentAttempts: number
    }
    canDelete: boolean
    deletionBlockedReason: string | null
}

export interface PaymentProviderDeviceCatalogDto {
    accountId: string
    provider: PaymentProvider
    remoteDiscoverySupported: boolean
    remoteDiscoveryMessage: string | null
    fetchedAt: string
    items: PaymentProviderDeviceCatalogItemDto[]
}

export interface PaymentProviderDiagnosticHttpRequestDto {
    method: string
    url: string
    headers: Record<string, string>
    body: unknown
}

export interface PaymentProviderDiagnosticHttpResponseDto {
    ok: boolean | null
    status: number | null
    headers: Record<string, string>
    body: unknown
}

export interface PaymentProviderDiagnosticRawDto {
    request: PaymentProviderDiagnosticHttpRequestDto | null
    response: PaymentProviderDiagnosticHttpResponseDto | null
    notes: string[]
}

export interface PaymentProviderConnectionDiagnosticDto {
    accountId: string
    provider: PaymentProvider
    ok: boolean
    message: string
    testedAt: string
    remoteStatusCode: number | null
    merchantCode: string | null
    remoteMerchantCode: string | null
    merchantCodeStatus: "match" | "mismatch" | "unknown"
    merchantCodeNote: string | null
    configuredAccountReferenceLabel?: string | null
    configuredAccountReferenceValue?: string | null
    remoteAccountReferenceLabel?: string | null
    remoteAccountReferenceValue?: string | null
    tokenFingerprint: string | null
    accountLabel: string | null
    raw: PaymentProviderDiagnosticRawDto | null
}

export interface PaymentProviderWebhookDiagnosticDto {
    accountId: string
    provider: PaymentProvider
    ok: boolean
    message: string
    testedAt: string
    webhookUrl: string | null
    httpStatus: number | null
    responseTimeMs: number | null
    raw: PaymentProviderDiagnosticRawDto | null
}

export interface PaymentProviderWebhookSimulationDiagnosticDto {
    accountId: string
    provider: PaymentProvider
    ok: boolean
    message: string
    testedAt: string
    webhookUrl: string | null
    httpStatus: number | null
    responseTimeMs: number | null
    dryRun: boolean
    eventType: string | null
    providerCheckoutId: string | null
    webhookStatus: string | null
    webhookTransactionId: string | null
    matchedAttemptId: string | null
    matchedOrderId: string | null
    raw: PaymentProviderDiagnosticRawDto | null
}

export interface PaymentProviderTerminalDiagnosticDto {
    deviceId: string
    accountId: string
    provider: PaymentProvider
    externalId: string
    label: string | null
    active: boolean
    ok: boolean
    connected: boolean
    status: "ready" | "busy" | "offline" | "unknown"
    message: string
    testedAt: string
    providerDeviceStatus: string | null
    providerScreenState: string | null
    pendingAttemptId: string | null
    pendingOrderId: string | null
    raw: PaymentProviderDiagnosticRawDto | null
}

export interface PaymentProviderAccountOptionDto {
    id: string
    label: string | null
    provider: PaymentProvider
    active: boolean
}

export interface PaymentProviderDeviceOptionDto {
    id: string
    label: string | null
    externalId: string
    active: boolean
    providerAccountId: string
    provider: PaymentProvider
}

export interface PaymentStationOptionDto {
    id: string
    name: string
}

export interface PaymentConfigMetaDto {
    providerAccounts: PaymentProviderAccountOptionDto[]
    providerDevices: PaymentProviderDeviceOptionDto[]
    stations: PaymentStationOptionDto[]
    providerDefinitions: PaymentProviderDefinitionDto[]
}

export interface ShopPaymentMethodConfigDto {
    id: string
    shopId: string
    method: PaymentMethod
    customLabel: string | null
    displayLabel: string
    enabled: boolean
    displayOrder: number
    revision: number
    accountPaymentProviderId: string | null
    accountPaymentProviderLabel: string | null
    provider: PaymentProvider | null
    defaultProviderDeviceId: string | null
    defaultProviderDeviceLabel: string | null
    cashDrawerImpactMode: CashDrawerImpactMode
    createdAt: string
    updatedAt: string
}

export interface StationPaymentMethodConfigDto {
    id: string
    stationId: string
    stationName: string
    shopPaymentMethodConfigId: string
    method: PaymentMethod
    customLabel: string | null
    displayLabel: string
    enabled: boolean
    displayOrder: number
    isDefault: boolean
    providerDeviceId: string | null
    providerDeviceLabel: string | null
    createdAt: string
    updatedAt: string
}
