export type LogLevel = "info" | "warn" | "error" | "success";

export interface LogEntry {
  id: string;
  level: LogLevel;
  message: string;
  timestamp: string;
  context?: Record<string, unknown>;
}

export interface AppConfig {
  environmentName: string;
  baseUrl: string;
  tenantCode: string;
  tenantDomain: string;
  deepSeek: {
    enabled: boolean;
    baseURL: string;
    apiKey: string;
    model: string;
    reasoningEffort: "low" | "medium" | "high";
  };
  database: {
    enabled: boolean;
    host: string;
    port: number;
    database: string;
    username: string;
    password: string;
  };
  mcp: {
    enabled: boolean;
    host: string;
    port: number;
    path: string;
    authToken: string;
    allowExternal: boolean;
  };
  auth: {
    tokenPath: string;
    clientId: string;
    clientSecret: string;
    grantType: "password";
  };
  requestTimeoutMs: number;
  dryRun: boolean;
}

export interface SessionStatus {
  authenticated: boolean;
  username?: string;
  expiresAt?: string;
}

export type TaskDangerLevel = "safe" | "review" | "danger";

export interface TaskParam {
  key: string;
  label: string;
  type: "text" | "password" | "number" | "file" | "textarea";
  required?: boolean;
  placeholder?: string;
}

export interface AgentTask {
  id: string;
  title: string;
  category: "system" | "member" | "points" | "coupon" | "order" | "product" | "ops";
  description: string;
  dangerLevel: TaskDangerLevel;
  endpoint?: string;
  method?: "GET" | "POST" | "PUT" | "DELETE";
  params: TaskParam[];
  requiresAuth: boolean;
}

export interface TaskRunRequest {
  taskId: string;
  params: Record<string, string>;
}

export interface TaskRunResult {
  taskId: string;
  success: boolean;
  message: string;
  startedAt: string;
  finishedAt: string;
  statusCode?: number;
  data?: unknown;
}

export interface DashboardSnapshot {
  config: AppConfig;
  session: SessionStatus;
  taskCount: number;
  lastLogs: LogEntry[];
}

export interface McpRuntimeConfigSnapshot {
  environmentName?: string;
  baseUrl?: string;
  desktopConfigPath?: string;
  auth?: Record<string, unknown>;
  database?: Record<string, unknown>;
  deepSeek?: Record<string, unknown>;
  mcp?: Record<string, unknown>;
}

export interface LoginRequest {
  username: string;
  password: string;
  tenantCode: string;
  captchaId: string;
  captchaCode: string;
}

export interface CaptchaResult {
  captchaId: string;
  captchaBase64: string;
}

export interface AssistantChatMessage {
  role: "user" | "assistant";
  content: string;
}

export interface AssistantChatRequest {
  messages: AssistantChatMessage[];
  authenticated: boolean;
  username?: string;
  environmentName: string;
  baseUrl: string;
  availableTasks: Array<Pick<AgentTask, "id" | "title" | "category" | "description" | "dangerLevel">>;
}

export interface AssistantChatResponse {
  content: string;
  model: string;
}

export interface AssistantRunRequest {
  message: string;
  history: AssistantChatMessage[];
  authenticated: boolean;
  username?: string;
  environmentName: string;
  baseUrl: string;
}

export interface AssistantToolCallRecord {
  name: string;
  source: "mcp" | "local";
  arguments: Record<string, unknown>;
  ok: boolean;
  result?: unknown;
  error?: string;
}

export interface AssistantResultTable {
  title: string;
  columns: string[];
  rows: Record<string, unknown>[];
}

export interface AssistantRunResponse {
  content: string;
  model: string;
  usedMcp: boolean;
  toolCalls: AssistantToolCallRecord[];
  tables?: AssistantResultTable[];
  comparisonRows?: PriceComparisonRow[];
}

export interface AssistantIntentRequest {
  message: string;
  history: AssistantChatMessage[];
  authenticated: boolean;
  username?: string;
  environmentName: string;
  baseUrl: string;
  availableTasks: Array<Pick<AgentTask, "id" | "title" | "category" | "description" | "dangerLevel">>;
}

export interface AssistantIntentResponse {
  taskId?: string;
  confidence: number;
  params?: Record<string, string>;
  reason?: string;
  model: string;
}

export interface PriceComparisonRow {
  platform: string;
  productName: string;
  price: string;
  unitPrice?: string;
  status: string;
  source: string;
  note: string;
  sourceUrl?: string;
  evidence?: string;
}

export interface PriceComparisonExportRequest {
  title?: string;
  rows: PriceComparisonRow[];
}

export interface PriceComparisonExportResult {
  filePath: string;
  fileName: string;
  rowCount: number;
  createdAt: string;
}

export interface TableExportRequest {
  title?: string;
  tables: AssistantResultTable[];
}

export interface TableExportResult {
  filePath: string;
  fileName: string;
  tableCount: number;
  rowCount: number;
  createdAt: string;
}

export interface CloudResult<T = unknown> {
  ok: boolean;
  status: number;
  message: string;
  data?: T;
  raw?: unknown;
}
