export type AiChatMessagePayload = Record<string, unknown>;

export type AiChatMessageDto = {
  chatPayload: AiChatMessagePayload;
  version: number;
  branchName?: string | null;
  authorEmail?: string | null;
  authorName?: string | null;
};

/**
 * Attachment reference for lease resolution.
 */
export interface AiAttachmentLeaseReference {
  /** Stable URL of the attachment */
  url: string;
  /** Media/MIME type */
  mediaType: string;
  /** Original filename */
  fileName: string;
  /** Optional storage key in object storage */
  storageKey?: string;
  /** Scope of the attachment: org-wide or app-specific */
  scopeType?: 'org' | 'app';
  /** Application ID if scopeType is "app" */
  applicationId?: string;
}

/**
 * Resolved attachment with fresh signed URL.
 */
export interface AiResolvedAttachment {
  url: string;
  signedUrl: string;
  expiresAt: string;
  mimeType: string;
}

/**
 * Request to resolve attachment leases via RPC.
 */
export interface AiResolveAttachmentLeasesRequest {
  attachments: AiAttachmentLeaseReference[];
  /** Scoped token for Bucketeer access (issued client-side). */
  scopedToken: string;
  /** Base URL of the Superblocks server, used to derive Bucketeer URL. */
  superblocksBaseUrl: string;
}

/**
 * Response with resolved attachment leases.
 */
export interface AiResolveAttachmentLeasesResponse {
  attachments: AiResolvedAttachment[];
}

/**
 * System event trigger types for AI operations without prompt_id
 */
export enum AiSystemTriggerType {
  APP_NAME = 'app_name',
  COMMIT_MESSAGE = 'commit_message',
  DASHBOARD_CHAT = 'dashboard_chat',
  FACT_METADATA = 'fact_metadata',
  PR_DESCRIPTION = 'pr_description'
}

/**
 * Attributes for AI workflow summary events (used in CloudEvents)
 * These are the core metrics tracked for AI workflows
 */
export interface AiWorkflowAttributes {
  /** Total number of tool invocations across all LLM calls */
  tool_call_count?: number;
  /** Total number of reasoning steps across all LLM calls */
  step_count?: number;
  /** Total lines of code added during workflow */
  lines_added?: number;
  /** Total lines of code edited during workflow */
  lines_edited?: number;
  /** Total lines of code removed during workflow */
  lines_removed?: number;
  /** When the workflow started (first LLM call) */
  workflow_started_at?: string;
  /** When the workflow completed (last LLM call) */
  workflow_completed_at?: string;
}

/**
 * AI workflow summary with promptId for socket protocol
 */
export interface AiWorkflowEvent {
  promptId: string;
  attributes: AiWorkflowAttributes;
}

/**
 * Per-LLM-call token usage payload used to calculate prompt cost.
 */
export interface AiPromptCostUsageEntry {
  model: string;
  provider: string;
  inputTokens: number;
  outputTokens: number;
  cachedReadTokens: number;
  cachedWriteTokens: number;
}

/**
 * Request payload for prompt cost calculation.
 */
export interface AiPromptCostRequest {
  promptId: string;
  usage: AiPromptCostUsageEntry[];
}

/**
 * Response payload for prompt cost calculation.
 */
export interface AiPromptCostResponse {
  totalCreditsUsed: number;
}

export * from './quota-paywall.js';
