/**
 * Get Started Machine — Types
 *
 * Context, events, and guard types for the XState state machine.
 */

import type {
  AccountStatus,
  VerifyCodeResponse,
  SendVerificationCodeResponse,
} from "@customer-portal/domain/get-started";
import type { AuthResponse } from "@customer-portal/domain/auth";
import type { BilingualAddress } from "@customer-portal/domain/address";

// ============================================================================
// Form & Service Types (mirrored from store for compatibility)
// ============================================================================

export interface ServiceContext {
  type: "sim" | "internet" | "vpn" | null;
  planSku?: string | undefined;
  redirectTo?: string | undefined;
}

export type GetStartedAddress = {
  [K in keyof BilingualAddress]?: BilingualAddress[K] | undefined;
};

export interface GetStartedFormData {
  email: string;
  firstName: string;
  lastName: string;
  phone: string;
  address: GetStartedAddress;
  dateOfBirth: string;
  gender: "male" | "female" | "other" | "";
  password: string;
  confirmPassword: string;
  acceptTerms: boolean;
  marketingConsent: boolean;
}

// ============================================================================
// Machine Context
// ============================================================================

export interface GetStartedContext {
  email: string | null;
  sessionToken: string | null;
  accountStatus: AccountStatus | null;
  formData: GetStartedFormData;
  prefill: VerifyCodeResponse["prefill"] | null;
  handoffToken: string | null;
  serviceContext: ServiceContext | null;
  redirectTo: string | null;
  inline: boolean;
  error: string | null;
  codeSent: boolean;
  attemptsRemaining: number | null;
  authResponse: AuthResponse | null;
}

// ============================================================================
// Machine Events
// ============================================================================

export type GetStartedEvent =
  | { type: "START" }
  | { type: "SEND_CODE"; email: string }
  | { type: "VERIFY_CODE"; code: string }
  | { type: "COMPLETE"; formData: GetStartedFormData }
  | { type: "MIGRATE"; password?: string | undefined }
  | { type: "RESET" }
  | { type: "SET_EMAIL"; email: string; handoffToken?: string | undefined }
  | {
      type: "RESTORE_VERIFIED_SESSION";
      sessionToken: string;
      accountStatus: AccountStatus;
      prefill?: VerifyCodeResponse["prefill"] | undefined;
    }
  | { type: "UPDATE_FORM_DATA"; data: Partial<GetStartedFormData> }
  | { type: "SET_SERVICE_CONTEXT"; context: ServiceContext }
  | { type: "SET_REDIRECT"; redirectTo: string }
  | { type: "GO_BACK" };

// ============================================================================
// Actor Input Types
// ============================================================================

export interface SendCodeInput {
  email: string;
}

export interface VerifyCodeInput {
  email: string;
  code: string;
  handoffToken?: string;
}

export interface CompleteAccountInput {
  sessionToken: string;
  formData: GetStartedFormData;
  accountStatus: AccountStatus | null;
}

export interface MigrateAccountInput {
  sessionToken: string;
  password: string;
  acceptTerms: boolean;
  marketingConsent: boolean;
}

// ============================================================================
// Actor Output Types
// ============================================================================

export type SendCodeOutput = SendVerificationCodeResponse;

export type VerifyCodeOutput = VerifyCodeResponse;

export type CompleteAccountOutput = AuthResponse;

export type MigrateAccountOutput = AuthResponse;

// ============================================================================
// Machine Input (for useMachine)
// ============================================================================

export interface GetStartedMachineInput {
  inline?: boolean | undefined;
  serviceContext?: ServiceContext | undefined;
}
