// === Shared Types for MCPEngine Studio ===

// --- Analysis Types ---
export interface AnalysisResult {
  id: string;
  service: string;
  baseUrl: string;
  endpoints: Endpoint[];
  authFlow: AuthConfig;
  toolGroups: ToolGroup[];
  appCandidates: AppCandidate[];
  rateLimits: RateLimitInfo;
}

export interface Endpoint {
  method: 'GET' | 'POST' | 'PUT' | 'PATCH' | 'DELETE';
  path: string;
  summary: string;
  parameters: Parameter[];
  responseSchema?: object;
  tags: string[];
}

export interface Parameter {
  name: string;
  type: 'string' | 'number' | 'boolean' | 'array' | 'object';
  required: boolean;
  description: string;
  default?: unknown;
  enum?: string[];
  location: 'path' | 'query' | 'body' | 'header';
}

export interface ToolGroup {
  name: string;
  description: string;
  tools: ToolDefinition[];
}

export interface ToolDefinition {
  name: string;
  description: string;
  inputSchema: {
    type: 'object';
    properties: Record<string, SchemaProperty>;
    required: string[];
  };
  outputSchema?: object;
  annotations?: ToolAnnotations;
  endpoint: string;
  method: string;
}

export interface SchemaProperty {
  type: string;
  description: string;
  enum?: string[];
  default?: unknown;
  items?: SchemaProperty;
}

export interface ToolAnnotations {
  title?: string;
  readOnlyHint?: boolean;
  destructiveHint?: boolean;
  idempotentHint?: boolean;
  openWorldHint?: boolean;
}

// --- Auth Types ---
export interface AuthConfig {
  type: 'api_key' | 'oauth2' | 'bearer' | 'basic' | 'custom';
  keyName?: string;
  keyLocation?: 'header' | 'query';
  oauthConfig?: {
    authUrl: string;
    tokenUrl: string;
    scopes: string[];
  };
}

// --- App Types ---
export interface AppCandidate {
  name: string;
  pattern: AppPattern;
  description: string;
  dataSource: string[];  // tool names that feed this app
  suggestedWidgets: string[];
}

export type AppPattern =
  | 'dashboard' | 'data-grid' | 'form' | 'card-list'
  | 'timeline' | 'calendar' | 'kanban' | 'chart' | 'detail-view';

export interface AppBundle {
  id: string;
  name: string;
  pattern: AppPattern;
  html: string;
  toolBindings: Record<string, string>;
}

// --- Generation Types ---
export interface ServerBundle {
  files: GeneratedFile[];
  packageJson: object;
  tsConfig: object;
  entryPoint: string;
  toolCount: number;
}

export interface GeneratedFile {
  path: string;
  content: string;
  language: 'typescript' | 'json' | 'markdown';
}

// --- Testing Types ---
export type TestLayer =
  | 'protocol' | 'static' | 'visual' | 'functional'
  | 'performance' | 'security';

export interface TestResult {
  layer: TestLayer;
  passed: boolean;
  total: number;
  failures: number;
  details: TestDetail[];
  duration: number;
}

export interface TestDetail {
  name: string;
  passed: boolean;
  message?: string;
  severity?: 'error' | 'warning' | 'info';
}

// --- Deployment Types ---
export type DeployTarget = 'mcpengine' | 'npm' | 'docker' | 'cloudflare' | 'download';

export interface DeployConfig {
  target: DeployTarget;
  slug?: string;
  envVars?: Record<string, string>;
  customDomain?: string;
}

export interface DeployResult {
  id: string;
  target: DeployTarget;
  status: 'pending' | 'building' | 'live' | 'failed' | 'stopped';
  url?: string;
  endpoint?: string;
  logs: string[];
  createdAt: string;
}

// --- Rate Limits ---
export interface RateLimitInfo {
  requestsPerMinute?: number;
  requestsPerHour?: number;
  requestsPerDay?: number;
  notes?: string;
}

// --- Streaming Events ---
export type PipelineEvent =
  | { type: 'analysis:progress'; step: string; percent: number }
  | { type: 'analysis:tool_found'; tool: ToolDefinition }
  | { type: 'analysis:complete'; result: AnalysisResult }
  | { type: 'generate:progress'; file: string; percent: number }
  | { type: 'generate:file_ready'; path: string; content: string }
  | { type: 'generate:complete'; bundle: ServerBundle }
  | { type: 'design:progress'; app: string; percent: number }
  | { type: 'design:complete'; bundle: AppBundle }
  | { type: 'test:running'; layer: TestLayer }
  | { type: 'test:result'; result: TestResult }
  | { type: 'deploy:progress'; step: string; percent: number }
  | { type: 'deploy:live'; result: DeployResult }
  | { type: 'error'; message: string; recoverable: boolean };

// --- Project Types ---
export type ProjectStatus = 'draft' | 'analyzed' | 'generated' | 'tested' | 'deployed';

// --- Marketplace Types ---
export interface MarketplaceTemplate {
  id: string;
  name: string;
  slug: string;
  description: string;
  category: string;
  tags: string[];
  toolCount: number;
  appCount: number;
  forkCount: number;
  isOfficial: boolean;
  isFeatured: boolean;
  author: { name: string; avatar?: string };
  createdAt: string;
}
