// Freebit API Type Definitions (cleaned)

export interface FreebitAuthRequest {
  oemId: string; // 4-char alphanumeric ISP identifier
  oemKey: string; // 32-char auth key
}

export interface FreebitAuthResponse {
  resultCode: string;
  status: {
    message: string;
    statusCode: string | number;
  };
  authKey: string; // Token for subsequent API calls
}

export interface FreebitAccountDetailsRequest {
  authKey: string;
  version?: string | number; // Docs recommend "2"
  requestDatas: Array<{
    kind: "MASTER" | "MVNO";
    account?: string | number;
  }>;
}

export interface FreebitAccountDetail {
  kind: "MASTER" | "MVNO";
  account: string | number;
  state: "active" | "suspended" | "temporary" | "waiting" | "obsolete";
  status?: "active" | "suspended" | "temporary" | "waiting" | "obsolete";
  startDate?: string | number;
  relationCode?: string;
  resultCode?: string | number;
  planCode?: string;
  planName?: string;
  iccid?: string | number;
  imsi?: string | number;
  eid?: string;
  contractLine?: string;
  size?: "standard" | "nano" | "micro" | "esim";
  simSize?: "standard" | "nano" | "micro" | "esim";
  msisdn?: string | number;
  sms?: number; // 10=active, 20=inactive
  talk?: number; // 10=active, 20=inactive
  ipv4?: string;
  ipv6?: string;
  quota?: number; // Remaining quota
  remainingQuotaMb?: string | number | null;
  remainingQuotaKb?: string | number | null;
  voicemail?: "10" | "20" | number | null;
  voiceMail?: "10" | "20" | number | null;
  callwaiting?: "10" | "20" | number | null;
  callWaiting?: "10" | "20" | number | null;
  worldwing?: "10" | "20" | number | null;
  worldWing?: "10" | "20" | number | null;
  networkType?: string;
  async?: { func: string; date: string | number };
}

export interface FreebitAccountDetailsResponse {
  resultCode: string;
  status: {
    message: string;
    statusCode: string | number;
  };
  masterAccount?: string;
  responseDatas: FreebitAccountDetail[];
}

export interface FreebitTrafficInfoRequest {
  authKey: string;
  account: string;
}

export interface FreebitTrafficInfoResponse {
  resultCode: string;
  status: {
    message: string;
    statusCode: string | number;
  };
  account: string;
  traffic: {
    today: string; // Today's usage in KB
    inRecentDays: string; // Comma-separated recent days usage
    blackList: string; // 10=blacklisted, 20=not blacklisted
  };
}

export interface FreebitTopUpRequest {
  authKey: string;
  account: string;
  quota: number; // KB units (e.g., 102400 for 100MB)
  quotaCode?: string; // Campaign code
  expire?: string; // YYYYMMDD format (8 digits)
  runTime?: string; // Scheduled execution time (YYYYMMDD format, 8 digits)
}

export interface FreebitTopUpResponse {
  resultCode: string;
  status: { message: string; statusCode: string | number };
}

// AddSpec request for updating SIM options/features immediately
export interface FreebitAddSpecRequest {
  authKey: string;
  account: string;
  kind?: string; // e.g. 'MVNO'
  // Feature flags: 10 = enabled, 20 = disabled
  voiceMail?: "10" | "20";
  voicemail?: "10" | "20";
  callWaiting?: "10" | "20";
  callwaiting?: "10" | "20";
  worldWing?: "10" | "20";
  worldwing?: "10" | "20";
  contractLine?: string; // '4G' or '5G'
}

export interface FreebitVoiceOptionSettings {
  voiceMail?: "10" | "20";
  callWaiting?: "10" | "20";
  callTransfer?: "10" | "20";
  callTransferWorld?: "10" | "20";
  callTransferNoId?: "10" | "20";
  worldCall?: "10" | "20";
  worldCallCreditLimit?: string;
  worldWing?: "10" | "20";
  worldWingCreditLimit?: string;
}

export interface FreebitVoiceOptionRequest {
  authKey: string;
  account: string;
  userConfirmed?: "10" | "20";
  aladinOperated?: "10" | "20";
  talkOption: FreebitVoiceOptionSettings;
}

export interface FreebitVoiceOptionResponse {
  resultCode: string;
  status: { message: string; statusCode: string | number };
}

