/**
 * A2P 10DLC Registration Types
 * Ported from a2p-autopilot with adaptations for CloseBot SMS
 */

// ============================================================
// INPUT TYPES — What we collect from the user
// ============================================================

export interface BusinessInfo {
  businessName: string;
  businessType: BusinessType;
  businessIndustry: BusinessIndustry;
  registrationIdentifier: RegistrationIdentifier;
  registrationNumber: string;
  websiteUrl: string;
  socialMediaUrls?: string[];
  businessIdentity: 'direct_customer' | 'isv_reseller_or_partner';
  regionsOfOperation: RegionOfOperation[];
  companyType: CompanyType;
  stockExchange?: string;
  stockTicker?: string;
  brandContactEmail?: string;
  skipAutoSecVet?: boolean;
}

export interface AuthorizedRep {
  firstName: string;
  lastName: string;
  businessTitle: string;
  jobPosition: JobPosition;
  phoneNumber: string;
  email: string;
}

export interface BusinessAddress {
  customerName: string;
  street: string;
  streetSecondary?: string;
  city: string;
  region: string;
  postalCode: string;
  isoCountry: string;
}

export interface CampaignInfo {
  useCase: CampaignUseCase;
  description: string;
  sampleMessages: string[];
  messageFlow: string;
  optInType: OptInType;
  optInMessage: string;
  optOutMessage: string;
  helpMessage: string;
  optInKeywords?: string[];
  optOutKeywords?: string[];
  helpKeywords?: string[];
  hasEmbeddedLinks: boolean;
  hasEmbeddedPhone: boolean;
}

export interface PhoneConfig {
  messagingServiceSid?: string;
  phoneNumbers?: string[];
}

export interface RegistrationInput {
  business: BusinessInfo;
  authorizedRep: AuthorizedRep;
  address: BusinessAddress;
  campaign: CampaignInfo;
  phone: PhoneConfig;
  externalId?: string;
  notifyWebhook?: string;
  notifyEmail?: string;
}

// ============================================================
// SUBMISSION STATE
// ============================================================

export type SubmissionStatus =
  | 'pending'
  | 'creating_profile'
  | 'profile_submitted'
  | 'creating_brand'
  | 'brand_pending'
  | 'brand_approved'
  | 'brand_failed'
  | 'creating_campaign'
  | 'campaign_pending'
  | 'campaign_approved'
  | 'campaign_failed'
  | 'remediation'
  | 'manual_review'
  | 'completed';

export interface SidChain {
  customerProfileSid?: string;
  businessEndUserSid?: string;
  authorizedRepEndUserSid?: string;
  addressSid?: string;
  supportingDocSid?: string;
  trustProductSid?: string;
  a2pProfileEndUserSid?: string;
  brandRegistrationSid?: string;
  messagingServiceSid?: string;
  campaignSid?: string;
}

export interface SubmissionRecord {
  id: string;
  input: RegistrationInput;
  status: SubmissionStatus;
  sidChain: SidChain;
  landingPageUrl?: string;
  privacyPolicyUrl?: string;
  termsUrl?: string;
  brandTrustScore?: number;
  failureReason?: string;
  attemptCount: number;
  maxAttempts: number;
  createdAt: string;
  updatedAt: string;
  brandSubmittedAt?: string;
  brandResolvedAt?: string;
  campaignSubmittedAt?: string;
  campaignResolvedAt?: string;
  completedAt?: string;
}

// ============================================================
// ENUMS / UNION TYPES
// ============================================================

export type CompanyType = 'public' | 'private' | 'non-profit' | 'government';

export type BusinessType =
  | 'Co-operative'
  | 'Corporation'
  | 'Limited Liability Corporation'
  | 'Non-profit Corporation'
  | 'Partnership';

export type BusinessIndustry =
  | 'AGRICULTURE' | 'AUTOMOTIVE' | 'BANKING' | 'CONSTRUCTION'
  | 'CONSUMER' | 'EDUCATION' | 'ELECTRONICS' | 'ENGINEERING'
  | 'ENERGY' | 'FAST_MOVING_CONSUMER_GOODS' | 'FINANCIAL'
  | 'FINTECH' | 'FOOD_AND_BEVERAGE' | 'GOVERNMENT' | 'HEALTHCARE'
  | 'HOSPITALITY' | 'INSURANCE' | 'JEWELRY' | 'LEGAL'
  | 'MANUFACTURING' | 'MEDIA' | 'NOT_FOR_PROFIT' | 'OIL_AND_GAS'
  | 'ONLINE' | 'PROFESSIONAL_SERVICES' | 'RAW_MATERIALS'
  | 'REAL_ESTATE' | 'RELIGION' | 'RETAIL' | 'TECHNOLOGY'
  | 'TELECOMMUNICATIONS' | 'TRANSPORTATION' | 'TRAVEL';

