/**
 * A2P AutoPilot — Shared Types
 * All modules reference these types for consistency.
 */

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

export interface BusinessInfo {
  // Business Details
  businessName: string;           // Exact legal name matching EIN
  businessType: BusinessType;
  businessIndustry: BusinessIndustry;
  registrationIdentifier: RegistrationIdentifier;
  registrationNumber: string;     // e.g. EIN "00-0000000"
  websiteUrl: string;
  socialMediaUrls?: string[];
  businessIdentity: 'direct_customer' | 'isv_reseller_or_partner';
  regionsOfOperation: RegionOfOperation[];
  companyType: 'public' | 'private' | 'non-profit' | 'government';
  stockExchange?: string;
  stockTicker?: string;
  brandContactEmail?: string;     // For 2FA (public companies)
  skipAutoSecVet?: boolean;       // true for low-volume standard
}

export interface AuthorizedRep {
  firstName: string;
  lastName: string;
  businessTitle: string;
  jobPosition: JobPosition;
  phoneNumber: string;            // E.164 format
  email: string;
}

export interface BusinessAddress {
  customerName: string;
  street: string;
  streetSecondary?: string;
  city: string;
  region: string;                 // 2-letter state/province
  postalCode: string;
  isoCountry: string;            // e.g. "US"
}

export interface CampaignInfo {
  useCase: CampaignUseCase;
  description: string;            // What messages are sent and why
  sampleMessages: string[];       // 1-5 example messages
  messageFlow: string;            // How users opt in
  optInType: OptInType;
  optInMessage: string;           // Confirmation after opt-in
  optOutMessage: string;          // Response to STOP
  helpMessage: string;            // Response to HELP
  optInKeywords?: string[];
  optOutKeywords?: string[];
  helpKeywords?: string[];
  hasEmbeddedLinks: boolean;
  hasEmbeddedPhone: boolean;
}

export interface PhoneConfig {
  messagingServiceSid?: string;   // Existing Twilio Messaging Service
  phoneNumbers?: string[];        // Phone numbers to assign
}

// Full registration input
export interface RegistrationInput {
  business: BusinessInfo;
  authorizedRep: AuthorizedRep;
  address: BusinessAddress;
  campaign: CampaignInfo;
  phone: PhoneConfig;
  // Optional: metadata
  externalId?: string;            // e.g. GHL sub-account ID
  notifyWebhook?: string;         // Webhook URL for status updates
  notifyEmail?: string;
}

// ============================================================
// SUBMISSION STATE — Tracking the multi-step Twilio flow
// ============================================================

export type SubmissionStatus =
  | 'pending'           // Not yet started
  | 'creating_profile'  // Steps 1-7: CustomerProfile
  | 'profile_submitted' // CustomerProfile submitted to Twilio
  | 'creating_brand'    // Steps 8-9: TrustProduct + BrandRegistration
  | 'brand_pending'     // Brand submitted to TCR, awaiting review
  | 'brand_approved'    // Brand approved by TCR
  | 'brand_failed'      // Brand rejected
  | 'creating_campaign' // Steps 10-12: Campaign submission
  | 'campaign_pending'  // Campaign submitted to TCR
  | 'campaign_approved' // Campaign approved — FULLY LIVE
  | 'campaign_failed'   // Campaign rejected
  | 'remediation'       // Auto-fixing and resubmitting
  | 'manual_review'     // Needs human intervention
  | 'completed';        // All done, fully registered

export interface SidChain {
  customerProfileSid?: string;      // BU...
  businessEndUserSid?: string;      // IT...
  authorizedRepEndUserSid?: string; // IT...
  addressSid?: string;              // AD...
  supportingDocSid?: string;        // RD...
  trustProductSid?: string;         // BU...
  a2pProfileEndUserSid?: string;    // IT...
  brandRegistrationSid?: string;    // BN...
  messagingServiceSid?: string;     // MG...
  campaignSid?: string;             // QE... (UsAppToPerson)
}

export interface SubmissionRecord {
  id: string;
  input: RegistrationInput;
  status: SubmissionStatus;
  sidChain: SidChain;
  landingPageUrl?: string;
  privacyPolicyUrl?: string;
  termsUrl?: string;
  // Tracking
  brandTrustScore?: number;
  failureReason?: string;
  remediationHistory: RemediationEntry[];
  attemptCount: number;
  maxAttempts: number;
  // Timestamps
  createdAt: Date;
  updatedAt: Date;
  brandSubmittedAt?: Date;
  brandResolvedAt?: Date;
  campaignSubmittedAt?: Date;
  campaignResolvedAt?: Date;
  completedAt?: Date;
}

export interface RemediationEntry {
  timestamp: Date;
  failureReason: string;
  fixApplied: string;
  fieldsChanged: Record<string, { old: string; new: string }>;
  resubmittedAt: Date;
}

// ============================================================
// LANDING PAGE TYPES
// ============================================================

export interface LandingPageConfig {
  businessName: string;
  businessSlug: string;            // URL-safe slug
  useCase: string;
  useCaseDescription: string;
  messageFrequency: string;        // e.g. "up to 5 messages per week"
  privacyPolicyUrl: string;
  termsUrl: string;
  contactEmail: string;
  contactPhone: string;
  brandColor?: string;
  logoUrl?: string;
}

// ============================================================
// NOTIFICATION TYPES
// ============================================================

export interface StatusNotification {
  submissionId: string;
  businessName: string;
  status: SubmissionStatus;
  message: string;
  details?: string;
  timestamp: Date;
}

// ============================================================
// ENUMS
// ============================================================

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';