export interface FreebitAddSpecResponse {
  resultCode: string;
  status: { message: string; statusCode: string | number };
}

export interface FreebitQuotaHistoryRequest {
  authKey: string;
  account: string;
  fromDate: string;
  toDate: string;
}

export interface FreebitQuotaHistoryItem {
  quota: string; // KB as string
  date: string;
  expire: string;
  quotaCode: string;
}

export interface FreebitQuotaHistoryResponse {
  resultCode: string;
  status: { message: string; statusCode: string | number };
  total: string | number;
  count: string | number;
  quotaHistory: FreebitQuotaHistoryItem[];
}

export interface FreebitPlanChangeRequest {
  authKey: string;
  account: string;
  planCode: string; // Note: API expects camelCase "planCode" not "plancode"
  globalip?: "0" | "1"; // 0=disabled, 1=assign global IP (PA05-21 expects legacy flags)
  runTime?: string; // YYYYMMDD format (8 digits, date only) - optional
  contractLine?: "4G" | "5G"; // Network type for contract line changes
}

export interface FreebitPlanChangeResponse {
  resultCode: string;
  status: { message: string; statusCode: string | number };
  ipv4?: string;
  ipv6?: string;
}

export interface FreebitPlanChangePayload {
  requestDatas: Array<{
    kind: "MVNO";
    account: string;
    newPlanCode: string;
    assignGlobalIp: boolean;
    scheduledAt?: string;
  }>;
}

export interface FreebitAddSpecPayload {
  requestDatas: Array<{
    kind: "MVNO";
    account: string;
    specCode: string;
    enabled?: boolean;
    networkType?: "4G" | "5G";
  }>;
}

export interface FreebitCancelPlanPayload {
  requestDatas: Array<{
    kind: "MVNO";
    account: string;
    runDate: string;
  }>;
}

export interface FreebitEsimReissuePayload {
  requestDatas: Array<{
    kind: "MVNO";
    account: string;
    newEid: string;
    oldEid?: string;
    planCode?: string;
  }>;
}

export interface FreebitContractLineChangeRequest {
  authKey: string;
  account: string;
  contractLine: "4G" | "5G";
  productNumber?: string;
  eid?: string;
}

export interface FreebitContractLineChangeResponse {
  resultCode: string | number;
  status?: { message?: string; statusCode?: string | number };
  statusCode?: string | number;
  message?: string;
}

export interface FreebitCancelPlanRequest {
  authKey: string;
  account: string;
  runTime?: string; // YYYYMMDD - optional
}

export interface FreebitCancelPlanResponse {
  resultCode: string;
  status: { message: string; statusCode: string | number };
}

// PA02-04: Account Cancellation (master/cnclAcnt)
export interface FreebitCancelAccountRequest {
  authKey: string;
  kind: string; // e.g., 'MVNO'
  account: string;
  runDate?: string; // YYYYMMDD
}

export interface FreebitCancelAccountResponse {
  resultCode: string;
  status: { message: string; statusCode: string | number };
}

export interface FreebitEsimReissueRequest {
  authKey: string;
  requestDatas: Array<{
    kind: "MVNO";
    account: string;
    newEid?: string;
    oldEid?: string;
    planCode?: string;
  }>;
}

export interface FreebitEsimReissueResponse {
  resultCode: string;
  status: { message: string; statusCode: string | number };
}

export interface FreebitEsimAddAccountRequest {
  authKey: string;
  aladinOperated: string; // '10' for issue, '20' for no-issue
  account: string;
  eid: string;
  addKind: "N" | "R"; // N = new, R = reissue
  shipDate?: string;
  planCode?: string;
  contractLine?: string;
  mnp?: {
    reserveNumber: string;
    reserveExpireDate: string;
  };
}

export interface FreebitEsimAddAccountResponse {
  resultCode: string;
  status: { message: string; statusCode: string | number };
}

