import { View } from 'react-native';

import type { Id } from '@spoon/backend/convex/_generated/dataModel.js';

import { SpoonAgentSettingsForm } from './spoon-agent-settings-form';
import { SpoonMaintenanceSettingsForm } from './spoon-maintenance-settings-form';
import { SpoonRemotesPanel } from './spoon-remotes-panel';
import { SpoonSecretsPanel } from './spoon-secrets-panel';

type ReasoningEffort = 'none' | 'minimal' | 'low' | 'medium' | 'high' | 'xhigh';

export const SpoonDetailSettings = ({
  actions,
  agentSettings,
  maintenanceSettings,
  pending,
  providerProfiles,
  remotes,
  secrets,
  spoon,
}: {
  actions: {
    addRemote: (label: string, url: string) => Promise<void>;
    addSecret: (name: string, value: string) => Promise<void>;
    importSecrets: (
      secrets: { name: string; value: string }[],
    ) => Promise<void>;
    removeRemote: (remoteId: string) => Promise<void>;
    removeSecret: (secretId: string) => Promise<void>;
    updateAgent: (patch: Record<string, unknown>) => Promise<void>;
    updateMaintenance: (patch: Record<string, unknown>) => Promise<void>;
    updateSpoon: (patch: Record<string, unknown>) => Promise<void>;
  };
  agentSettings?: {
    agentModel: string;
    aiProviderProfileId?: Id<'aiProviderProfiles'>;
    autoDetectCommands?: boolean;
    branchPrefix: string;
    checkCommand?: string;
    enabled?: boolean;
    envFilePath?: string;
    installCommand?: string;
    materializeEnvFileByDefault?: boolean;
    reasoningEffort: ReasoningEffort;
    testCommand?: string;
  };
  maintenanceSettings?: {
    autoRefreshEnabled: boolean;
    autoReviewEnabled: boolean;
    autoSyncEnabled: boolean;
  };
  pending: {
    addingRemote: boolean;
    addingSecret: boolean;
    importingSecrets: boolean;
    removingRemoteId?: string;
    removingSecretId?: string;
    savingSettings: boolean;
  };
  providerProfiles: {
    _id: Id<'aiProviderProfiles'>;
    defaultModel: string;
    enabled: boolean;
    isDefault?: boolean;
    modelOptions?: string[];
    name: string;
    reasoningEffort: ReasoningEffort;
  }[];
  remotes: { _id: string; label: string; url: string }[];
  secrets: { _id: string; name: string; valuePreview?: string }[];
  spoon: {
    maintenanceMode: 'watch' | 'auto_pr' | 'paused';
    syncCadence: 'daily' | 'weekly' | 'manual';
  };
}) => (
  <View className='gap-4'>
    <SpoonMaintenanceSettingsForm
      maintenance={maintenanceSettings}
      saving={pending.savingSettings}
      spoon={spoon}
      onUpdateMaintenance={actions.updateMaintenance}
      onUpdateSpoon={actions.updateSpoon}
    />
    <SpoonAgentSettingsForm
      agent={agentSettings}
      profiles={providerProfiles}
      onUpdate={actions.updateAgent}
    />
    <SpoonSecretsPanel
      adding={pending.addingSecret}
      importing={pending.importingSecrets}
      removingId={pending.removingSecretId}
      secrets={secrets}
      onAddSecret={actions.addSecret}
      onImportSecrets={actions.importSecrets}
      onRemoveSecret={actions.removeSecret}
    />
    <SpoonRemotesPanel
      adding={pending.addingRemote}
      remotes={remotes}
      removingId={pending.removingRemoteId}
      onAddRemote={actions.addRemote}
      onRemoveRemote={actions.removeRemote}
    />
  </View>
);
