export type AgentProviderKind = 'remote' | 'ollama';

export type TaskProfile =
  | 'interactiveChat'
  | 'analysis'
  | 'summarization'
  | 'toolUse'
  | 'noteEnrichment'
  | 'relationshipInference'
  | 'ghostSynthesis'
  | 'memoStructuring';

export interface AgentTaskRoute {
  provider: AgentProviderKind;
  model: string;
}

export interface ProviderConfigStatus {
  enabled: boolean;
  configured: boolean;
  baseUrl: string;
  defaultModel: string;
  hasApiKey: boolean;
}

export interface AgentProviderStatuses {
  remote: ProviderConfigStatus;
  ollama: ProviderConfigStatus;
}

export interface AgentConfigStatus {
  configured: boolean;
  providers: AgentProviderStatuses;
  taskDefaults: Record<TaskProfile, AgentTaskRoute>;
  hasSecEdgarUserAgent: boolean;
  secEdgarUserAgent: string;
}

export interface SaveRemoteProviderSettings {
  enabled: boolean;
  baseUrl: string;
  defaultModel: string;
}

export interface SaveOllamaProviderSettings {
  enabled: boolean;
  baseUrl: string;
  defaultModel: string;
}

export interface SaveAgentRuntimeConfigRequest {
  remote: SaveRemoteProviderSettings;
  ollama: SaveOllamaProviderSettings;
  taskDefaults: Record<TaskProfile, AgentTaskRoute>;
  secEdgarUserAgent: string;
}

export interface UpdateRemoteApiKeyRequest {
  apiKey: string;
}

// Runtime validators for type checking
export const isValidAgentProviderKind = (value: unknown): value is AgentProviderKind => {
  return value === 'remote' || value === 'ollama';
};

export const isValidTaskProfile = (value: unknown): value is TaskProfile => {
  const validProfiles: TaskProfile[] = [
    'interactiveChat',
    'analysis',
    'summarization',
    'toolUse',
    'noteEnrichment',
    'relationshipInference',
    'ghostSynthesis',
    'memoStructuring',
  ];
  return typeof value === 'string' && validProfiles.includes(value as TaskProfile);
};

export const isValidAgentTaskRoute = (value: unknown): value is AgentTaskRoute => {
  if (!value || typeof value !== 'object') {
    return false;
  }
  const route = value as Record<string, unknown>;
  return (
    isValidAgentProviderKind(route.provider) &&
    typeof route.model === 'string'
  );
};

export const isValidProviderConfigStatus = (value: unknown): value is ProviderConfigStatus => {
  if (!value || typeof value !== 'object') {
    return false;
  }
  const config = value as Record<string, unknown>;
  return (
    typeof config.enabled === 'boolean' &&
    typeof config.configured === 'boolean' &&
    typeof config.baseUrl === 'string' &&
    typeof config.defaultModel === 'string' &&
    typeof config.hasApiKey === 'boolean'
  );
};

export const isValidAgentProviderStatuses = (value: unknown): value is AgentProviderStatuses => {
  if (!value || typeof value !== 'object') {
    return false;
  }
  const providers = value as Record<string, unknown>;
  return Boolean(
    providers.remote &&
    providers.ollama &&
    isValidProviderConfigStatus(providers.remote) &&
    isValidProviderConfigStatus(providers.ollama)
  );
};

export const isValidAgentConfigStatus = (value: unknown): value is AgentConfigStatus => {
  if (!value || typeof value !== 'object') {
    return false;
  }

  const status = value as Record<string, unknown>;

  // Check configured
  if (typeof status.configured !== 'boolean') {
    console.error('[isValidAgentConfigStatus] Invalid configured field:', status.configured);
    return false;
  }

  // Check providers
  if (!isValidAgentProviderStatuses(status.providers)) {
    console.error('[isValidAgentConfigStatus] Invalid providers field:', status.providers);
    return false;
  }

  // Check taskDefaults
  if (!status.taskDefaults || typeof status.taskDefaults !== 'object') {
    console.error('[isValidAgentConfigStatus] Invalid taskDefaults field:', status.taskDefaults);
    return false;
  }

  // Check each task default
  const taskDefaults = status.taskDefaults as Record<string, unknown>;
  for (const [key, route] of Object.entries(taskDefaults)) {
    if (!isValidTaskProfile(key)) {
      console.error('[isValidAgentConfigStatus] Invalid task profile key:', key);
      return false;
    }
    if (!isValidAgentTaskRoute(route)) {
      console.error('[isValidAgentConfigStatus] Invalid task route for profile:', key, route);
      return false;
    }
  }

  // Check secEdgarUserAgent fields
  if (typeof status.hasSecEdgarUserAgent !== 'boolean') {
    console.error('[isValidAgentConfigStatus] Invalid hasSecEdgarUserAgent field:', status.hasSecEdgarUserAgent);
    return false;
  }

  if (typeof status.secEdgarUserAgent !== 'string') {
    console.error('[isValidAgentConfigStatus] Invalid secEdgarUserAgent field:', status.secEdgarUserAgent);
    return false;
  }

  return true;
};

export const TASK_PROFILES: TaskProfile[] = [
  'interactiveChat',
  'analysis',
  'summarization',
  'toolUse',
  'noteEnrichment',
  'relationshipInference',
  'ghostSynthesis',
  'memoStructuring',
];

export const AGENT_PROVIDER_LABELS: Record<AgentProviderKind, string> = {
  remote: 'Remote',
  ollama: 'Local (Ollama)',
};

export const TASK_LABELS: Record<TaskProfile, string> = {
  interactiveChat: 'Interactive Chat',
  analysis: 'Analysis',
  summarization: 'Summarization',
  toolUse: 'Tool Use',
  noteEnrichment: 'Note Enrichment',
  relationshipInference: 'Relationship Inference',
  ghostSynthesis: 'Ghost Synthesis',
  memoStructuring: 'Memo Structuring',
};
