/**
 * A2P AutoPilot MCP Tool Definitions
 * All tool schemas with proper inputSchema, descriptions, and UI metadata
 */

import { z } from 'zod';

// ============================================================
// TOOL VISIBILITY HELPERS
// ============================================================

export const MODEL_AND_APP_VISIBLE = undefined; // Default visibility
export const APP_ONLY_VISIBLE = ['app'] as const;

// ============================================================
// MODEL + APP VISIBLE TOOLS (Show UI)
// ============================================================

export const a2pRegistrationWizardTool = {
  name: 'a2p_registration_wizard',
  description: 'Opens the A2P registration wizard UI. Start a new registration or pre-fill from existing data.',
  inputSchema: {
    type: 'object',
    properties: {
      externalId: {
        type: 'string',
        description: 'Optional: External ID (e.g., GHL sub-account ID) to pre-fill registration data'
      }
    }
  },
  _meta: {
    ui: {
      resourceUri: 'ui://a2p/registration-wizard'
    }
  }
};

export const a2pDashboardTool = {
  name: 'a2p_dashboard',
  description: 'Opens the A2P submissions dashboard. View all registrations with filtering and stats.',
  inputSchema: {
    type: 'object',
    properties: {
      status: {
        type: 'string',
        enum: [
          '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'
        ],
        description: 'Optional: Filter submissions by status'
      }
    }
  },
  _meta: {
    ui: {
      resourceUri: 'ui://a2p/dashboard'
    }
  }
};

export const a2pViewSubmissionTool = {
  name: 'a2p_view_submission',
  description: 'Opens detailed view for a single A2P submission. Shows full status, SID chain, and history.',
  inputSchema: {
    type: 'object',
    properties: {
      submissionId: {
        type: 'string',
        description: 'The submission ID to view'
      }
    },
    required: ['submissionId']
  },
  _meta: {
    ui: {
      resourceUri: 'ui://a2p/submission-detail'
    }
  }
};

export const a2pPreviewLandingTool = {
  name: 'a2p_preview_landing',
  description: 'Preview generated landing pages (terms, privacy, opt-in) for an A2P submission.',
  inputSchema: {
    type: 'object',
    properties: {
      submissionId: {
        type: 'string',
        description: 'The submission ID to preview landing pages for'
      }
    },
    required: ['submissionId']
  },
  _meta: {
    ui: {
      resourceUri: 'ui://a2p/landing-preview'
    }
  }
};

// ============================================================
// APP-ONLY TOOLS (Backend operations)
// ============================================================