// PA05-41 eSIM Account Activation (addAcct)
// Based on Freebit API specification - all parameters from JSON table
export interface FreebitEsimAccountActivationRequest {
  authKey: string;                    // Row 1: 認証キー (Required)
  aladinOperated: string;             // Row 2: ALADIN帳票作成フラグ ('10':操作済, '20':未操作) (Required)
  masterAccount?: string;             // Row 3: マスタアカウント (Conditional - for service provider)
  masterPassword?: string;            // Row 4: マスタパスワード (Conditional - for service provider)
  createType: string;                 // Row 5: 登録区分 ('new', 'reissue', 'add') (Required)
  eid?: string;                       // Row 6: eSIM識別番号 (Conditional - required for reissue/exchange)
  account: string;                    // Row 7: アカウント/MSISDN (Required)
  simkind: string;                    // Row 8: SIM種別 (Conditional - Required except when addKind='R')
                                      //        eSIM: 'E0':音声あり, 'E2':SMSなし, 'E3':SMSあり
                                      //        Physical: '3MS', '3MR', etc
  contractLine?: string;              // Row 9: 契約回線種別 ('4G', '5G') (Conditional)
  repAccount?: string;                // Row 10: 代表番号 (Conditional)
  addKind?: string;                   // Row 11: 開通種別 ('N':新規, 'M':MNP転入, 'R':再発行) (Required)
  reissue?: string;                   // Row 12: 再発行情報 (Conditional)
  oldProductNumber?: string;          // Row 13: 元製造番号 (Conditional - for exchange)
  oldEid?: string;                    // Row 14: 元eSIM識別番号 (Conditional - for exchange)
  mnp?: {                             // Row 15: MNP情報 (Conditional)
    reserveNumber: string;            // Row 16: MNP予約番号 (Conditional)
    reserveExpireDate?: string;       // (Conditional) YYYYMMDD
  };
  firstnameKanji?: string;            // Row 17: 由字(漢字) (Conditional)
  lastnameKanji?: string;             // Row 18: 名前(漢字) (Conditional) 
  firstnameZenKana?: string;          // Row 19: 由字(全角カタカナ) (Conditional)
  lastnameZenKana?: string;           // Row 20: 名前(全角カタカナ) (Conditional)
  gender?: string;                    // Row 21: 性別 ('M', 'F') (Required for identification)
  birthday?: string;                  // Row 22: 生年月日 YYYYMMDD (Conditional)
  shipDate?: string;                  // Row 23: 出荷日 YYYYMMDD (Conditional)
  planCode?: string;                  // Row 24: プランコード (Max 32 chars) (Conditional)
  deliveryCode?: string;              // Row 25: 顧客コード (Max 10 chars) (Conditional - OEM specific)
  globalIp?: string;                  // Additional: グローバルIP ('10': なし, '20': あり)
  size?: string;                      // SIM physical size (for physical SIMs)
}

export interface FreebitEsimAccountActivationResponse {
  resultCode: string;
  status?: {
    message?: string;
    statusCode?: string | number;
  };
  statusCode?: string | number;
  message?: string;
}

// Portal-specific types for SIM management
export interface SimDetails {
  account: string;
  status: "active" | "suspended" | "cancelled" | "pending";
  planCode: string;
  planName: string;
  simType: "standard" | "nano" | "micro" | "esim";
  iccid: string;
  eid: string;
  msisdn: string;
  imsi: string;
  remainingQuotaMb: number;
  remainingQuotaKb: number;
  voiceMailEnabled: boolean;
  callWaitingEnabled: boolean;
  internationalRoamingEnabled: boolean;
  networkType: string;
  activatedAt?: string;
  expiresAt?: string;
  ipv4?: string;
  ipv6?: string;
  startDate?: string;
  hasVoice?: boolean;
  hasSms?: boolean;
}

export interface SimUsage {
  account: string;
  todayUsageMb: number;
  todayUsageKb: number;
  monthlyUsageMb?: number;
  monthlyUsageKb?: number;
  recentDaysUsage: Array<{ date: string; usageKb: number; usageMb: number }>;
  isBlacklisted: boolean;
  lastUpdated?: string;
}

export interface SimTopUpHistory {
  account: string;
  totalAdditions: number;
  additionCount: number;
  history: Array<{
    quotaKb: number;
    quotaMb: number;
    addedDate: string;
    expiryDate: string;
    campaignCode: string;
  }>;
}

// Error handling
export interface FreebitError extends Error {
  resultCode: string;
  statusCode: string | number;
  freebititMessage: string;
}

// Configuration
export interface FreebitConfig {
  baseUrl: string;
  oemId: string;
  oemKey: string;
  timeout: number;
  retryAttempts: number;
  detailsEndpoint?: string;
}
