import type { WaAuthCredentials, WaMobileTransportDeviceInfo } from '../types'
import type { Logger } from '../../infra/log/types'
import type { WaAuthStore } from '../../store/contracts/auth.store'
import type { WaPreKeyStore } from '../../store/contracts/pre-key.store'
import type { WaSignalStore } from '../../store/contracts/signal.store'
import type { AndroidApkMaterial } from './token'

export interface WaHttpRegistrationFlowDeps {
    readonly logger: Logger
    readonly authStore: WaAuthStore
    readonly signalStore: WaSignalStore
    readonly preKeyStore: WaPreKeyStore
    /**
     * Custom fetch implementation (defaults to global fetch)
     */
    readonly fetch?: typeof fetch
    /**
     * Base URL for registration endpoints (defaults to https://v.whatsapp.net)
     */
    readonly baseUrl?: string
    /**
     * APK material for Android registration tokens. Android has no static
     * token: the token is an HMAC over the APK's signing certificates,
     * `classes.dex` and `about_logo.png`. Without this, register as iOS.
     */
    readonly androidApkMaterial?: AndroidApkMaterial
    /**
     * Override the registration server X25519 public key (32 bytes). The
     * default is WhatsApp's long-lived registration key.
     */
    readonly registrationPublicKey?: Uint8Array
    /**
     * Override the static iOS token (consumer or business). Only needed when a
     * newer build ships a different constant.
     */
    readonly tokenStatic?: string
}

export type RegistrationMethod = 'sms' | 'voice' | 'wa_old'

export interface RequestCodeOptions {
    /** Country code (e.g., '49' without +) */
    readonly cc: string
    /** Phone number without country code */
    readonly in: string
    readonly method?: RegistrationMethod
    readonly lg?: string
    readonly lc?: string
    readonly simMcc?: string
    readonly simMnc?: string
    readonly deviceInfo?: Partial<WaMobileTransportDeviceInfo>
    /** Push name persisted on the credentials once the number is registered. */
    readonly pushName?: string
    readonly reason?: string
}

export interface RequestCodeResult {
    readonly status: 'sent' | 'fail'
    readonly reason?: string
    readonly retry_after?: number
    readonly length?: number
    readonly login?: string
    readonly raw?: unknown
}

export interface RegisterOptions {
    /** Country code */
    readonly cc: string
    /** Phone number without country code */
    readonly in: string
    readonly code: string
    readonly method?: RegistrationMethod
    readonly lg?: string
    readonly lc?: string
    readonly simMcc?: string
    readonly simMnc?: string
    readonly deviceInfo?: Partial<WaMobileTransportDeviceInfo>
    /** Push name persisted on the credentials once the number is registered. */
    readonly pushName?: string
}

export interface RegisterResult {
    readonly status: 'ok' | 'fail'
    readonly reason?: string
    readonly login?: string
    readonly pw?: string
    readonly type?: string
    readonly edge_routing_info?: string
    readonly raw?: unknown
}

export interface RegisteredMobilePrimary {
    readonly meJid: string
    readonly login: string
    readonly credentials: WaAuthCredentials
    readonly deviceInfo: WaMobileTransportDeviceInfo
}

export interface HttpRegistrationFlow {
    loadOrCreateKeys(): Promise<WaAuthCredentials>
    checkExist(options: RequestCodeOptions): Promise<any>
    requestCode(options: RequestCodeOptions): Promise<RequestCodeResult>
    register(options: RegisterOptions): Promise<RegisterResult>
    registerPhone(
        options: RequestCodeOptions & { code: string }
    ): Promise<RegisteredMobilePrimary>
}
