export interface AiConversation {
  id: number;
  title: string;
  lockedSkillKey: string | null;
  createdAt: string;
  updatedAt: string;
  lastMessageAt: string | null;
}

export interface AiSkillTool {
  name: string;
  description: string;
}

export interface AiSkill {
  key: string;
  name: string;
  description: string;
  examples: string[];
  tools: AiSkillTool[];
}

export interface AiAttachment {
  id: number;
  name: string;
  mimeType: string;
  size: number;
  status: 'processing' | 'ready' | 'failed';
  error?: string | null;
  url: string;
  createdAt: string;
}

export interface AiFormFieldOption {
  label: string;
  value: string;
}

export interface AiFormField {
  name: string;
  label: string;
  type: 'input' | 'textarea' | 'number' | 'select' | 'date';
  required?: boolean;
  placeholder?: string;
  defaultValue?: string | number;
  options?: AiFormFieldOption[];
}

export interface AiFormSchema {
  id: string;
  title: string;
  description?: string | null;
  submitLabel?: string;
  fields: AiFormField[];
  status?: 'pending' | 'submitted';
}

export interface AiReviewColumn {
  key: string;
  title: string;
}

export interface AiReviewRow {
  [key: string]: string | number | boolean | null;
}

export type AiReviewSectionStatus = 'pending' | 'submitted' | 'failed' | 'skipped';
export type AiReviewSectionType = 'students' | 'rooms' | 'transfers' | 'checkins';

export interface AiReviewSection {
  key: string;
  type?: AiReviewSectionType;
  title: string;
  kind: 'table';
  sheet?: string;
  columns: AiReviewColumn[];
  rows: AiReviewRow[];
  issues: string[];
  status?: AiReviewSectionStatus;
  resultSummary?: string | null;
  submittedAt?: string | null;
}

export interface AiReviewSchema {
  id: string;
  title: string;
  summary?: string | null;
  sections: AiReviewSection[];
  status?: 'pending' | 'submitted' | 'expired';
  resultSummary?: string | null;
}

export interface AiChartSchema {
  id: string;
  title: string;
  chartType: 'line' | 'bar' | 'pie' | 'area' | 'scatter' | 'radar' | 'gauge' | 'funnel';
  columns: AiReviewColumn[];
  rows: AiReviewRow[];
}

export type AiToolRunStatus =
  | 'running'
  | 'success'
  | 'error'
  | 'failed'
  | 'denied'
  | 'not_found';

export interface AiToolRun {
  id?: number;
  toolCallId: string;
  toolName: string;
  skillKey?: string | null;
  status: AiToolRunStatus;
  summary?: string | null;
  argumentsSummary?: string | null;
  resultSummary?: string | null;
  durationMs?: number | null;
}

export interface AiModelRetryInfo {
  attempt: number;
  maxRetries: number;
  delayMs?: number;
  reason?: string;
}

export type AiMessageRole = 'user' | 'assistant';
export type AiMessageFeedback = 'like' | 'dislike' | null;

export interface AiChatMessage {
  id?: number | string;
  role: AiMessageRole;
  content: string;
  reasoningContent: string;
  toolRuns: AiToolRun[];
  attachments: AiAttachment[];
  forms?: AiFormSchema[];
  reviews?: AiReviewSchema[];
  charts?: AiChartSchema[];
  replyToMessageId?: number | null;
  feedback?: AiMessageFeedback;
  feedbackReason?: string | null;
  metadata?: Record<string, unknown> | null;
  retrying?: AiModelRetryInfo | null;
  error?: string;
  cancelled?: boolean;
}

export interface AiMessageRecord {
  id: number;
  role: AiMessageRole;
  content: string;
  reasoningContent: string | null;
  status: 'pending' | 'completed' | 'failed' | 'cancelled';
  errorCode: string | null;
  replyToMessageId?: number | null;
  feedback?: AiMessageFeedback;
  feedbackReason?: string | null;
  metadata?: Record<string, unknown> | null;
  attachments?: AiAttachment[];
  createdAt: string;
  toolRuns?: AiToolRun[];
}

export interface AiMessagePage {
  items: AiMessageRecord[];
  total: number;
  page: number;
  limit: number;
}

export interface AiChatInput {
  message: string;
  attachmentIds: number[];
  skillKey: string | null;
  clientRequestId: string;
  reasoningEffort?: string | null;
  localAttachments?: AiAttachment[];
  formSubmission?: {
    formId: string;
    values: Record<string, unknown>;
    formTitle?: string;
  };
  reviewSubmission?: {
    reviewId: string;
    reviewTitle?: string;
  };
  regenerateMessageId?: number;
  reloadMessage?: AiChatMessage;
}

export type AiChatMessageStatus = 'local' | 'loading' | 'updating' | 'success' | 'error' | 'abort';

export interface AiApiResponse<T> {
  success: boolean;
  data: T;
  message?: string;
}

export interface AiSseChunk {
  event?: string;
  data?: string;
  id?: string;
}
