import type { CashDrawerImpactMode, ExecutionMethod, OrderStatus, PaymentAttemptStatus, PaymentMethod, PaymentProvider } from "../enums.js"

export interface ResolvedPaymentConfigDto {
    source: "shop_default" | "station_override"
    shopId: string
    orderId: string
    stationId: string | null
    method: PaymentMethod
    executionMethod: ExecutionMethod
    provider: PaymentProvider | null
    accountPaymentProviderId: string | null
    accountPaymentProviderLabel: string | null
    providerDeviceId: string | null
    providerDeviceLabel: string | null
    cashDrawerImpactMode: CashDrawerImpactMode
    shopPaymentMethodConfigId: string | null
    stationPaymentMethodConfigId: string | null
    configSnapshot: Record<string, unknown>
}

export interface PaymentAttemptDto {
    id: string
    orderId: string
    shopId: string
    stationId: string | null
    amount: number
    currency: string
    method: PaymentMethod
    executionMethod: ExecutionMethod | null
    provider: PaymentProvider | null
    accountPaymentProviderId: string | null
    providerDeviceId: string | null
    shopPaymentMethodConfigId: string | null
    stationPaymentMethodConfigId: string | null
    providerCheckoutId: string | null
    providerTxId: string | null
    status: PaymentAttemptStatus
    failureReason: string | null
    configSnapshot: Record<string, unknown> | null
    createdAt: string
    updatedAt: string
}

export interface StartPaymentAttemptResultDto {
    resolution: ResolvedPaymentConfigDto
    attempt: PaymentAttemptDto
    remaining: number
}

export interface PaymentExecutionOptionDto {
    executionMethod: ExecutionMethod
    label: string
    provider: PaymentProvider | null
    providerLabel: string | null
    providerDeviceId: string | null
    providerDeviceLabel: string | null
    shopPaymentMethodConfigId: string | null
    stationPaymentMethodConfigId: string | null
    source: "shop_default" | "station_override"
}

export interface AddPaymentResultDto {
    status: OrderStatus
    change: number
    appliedAmount: number
    paymentId: string
    paymentAttemptId: string
}

/**
 * Option de paiement disponible pour une commande.
 * Retournée par l'endpoint GET /orders/:id/payment-options
 */
export interface PaymentOptionDto {
    method: PaymentMethod
    label: string
    executionMethods: PaymentExecutionOptionDto[]
}

/**
 * Statut d'un terminal de paiement (reader).
 * Retourné par l'endpoint GET /sumup/reader-status/:deviceId
 */
export interface ReaderStatusDto {
    deviceId: string
    available: boolean
    status: "ready" | "busy" | "offline" | "unknown"
    reason: string | null
}

export type PaymentTerminalRealtimeStatus = "busy" | "refresh_required"

export interface PaymentTerminalStatusChangedDto {
    deviceId: string
    status: PaymentTerminalRealtimeStatus
    paymentAttemptId?: string | null
    orderId?: string | null
    stationId?: string | null
    reason?: string | null
}

export type TapToPayEnvironment = "sandbox" | "production"

export interface TapToPayInitResponseDto {
    requestId: string
    flowType: "tap_to_pay"
    orderId: string
    amount: number
    currency: string
    resolution?: ResolvedPaymentConfigDto
    sumupAccessToken: string
    accessToken?: string
    environment?: TapToPayEnvironment
    merchantId?: string
}

export interface TapToPayActivateResponseDto {
    requestId: string
    attempt: PaymentAttemptDto
    paymentAttemptId: string
    flowType: "tap_to_pay"
    orderId: string
    amount: number
    currency: string
    resolution: ResolvedPaymentConfigDto
}

export interface TapToPayCompleteResponseDto {
    status: "PAID" | "FAILED"
    paymentId?: string
    orderId: string
    reason?: string
}

export interface ManualResolvePaymentAttemptInputDto {
    status: "PAID" | "FAILED"
    reason?: string
    reference?: string
}

export interface ManualResolvePaymentAttemptResultDto {
    attemptId: string
    orderId: string
    status: "PAID" | "FAILED"
    paymentId: string | null
    failureReason: string | null
}

