import { z } from 'zod';
import { CredentialType } from '@bubblelab/shared-schemas';
// ─── Shared Fields ───────────────────────────────────────────────────────────
const credentialsField = z
    .record(z.nativeEnum(CredentialType), z.string())
    .optional()
    .describe('Object mapping credential types to values (injected at runtime)');
const customFieldsField = z
    .array(z.object({
    slug: z.string().describe('The slug of the custom field'),
    value: z.string().optional().describe('Value for single-valued custom fields'),
    values: z
        .array(z.string())
        .optional()
        .describe('Values for multi-valued custom fields (e.g. multiselect)'),
}))
    .optional()
    .describe('Custom fields to set on the resource');
const tagsField = z
    .array(z.string())
    .optional()
    .describe('Tags to apply to the resource');
const tagsApplyModeField = z
    .enum(['append_only', 'remove_only', 'replace'])
    .optional()
    .default('replace')
    .describe('How to apply tags: append_only, remove_only, or replace (default: replace)');
const paginationLimit = z
    .number()
    .int()
    .positive()
    .optional()
    .default(25)
    .describe('Maximum number of results to return (default: 25)');
const paginationCursor = z
    .string()
    .optional()
    .describe('Cursor for pagination - pass the cursor from a previous response to get the next page');
// ─── Params Schema ───────────────────────────────────────────────────────────
export const PylonParamsSchema = z.discriminatedUnion('operation', [
    // Issues
    z.object({
        operation: z.literal('create_issue').describe('Create a new support issue'),
        title: z.string().min(1, 'Title is required').describe('Title of the issue'),
        body_html: z
            .string()
            .optional()
            .describe('HTML body content for the initial message on the issue'),
        assignee_id: z.string().optional().describe('User ID to assign the issue to'),
        priority: z
            .enum(['urgent', 'high', 'normal', 'low'])
            .optional()
            .describe('Priority level of the issue'),
        tags: tagsField,
        account_id: z
            .string()
            .optional()
            .describe('Account ID to associate the issue with'),
        requester_email: z
            .string()
            .optional()
            .describe('Email of the requester to create the issue on behalf of'),
        credentials: credentialsField,
    }),
    z.object({
        operation: z.literal('list_issues').describe('List issues with optional filtering'),
        limit: paginationLimit,
        cursor: paginationCursor,
        state: z
            .enum(['open', 'closed', 'snoozed'])
            .optional()
            .describe('Filter by issue state'),
        credentials: credentialsField,
    }),
    z.object({
        operation: z.literal('get_issue').describe('Get a specific issue by ID'),
        issue_id: z
            .string()
            .min(1, 'Issue ID is required')
            .describe('The unique identifier of the issue'),
        credentials: credentialsField,
    }),
    z.object({
        operation: z.literal('update_issue').describe('Update an existing issue'),
        issue_id: z
            .string()
            .min(1, 'Issue ID is required')
            .describe('The unique identifier of the issue to update'),
        title: z.string().optional().describe('New title for the issue'),
        state: z
            .enum(['open', 'closed', 'snoozed'])
            .optional()
            .describe('New state for the issue'),
        priority: z
            .enum(['urgent', 'high', 'normal', 'low'])
            .optional()
            .describe('New priority for the issue'),
        assignee_id: z
            .string()
            .optional()
            .describe('New assignee user ID (empty string to unassign)'),
        tags: tagsField,
        tags_apply_mode: tagsApplyModeField,
        credentials: credentialsField,
    }),
    // Messages
    z.object({
        operation: z
            .literal('send_message')
            .describe('Send a message (reply) on an issue'),
        issue_id: z
            .string()
            .min(1, 'Issue ID is required')
            .describe('The issue ID to send the message on'),
        body_html: z
            .string()
            .min(1, 'Message body is required')
            .describe('HTML content of the message'),
        is_private: z
            .boolean()
            .optional()
            .default(false)
            .describe('Whether the message is an internal note (default: false)'),
        credentials: credentialsField,
    }),
    z.object({
        operation: z
            .literal('list_messages')
            .describe('List messages on an issue'),
        issue_id: z
            .string()
            .min(1, 'Issue ID is required')
            .describe('The issue ID to list messages for'),
        limit: paginationLimit,
        cursor: paginationCursor,
        credentials: credentialsField,
    }),
    // Accounts
    z.object({
        operation: z.literal('create_account').describe('Create a new account'),
        name: z
            .string()
            .min(1, 'Account name is required')
            .describe('Name of the account'),
        owner_id: z.string().optional().describe('User ID of the account owner'),
        tags: tagsField,
        custom_fields: customFieldsField,
        credentials: credentialsField,
    }),
    z.object({
        operation: z.literal('list_accounts').describe('List accounts'),
        limit: paginationLimit,
        cursor: paginationCursor,
        credentials: credentialsField,
    }),
    z.object({
        operation: z.literal('update_account').describe('Update an existing account'),
        account_id: z
            .string()
            .min(1, 'Account ID is required')
            .describe('The unique identifier of the account to update'),
        name: z.string().optional().describe('New name for the account'),
        owner_id: z
            .string()
            .optional()
            .describe('New owner user ID (empty string to remove owner)'),
        tags: tagsField,
        tags_apply_mode: tagsApplyModeField,
        custom_fields: customFieldsField,
        credentials: credentialsField,
    }),
    // Contacts
    z.object({
        operation: z.literal('list_contacts').describe('List contacts'),
        limit: paginationLimit,
        cursor: paginationCursor,
        credentials: credentialsField,
    }),
]);
// ─── Result Schema ───────────────────────────────────────────────────────────
export const PylonResultSchema = z.discriminatedUnion('operation', [
    // Issues
    z.object({
        operation: z.literal('create_issue'),
        success: z.boolean(),
        issue_id: z.string().optional(),
        error: z.string(),
    }),
    z.object({
        operation: z.literal('list_issues'),
        success: z.boolean(),
        issues: z.array(z.record(z.unknown())).optional(),
        cursor: z.string().optional(),
        error: z.string(),
    }),
    z.object({
        operation: z.literal('get_issue'),
        success: z.boolean(),
        issue: z.record(z.unknown()).optional(),
        error: z.string(),
    }),
    z.object({
        operation: z.literal('update_issue'),
        success: z.boolean(),
        error: z.string(),
    }),
    // Messages
    z.object({
        operation: z.literal('send_message'),
        success: z.boolean(),
        message_id: z.string().optional(),
        error: z.string(),
    }),
    z.object({
        operation: z.literal('list_messages'),
        success: z.boolean(),
        messages: z.array(z.record(z.unknown())).optional(),
        cursor: z.string().optional(),
        error: z.string(),
    }),
    // Accounts
    z.object({
        operation: z.literal('create_account'),
        success: z.boolean(),
        account_id: z.string().optional(),
        error: z.string(),
    }),
    z.object({
        operation: z.literal('list_accounts'),
        success: z.boolean(),
        accounts: z.array(z.record(z.unknown())).optional(),
        cursor: z.string().optional(),
        error: z.string(),
    }),
    z.object({
        operation: z.literal('update_account'),
        success: z.boolean(),
        error: z.string(),
    }),
    // Contacts
    z.object({
        operation: z.literal('list_contacts'),
        success: z.boolean(),
        contacts: z.array(z.record(z.unknown())).optional(),
        cursor: z.string().optional(),
        error: z.string(),
    }),
]);
//# sourceMappingURL=pylon.schema.js.map