/**
 * Mock data for A2P AutoPilot MCP Server
 * Used for development and testing
 */

import type {
  SubmissionRecord,
  SubmissionStatus,
  RegistrationInput,
  RemediationEntry
} from '../../src/types.js';

// Realistic Twilio SID formats
const generateSid = (prefix: string) => `${prefix}${Math.random().toString(36).substring(2, 15).padEnd(32, '0')}`;

export const mockSidChains = {
  complete: {
    customerProfileSid: generateSid('BU'),
    businessEndUserSid: generateSid('IT'),
    authorizedRepEndUserSid: generateSid('IT'),
    addressSid: generateSid('AD'),
    supportingDocSid: generateSid('RD'),
    trustProductSid: generateSid('BU'),
    a2pProfileEndUserSid: generateSid('IT'),
    brandRegistrationSid: generateSid('BN'),
    messagingServiceSid: generateSid('MG'),
    campaignSid: generateSid('QE')
  },
  brandPending: {
    customerProfileSid: generateSid('BU'),
    businessEndUserSid: generateSid('IT'),
    authorizedRepEndUserSid: generateSid('IT'),
    addressSid: generateSid('AD'),
    supportingDocSid: generateSid('RD'),
    trustProductSid: generateSid('BU'),
    a2pProfileEndUserSid: generateSid('IT'),
    brandRegistrationSid: generateSid('BN')
  },
  profileOnly: {
    customerProfileSid: generateSid('BU'),
    businessEndUserSid: generateSid('IT'),
    authorizedRepEndUserSid: generateSid('IT'),
    addressSid: generateSid('AD')
  }
};

const baseRegistrationInput: RegistrationInput = {
  business: {
    businessName: 'Acme Corporation',
    businessType: 'Corporation',
    businessIndustry: 'TECHNOLOGY',
    registrationIdentifier: 'EIN',
    registrationNumber: '12-3456789',
    websiteUrl: 'https://acme.example.com',
    businessIdentity: 'direct_customer',
    regionsOfOperation: ['USA_AND_CANADA'],
    companyType: 'private'
  },
  authorizedRep: {
    firstName: 'John',
    lastName: 'Smith',
    businessTitle: 'Chief Compliance Officer',
    jobPosition: 'CEO',
    phoneNumber: '+15551234567',
    email: 'john.smith@acme.example.com'
  },
  address: {
    customerName: 'Acme Corporation',
    street: '123 Main Street',
    city: 'San Francisco',
    region: 'CA',
    postalCode: '94102',
    isoCountry: 'US'
  },
  campaign: {
    useCase: 'ACCOUNT_NOTIFICATION',
    description: 'Account notifications and security alerts',
    sampleMessages: [
      'Your account password was changed. If this wasn\'t you, reply HELP.',
      'Security alert: New login from Chrome on Windows. Reply STOP to unsubscribe.'
    ],
    messageFlow: 'Users opt in during account creation via web form',
    optInType: 'WEB_FORM',
    optInMessage: 'Thanks! You\'re subscribed to Acme account alerts. Reply STOP to unsubscribe.',
    optOutMessage: 'You\'ve been unsubscribed from Acme alerts. Reply START to resubscribe.',
    helpMessage: 'Acme Account Alerts. For support: help@acme.example.com. Reply STOP to unsubscribe.',
    hasEmbeddedLinks: true,
    hasEmbeddedPhone: false
  },
  phone: {
    phoneNumbers: ['+15559876543']
  },
  externalId: 'acme-prod-001'
};

