import type { ReactNode } from 'react'
import type { AccountPauseInfo } from '../../types/agentAddons'

export type BillingAddonKindKey = 'taskPack' | 'contactPack' | 'browserTaskPack' | 'advancedCaptcha'

export type BillingAddonOption = {
  priceId: string
  quantity: number
  delta: number
  unitAmount: number | null
  currency: string
  priceDisplay: string
}

export type BillingAddonContext = {
  kinds: Partial<Record<BillingAddonKindKey, { options: BillingAddonOption[] }>>
  totals: {
    amountCents: number
    currency: string
    amountDisplay: string
  }
}

export type BillingPlan = Record<string, unknown> & {
  id?: string
  name?: string
  currency?: string
  price?: number
  monthly_price?: number
}

export type DedicatedIpAssignedAgent = { id: string; name: string }

export type DedicatedIpProxy = {
  id: string
  label: string
  name: string
  staticIp: string | null
  host: string
  assignedAgents: DedicatedIpAssignedAgent[]
}

export type DedicatedIpContext = {
  allowed: boolean
  unitPrice: number
  currency: string
  multiAssign: boolean
  proxies: DedicatedIpProxy[]
}

export type BillingEndpoints = {
  updateUrl: string
  cancelSubscriptionUrl?: string
  churnKeySyncUrl?: string
  resumeSubscriptionUrl?: string
  stripePortalUrl?: string
}

export type BillingChurnKeyConfig = {
  enabled: boolean
  appId: string
  customerId: string
  subscriptionId: string
  authHash: string
  mode: 'live' | 'test'
  provider: 'stripe'
}

export type BillingTrial = {
  isTrialing: boolean
  trialEndsAtIso: string | null
}

export type BillingExtraTasksSettings = {
  enabled: boolean
  infinite: boolean
  maxTasks: number
  configuredLimit: number
  canModify: boolean
  endpoints: {
    loadUrl: string
    updateUrl: string
  }
}

export type BillingPersonalData = {
  contextType: 'personal'
  canManageBilling: boolean
  accountPause?: AccountPauseInfo | null
  paidSubscriber: boolean
  plan: BillingPlan
  trial: BillingTrial
  extraTasks: BillingExtraTasksSettings
  periodStartDate?: string | null
  periodEndDate?: string | null
  cancelAt?: string | null
  cancelAtPeriodEnd: boolean
  churnKey?: BillingChurnKeyConfig | null
  addons: BillingAddonContext
  addonsDisabled: boolean
  dedicatedIps: DedicatedIpContext
  endpoints: BillingEndpoints
}

export type BillingOrgData = {
  contextType: 'organization'
  organization: { id: string; name: string }
  canManageBilling: boolean
  accountPause?: AccountPauseInfo | null
  plan: BillingPlan
  trial: BillingTrial
  extraTasks: BillingExtraTasksSettings
  paidSubscriber: boolean
  seats: {
    purchased: number
    reserved: number
    available: number
    unitPrice: number
    currency: string
    pendingQuantity: number | null
    pendingEffectiveAtIso: string | null
    hasStripeSubscription: boolean
  }
  addons: BillingAddonContext
  addonsDisabled: boolean
  dedicatedIps: DedicatedIpContext
  endpoints: BillingEndpoints
}

export type BillingInitialData = BillingPersonalData | BillingOrgData

export type BillingScreenVariant = 'standalone' | 'embedded'

export type BillingScreenProps = {
  initialData: BillingInitialData
  variant?: BillingScreenVariant
}

export type Money = {
  amountCents: number
  currency: string
}

export type ConfirmDialogProps = {
  open: boolean
  title: string
  description?: ReactNode
  confirmLabel: string
  cancelLabel?: string
  confirmDisabled?: boolean
  icon?: ReactNode
  busy?: boolean
  danger?: boolean
  onConfirm: () => void
  onClose: () => void
  footerNote?: ReactNode
  children?: ReactNode
}

export type ToggleSwitchProps = {
  checked: boolean
  disabled?: boolean
  label: string
  description?: ReactNode
  onChange: (checked: boolean) => void
}