export type RegistrationIdentifier =
  | 'EIN' | 'DUNS' | 'CBN' | 'CN' | 'ACN' | 'CIN'
  | 'VAT' | 'VATRN' | 'RN' | 'Other';

export type RegionOfOperation =
  | 'AFRICA' | 'ASIA' | 'EUROPE' | 'LATIN_AMERICA' | 'USA_AND_CANADA';

export type JobPosition =
  | 'Director' | 'GM' | 'VP' | 'CEO' | 'CFO' | 'General Counsel' | 'Other';

export type CampaignUseCase =
  | 'ACCOUNT_NOTIFICATION' | 'CUSTOMER_CARE' | 'DELIVERY_NOTIFICATION'
  | 'FRAUD_ALERT' | 'HIGHER_EDUCATION' | 'LOW_VOLUME'
  | 'MARKETING' | 'MIXED' | 'POLITICAL' | 'POLLING_VOTING'
  | 'PUBLIC_SERVICE_ANNOUNCEMENT' | 'SECURITY_ALERT';

export type OptInType =
  | 'VERBAL' | 'WEB_FORM' | 'PAPER_FORM' | 'VIA_TEXT'
  | 'MOBILE_QR_CODE' | 'OTHER';

// ============================================================
// DISPLAY HELPERS
// ============================================================

export const BUSINESS_TYPE_OPTIONS: BusinessType[] = [
  'Co-operative',
  'Corporation',
  'Limited Liability Corporation',
  'Non-profit Corporation',
  'Partnership',
];

export const INDUSTRY_OPTIONS: BusinessIndustry[] = [
  'AGRICULTURE', 'AUTOMOTIVE', 'BANKING', 'CONSTRUCTION',
  'CONSUMER', 'EDUCATION', 'ELECTRONICS', 'ENGINEERING',
  'ENERGY', 'FAST_MOVING_CONSUMER_GOODS', 'FINANCIAL',
  'FINTECH', 'FOOD_AND_BEVERAGE', 'GOVERNMENT', 'HEALTHCARE',
  'HOSPITALITY', 'INSURANCE', 'JEWELRY', 'LEGAL',
  'MANUFACTURING', 'MEDIA', 'NOT_FOR_PROFIT', 'OIL_AND_GAS',
  'ONLINE', 'PROFESSIONAL_SERVICES', 'RAW_MATERIALS',
  'REAL_ESTATE', 'RELIGION', 'RETAIL', 'TECHNOLOGY',
  'TELECOMMUNICATIONS', 'TRANSPORTATION', 'TRAVEL',
];

export const JOB_POSITION_OPTIONS: JobPosition[] = [
  'Director', 'GM', 'VP', 'CEO', 'CFO', 'General Counsel', 'Other',
];

export const USE_CASE_OPTIONS: CampaignUseCase[] = [
  'ACCOUNT_NOTIFICATION', 'CUSTOMER_CARE', 'DELIVERY_NOTIFICATION',
  'FRAUD_ALERT', 'HIGHER_EDUCATION', 'LOW_VOLUME',
  'MARKETING', 'MIXED', 'POLITICAL', 'POLLING_VOTING',
  'PUBLIC_SERVICE_ANNOUNCEMENT', 'SECURITY_ALERT',
];

export const OPT_IN_TYPE_OPTIONS: OptInType[] = [
  'VERBAL', 'WEB_FORM', 'PAPER_FORM', 'VIA_TEXT',
  'MOBILE_QR_CODE', 'OTHER',
];

export const COMPANY_TYPE_OPTIONS: CompanyType[] = [
  'public', 'private', 'non-profit', 'government',
];

export const REGISTRATION_ID_OPTIONS: RegistrationIdentifier[] = [
  'EIN', 'DUNS', 'CBN', 'CN', 'ACN', 'CIN', 'VAT', 'VATRN', 'RN', 'Other',
];

export function formatStatus(status: SubmissionStatus): string {
  return status
    .replace(/_/g, ' ')
    .replace(/\b\w/g, (c) => c.toUpperCase());
}

export function getStatusColor(status: SubmissionStatus): string {
  if (status === 'completed' || status === 'campaign_approved' || status === 'brand_approved') return 'green';
  if (status === 'brand_failed' || status === 'campaign_failed') return 'red';
  if (status === 'pending') return 'yellow';
  if (status === 'manual_review' || status === 'remediation') return 'orange';
  if (status.startsWith('creating_')) return 'blue';
  if (status.endsWith('_pending') || status === 'profile_submitted') return 'amber';
  return 'slate';
}

export function formatIndustry(industry: BusinessIndustry): string {
  return industry
    .replace(/_/g, ' ')
    .replace(/\b\w/g, (c) => c.toUpperCase());
}

export function formatUseCase(useCase: CampaignUseCase): string {
  return useCase
    .replace(/_/g, ' ')
    .replace(/\b\w/g, (c) => c.toUpperCase());
}