export const mockSubmissions: SubmissionRecord[] = [
  {
    id: 'sub_1',
    input: baseRegistrationInput,
    status: 'completed',
    sidChain: mockSidChains.complete,
    landingPageUrl: 'https://a2p.acme.example.com/landing',
    privacyPolicyUrl: 'https://a2p.acme.example.com/privacy',
    termsUrl: 'https://a2p.acme.example.com/terms',
    brandTrustScore: 75,
    remediationHistory: [],
    attemptCount: 1,
    maxAttempts: 3,
    createdAt: new Date('2025-01-15T10:00:00Z'),
    updatedAt: new Date('2025-01-16T14:30:00Z'),
    brandSubmittedAt: new Date('2025-01-15T11:00:00Z'),
    brandResolvedAt: new Date('2025-01-15T18:00:00Z'),
    campaignSubmittedAt: new Date('2025-01-16T09:00:00Z'),
    campaignResolvedAt: new Date('2025-01-16T14:30:00Z'),
    completedAt: new Date('2025-01-16T14:30:00Z')
  },
  {
    id: 'sub_2',
    input: {
      ...baseRegistrationInput,
      business: { ...baseRegistrationInput.business, businessName: 'TechStart Inc' },
      externalId: 'techstart-001'
    },
    status: 'brand_pending',
    sidChain: mockSidChains.brandPending,
    brandTrustScore: 50,
    remediationHistory: [],
    attemptCount: 1,
    maxAttempts: 3,
    createdAt: new Date('2025-02-01T08:00:00Z'),
    updatedAt: new Date('2025-02-01T10:00:00Z'),
    brandSubmittedAt: new Date('2025-02-01T10:00:00Z')
  },
  {
    id: 'sub_3',
    input: {
      ...baseRegistrationInput,
      business: { ...baseRegistrationInput.business, businessName: 'HealthCare Plus' },
      campaign: { ...baseRegistrationInput.campaign, useCase: 'DELIVERY_NOTIFICATION' },
      externalId: 'healthcare-001'
    },
    status: 'campaign_approved',
    sidChain: mockSidChains.complete,
    brandTrustScore: 85,
    remediationHistory: [],
    attemptCount: 1,
    maxAttempts: 3,
    createdAt: new Date('2025-01-28T12:00:00Z'),
    updatedAt: new Date('2025-01-30T16:00:00Z'),
    brandSubmittedAt: new Date('2025-01-28T14:00:00Z'),
    brandResolvedAt: new Date('2025-01-29T10:00:00Z'),
    campaignSubmittedAt: new Date('2025-01-29T11:00:00Z'),
    campaignResolvedAt: new Date('2025-01-30T16:00:00Z')
  },
  {
    id: 'sub_4',
    input: {
      ...baseRegistrationInput,
      business: { ...baseRegistrationInput.business, businessName: 'RetailCo' },
      externalId: 'retail-001'
    },
    status: 'brand_failed',
    sidChain: mockSidChains.brandPending,
    brandTrustScore: 25,
    failureReason: 'Business verification failed: EIN mismatch with business name',
    remediationHistory: [],
    attemptCount: 2,
    maxAttempts: 3,
    createdAt: new Date('2025-02-02T09:00:00Z'),
    updatedAt: new Date('2025-02-02T18:00:00Z'),
    brandSubmittedAt: new Date('2025-02-02T10:00:00Z'),
    brandResolvedAt: new Date('2025-02-02T18:00:00Z')
  },
  {
    id: 'sub_5',
    input: {
      ...baseRegistrationInput,
      business: { ...baseRegistrationInput.business, businessName: 'FinTech Solutions' },
      externalId: 'fintech-001'
    },
    status: 'remediation',
    sidChain: mockSidChains.brandPending,
    brandTrustScore: 40,
    failureReason: 'Missing supporting documentation',
    remediationHistory: [
      {
        timestamp: new Date('2025-02-03T10:00:00Z'),
        failureReason: 'Missing supporting documentation',
        fixApplied: 'Uploaded business license and EIN verification letter',
        fieldsChanged: {},
        resubmittedAt: new Date('2025-02-03T11:00:00Z')
      }
    ],
    attemptCount: 2,
    maxAttempts: 3,
    createdAt: new Date('2025-02-03T08:00:00Z'),
    updatedAt: new Date('2025-02-03T11:00:00Z')
  },
  {
    id: 'sub_6',
    input: {
      ...baseRegistrationInput,
      business: { ...baseRegistrationInput.business, businessName: 'EduLearn Platform' },
      campaign: { ...baseRegistrationInput.campaign, useCase: 'HIGHER_EDUCATION' },
      externalId: 'edu-001'
    },
    status: 'creating_campaign',
    sidChain: {
      ...mockSidChains.brandPending,
      messagingServiceSid: generateSid('MG')
    },
    brandTrustScore: 70,
    remediationHistory: [],
    attemptCount: 1,
    maxAttempts: 3,
    createdAt: new Date('2025-02-03T14:00:00Z'),
    updatedAt: new Date('2025-02-03T15:30:00Z'),
    brandSubmittedAt: new Date('2025-02-03T14:30:00Z'),
    brandResolvedAt: new Date('2025-02-03T15:00:00Z')
  },
  {
    id: 'sub_7',
    input: {
      ...baseRegistrationInput,
      business: { ...baseRegistrationInput.business, businessName: 'Marketing Pro' },
      campaign: { ...baseRegistrationInput.campaign, useCase: 'MARKETING' },
      externalId: 'marketing-001'
    },
    status: 'profile_submitted',
    sidChain: mockSidChains.profileOnly,
    remediationHistory: [],
    attemptCount: 1,
    maxAttempts: 3,
    createdAt: new Date('2025-02-03T16:00:00Z'),
    updatedAt: new Date('2025-02-03T16:30:00Z')
  },
  {
    id: 'sub_8',
    input: {
      ...baseRegistrationInput,
      business: { ...baseRegistrationInput.business, businessName: 'LogisticsNow' },
      campaign: { ...baseRegistrationInput.campaign, useCase: 'DELIVERY_NOTIFICATION' },
      externalId: 'logistics-001'
    },
    status: 'campaign_pending',
    sidChain: {
      ...mockSidChains.complete
    },
    brandTrustScore: 65,
    remediationHistory: [],
    attemptCount: 1,
    maxAttempts: 3,
    createdAt: new Date('2025-02-03T10:00:00Z'),
    updatedAt: new Date('2025-02-03T18:00:00Z'),
    brandSubmittedAt: new Date('2025-02-03T11:00:00Z'),
    brandResolvedAt: new Date('2025-02-03T15:00:00Z'),
    campaignSubmittedAt: new Date('2025-02-03T16:00:00Z')
  },
  {
    id: 'sub_9',
    input: {
      ...baseRegistrationInput,
      business: { ...baseRegistrationInput.business, businessName: 'Security Alert Co' },
      campaign: { ...baseRegistrationInput.campaign, useCase: 'SECURITY_ALERT' },
      externalId: 'security-001'
    },
    status: 'manual_review',
    sidChain: mockSidChains.brandPending,
    brandTrustScore: 30,
    failureReason: 'High-risk industry requires manual verification',
    remediationHistory: [
      {
        timestamp: new Date('2025-02-02T14:00:00Z'),
        failureReason: 'Suspicious business pattern detected',
        fixApplied: 'Submitted additional identity verification documents',
        fieldsChanged: {},
        resubmittedAt: new Date('2025-02-02T15:00:00Z')
      }
    ],
    attemptCount: 3,
    maxAttempts: 3,
    createdAt: new Date('2025-02-02T10:00:00Z'),
    updatedAt: new Date('2025-02-02T15:00:00Z')
  },
  {
    id: 'sub_10',
    input: {
      ...baseRegistrationInput,
      business: { ...baseRegistrationInput.business, businessName: 'CustomerCare Hub' },
      campaign: { ...baseRegistrationInput.campaign, useCase: 'CUSTOMER_CARE' },
      externalId: 'customer-care-001'
    },
    status: 'pending',
    sidChain: {},
    remediationHistory: [],
    attemptCount: 0,
    maxAttempts: 3,
    createdAt: new Date('2025-02-03T18:00:00Z'),
    updatedAt: new Date('2025-02-03T18:00:00Z')
  }
];