export const a2pSubmitRegistrationTool = {
  name: 'a2p_submit_registration',
  description: 'Submit a new A2P registration. Called by the wizard form on submit.',
  inputSchema: {
    type: 'object',
    properties: {
      business: {
        type: 'object',
        properties: {
          businessName: { type: 'string' },
          businessType: { type: 'string' },
          businessIndustry: { type: 'string' },
          registrationIdentifier: { type: 'string' },
          registrationNumber: { type: 'string' },
          websiteUrl: { type: 'string' },
          socialMediaUrls: { type: 'array', items: { type: 'string' } },
          businessIdentity: { type: 'string', enum: ['direct_customer', 'isv_reseller_or_partner'] },
          regionsOfOperation: { type: 'array', items: { type: 'string' } },
          companyType: { type: 'string', enum: ['public', 'private', 'non-profit', 'government'] },
          stockExchange: { type: 'string' },
          stockTicker: { type: 'string' },
          brandContactEmail: { type: 'string' },
          skipAutoSecVet: { type: 'boolean' }
        },
        required: [
          'businessName', 'businessType', 'businessIndustry',
          'registrationIdentifier', 'registrationNumber', 'websiteUrl',
          'businessIdentity', 'regionsOfOperation', 'companyType'
        ]
      },
      authorizedRep: {
        type: 'object',
        properties: {
          firstName: { type: 'string' },
          lastName: { type: 'string' },
          businessTitle: { type: 'string' },
          jobPosition: { type: 'string' },
          phoneNumber: { type: 'string' },
          email: { type: 'string' }
        },
        required: ['firstName', 'lastName', 'businessTitle', 'jobPosition', 'phoneNumber', 'email']
      },
      address: {
        type: 'object',
        properties: {
          customerName: { type: 'string' },
          street: { type: 'string' },
          streetSecondary: { type: 'string' },
          city: { type: 'string' },
          region: { type: 'string' },
          postalCode: { type: 'string' },
          isoCountry: { type: 'string' }
        },
        required: ['customerName', 'street', 'city', 'region', 'postalCode', 'isoCountry']
      },
      campaign: {
        type: 'object',
        properties: {
          useCase: { type: 'string' },
          description: { type: 'string' },
          sampleMessages: { type: 'array', items: { type: 'string' } },
          messageFlow: { type: 'string' },
          optInType: { type: 'string' },
          optInMessage: { type: 'string' },
          optOutMessage: { type: 'string' },
          helpMessage: { type: 'string' },
          optInKeywords: { type: 'array', items: { type: 'string' } },
          optOutKeywords: { type: 'array', items: { type: 'string' } },
          helpKeywords: { type: 'array', items: { type: 'string' } },
          hasEmbeddedLinks: { type: 'boolean' },
          hasEmbeddedPhone: { type: 'boolean' }
        },
        required: [
          'useCase', 'description', 'sampleMessages', 'messageFlow',
          'optInType', 'optInMessage', 'optOutMessage', 'helpMessage',
          'hasEmbeddedLinks', 'hasEmbeddedPhone'
        ]
      },
      phone: {
        type: 'object',
        properties: {
          messagingServiceSid: { type: 'string' },
          phoneNumbers: { type: 'array', items: { type: 'string' } }
        }
      },
      externalId: { type: 'string' },
      notifyWebhook: { type: 'string' },
      notifyEmail: { type: 'string' }
    },
    required: ['business', 'authorizedRep', 'address', 'campaign', 'phone']
  },
  _meta: {
    visibility: APP_ONLY_VISIBLE
  }
};

export const a2pRetrySubmissionTool = {
  name: 'a2p_retry_submission',
  description: 'Retry a failed A2P submission. Attempts to remediate and resubmit.',
  inputSchema: {
    type: 'object',
    properties: {
      submissionId: {
        type: 'string',
        description: 'The submission ID to retry'
      }
    },
    required: ['submissionId']
  },
  _meta: {
    visibility: APP_ONLY_VISIBLE
  }
};

export const a2pCancelSubmissionTool = {
  name: 'a2p_cancel_submission',
  description: 'Cancel a pending A2P submission.',
  inputSchema: {
    type: 'object',
    properties: {
      submissionId: {
        type: 'string',
        description: 'The submission ID to cancel'
      }
    },
    required: ['submissionId']
  },
  _meta: {
    visibility: APP_ONLY_VISIBLE
  }
};

export const a2pCheckStatusTool = {
  name: 'a2p_check_status',
  description: 'Refresh the status of an A2P submission. Polls Twilio/TCR for updates.',
  inputSchema: {
    type: 'object',
    properties: {
      submissionId: {
        type: 'string',
        description: 'The submission ID to check'
      }
    },
    required: ['submissionId']
  },
  _meta: {
    visibility: APP_ONLY_VISIBLE
  }
};

export const a2pGetStatsTool = {
  name: 'a2p_get_stats',
  description: 'Get dashboard statistics: submission counts by status, success rate, etc.',
  inputSchema: {
    type: 'object',
    properties: {}
  },
  _meta: {
    visibility: APP_ONLY_VISIBLE
  }
};

// ============================================================
// ALL TOOLS EXPORT
// ============================================================

export const allTools = [
  // Model + App visible (show UI)
  a2pRegistrationWizardTool,
  a2pDashboardTool,
  a2pViewSubmissionTool,
  a2pPreviewLandingTool,
  // App-only (backend operations)
  a2pSubmitRegistrationTool,
  a2pRetrySubmissionTool,
  a2pCancelSubmissionTool,
  a2pCheckStatusTool,
  a2pGetStatsTool
];
