/**
 * Platform PDF Tool
 *
 * Factory function for creating a PDF render tool that generates
 * documents using pdfmake and stores them in platform storage.
 *
 * Usage:
 * ```typescript
 * const pdfTool = createPdfRenderTool({
 *   name: 'render_pdf',
 *   description: 'Render a PDF document and store it in platform storage'
 * })
 * ```
 */

import type { Tool, ToolExecutionOptions } from '../../types'
import { serviceUnavailable } from '../../types'
import { getToolServices } from '../../registry'
import {
  PdfRenderInputSchema,
  PdfRenderOutputSchema,
  type PdfRenderInput,
  type PdfRenderOutput,
  type PdfToolConfig
} from './types'

/**
 * Create a PDF render tool for workflows and agents
 *
 * PDF render tools enable workflows and agents to generate PDF documents
 * from structured data and store them in platform storage. Documents are
 * rendered using pdfmake with configurable theming.
 *
 * @param config - Configuration for the PDF tool
 * @returns Tool that renders PDFs and returns storage URLs
 *
 * @example
 * const pdfTool = createPdfRenderTool({
 *   name: 'render_proposal',
 *   description: 'Render a proposal PDF and store in acquisition bucket'
 * })
 *
 * // Execute with document structure
 * await pdfTool.execute({
 *   input: {
 *     document: {
 *       pages: [{
 *         sections: [
 *           { type: 'text', content: 'Proposal', variant: 'title' },
 *           { type: 'metric', value: '$50,000', label: 'Project Value' }
 *         ]
 *       }]
 *     },
 *     storage: {
 *       bucket: 'acquisition',
 *       path: 'proposals/deal-123/proposal.pdf'
 *     }
 *   },
 *   executionContext: context
 * })
 */
export function createPdfRenderTool(config: PdfToolConfig): Tool {
  return {
    name: config.name,
    description: config.description,
    inputSchema: PdfRenderInputSchema,
    outputSchema: PdfRenderOutputSchema,

    async execute(options: ToolExecutionOptions): Promise<PdfRenderOutput> {
      // ExecutionContext is required for multi-tenant isolation
      if (!options.executionContext) {
        throw serviceUnavailable('ExecutionContext', {
          tool: config.name,
          reason: 'organizationId required for multi-tenant isolation'
        })
      }

      // Get PDF service from global registry
      const { pdfService } = getToolServices()
      if (!pdfService) {
        throw serviceUnavailable('PdfService', {
          message: 'Ensure toolServicesRegistry.initialize() was called with pdfService in apps/api/src/main.ts',
          tool: config.name
        })
      }

      // Parse and validate input
      const input = PdfRenderInputSchema.parse(options.input) as PdfRenderInput

      // Render PDF via service
      const result = await pdfService.render({
        organizationId: options.executionContext.organizationId,
        document: input.document,
        theme: input.theme,
        storage: input.storage
      })

      return result
    }
  }
}

// Re-export types for consumers
export {
  PdfRenderInputSchema,
  PdfRenderOutputSchema,
  type PdfRenderInput,
  type PdfRenderOutput,
  type PdfToolConfig
} from './types'