export const mockStats = {
  total: mockSubmissions.length,
  byStatus: {
    pending: 1,
    creating_profile: 0,
    profile_submitted: 1,
    creating_brand: 0,
    brand_pending: 1,
    brand_approved: 0,
    brand_failed: 1,
    creating_campaign: 1,
    campaign_pending: 1,
    campaign_approved: 1,
    campaign_failed: 0,
    remediation: 1,
    manual_review: 1,
    completed: 1
  },
  successRate: 20, // 2 out of 10 completed
  averageDuration: '2.5 days',
  totalAttempts: 12
};

export const mockAuditLog = [
  {
    timestamp: new Date('2025-02-03T18:00:00Z'),
    submissionId: 'sub_10',
    event: 'submission_created',
    details: 'New registration for CustomerCare Hub'
  },
  {
    timestamp: new Date('2025-02-03T16:30:00Z'),
    submissionId: 'sub_7',
    event: 'profile_submitted',
    details: 'Customer profile submitted to Twilio'
  },
  {
    timestamp: new Date('2025-02-03T16:00:00Z'),
    submissionId: 'sub_8',
    event: 'campaign_submitted',
    details: 'Campaign submitted to TCR for approval'
  },
  {
    timestamp: new Date('2025-02-03T15:30:00Z'),
    submissionId: 'sub_6',
    event: 'brand_approved',
    details: 'Brand approved by TCR with trust score 70'
  },
  {
    timestamp: new Date('2025-02-03T11:00:00Z'),
    submissionId: 'sub_5',
    event: 'remediation_attempted',
    details: 'Uploaded additional documentation'
  }
];

// Helper to get submission by ID
export function getMockSubmission(id: string): SubmissionRecord | undefined {
  return mockSubmissions.find(sub => sub.id === id);
}

// Helper to filter submissions by status
export function getMockSubmissionsByStatus(status?: SubmissionStatus): SubmissionRecord[] {
  if (!status) return mockSubmissions;
  return mockSubmissions.filter(sub => sub.status === status);
}
