export interface SetupWizardState {
  completed: boolean;
  lastShownVersion: string;
  stepStatus: {
    [stepId: string]: {
      completed: boolean;
      lastChecked: number;
      data?: Record<string, any>;
    };
  };
  dismissedAt?: number;
}

export interface CareerSetupState {
  completed: boolean;
  lastShownVersion: string;
  stepStatus: {
    [stepId: string]: {
      completed: boolean;
      lastChecked: number;
      data?: Record<string, any>;
    };
  };
  dismissedAt?: number;
  // Career-specific data
  baseCodeDirectory?: string;
  canvasHorizonPath?: string;
  journeyPath?: string;
}

export interface LearnerHubSetupState {
  completed: boolean;
  lastShownVersion: string;
  stepStatus: {
    [stepId: string]: {
      completed: boolean;
      lastChecked: number;
      data?: Record<string, any>;
    };
  };
  dismissedAt?: number;
  // Learner Hub-specific data
  baseDirectory?: string;
  learnerHubPath?: string;
}

export interface Settings {
  openShortcut: string;
  autoUpdate: boolean;
  githubToken: string;
  theme: 'light' | 'dark' | 'system';
  onboarding: SetupWizardState;
  careerSetup: CareerSetupState;
  multiDeploy: boolean;
}

export interface DockerVersion {
  version: string;
  buildNumber: string;
  downloadUrl: string;
  isLatest: boolean;
}

export interface DockerSettings {
  cpus: number;
  cpusRecommended: number;
  memoryMiB: number;
  memoryRecommended: number;
  swapMiB: number;
  swapRecommended: number;
  diskSizeMiB: number;
  diskSizeRecommended: number;
}

export interface DockerStatus {
  installed: boolean;
  version: string | null;
  dockerDesktopVersion: string | null;
  updateAvailable: boolean;
  latestVersion: string | null;
  daemonRunning: boolean;
  composeInstalled: boolean;
  composeVersion: string | null;
  settings?: DockerSettings;
}

export interface HomebrewStatus {
  installed: boolean;
  version: string | null;
  path: string | null;
  updateAvailable: boolean;
  latestVersion: string | null;
}

export interface FrpcStatus {
  installed: boolean;
  version: string | null;
  path: string | null;
  updateAvailable: boolean;
  latestVersion: string | null;
}

export interface FrpcConfigStatus {
  serviceFileExists: boolean;
  configFileExists: boolean;
  isRunning: boolean;
  configured: boolean;
  canvasUrl: string | null;
  folderName: string | null;
  frpcUsername: string | null;
}

export interface GitCloneStatus {
  configured: boolean;
  repositoryPath: string | null;
  exists: boolean;
  isValidRepo: boolean;
  isCanvasLmsRepo: boolean;
  remoteUrl: string | null;
  currentBranch: string | null;
  lastCommit: {
    hash: string;
    author: string;
    date: string;
    message: string;
  } | null;
  cloneFolder?: string | null;
  folderName?: string;
}

export interface SystemStatus {
  docker: DockerStatus;
  homebrew: HomebrewStatus;
}

export interface InstallResult {
  success: boolean;
  message: string;
}

export interface StepValidationResult {
  isComplete: boolean;
  canProceed: boolean;
  isLoading?: boolean;
  hasWarnings?: boolean;
  message?: string;
  details?: Record<string, any>;
}

export interface SetupWizardStepConfig {
  id: string;
  title: string;
  description: string;
  componentName: string;
  order: number;
  required: boolean;
  icon?: string;
}

export interface ArtifactoryStatus {
  configured: boolean;
  gemrcExists: boolean;
  npmrcConfigured: boolean;
  username?: string;
  token?: string;
  credentialsValid?: boolean;
  credentialsValidationError?: string;
  details?: any;
}

export interface ArtifactorySetupOptions {
  username: string;
  token: string;
  overwrite?: boolean;
}

export interface InstCliStatus {
  imageExists: boolean;
  imageId?: string;
  imageCreated?: string;
  versions?: {
    inst?: string;
    instCliQuizzes?: string;
    ruby?: string;
    pnpm?: string;
    instFunctions?: string;
    claude?: string;
  };
  latestVersions?: {
    inst?: string;
    instCliQuizzes?: string;
    claude?: string;
  };
  updateAvailable?: boolean;
  instUpdateAvailable?: boolean;
  instCliQuizzesUpdateAvailable?: boolean;
  claudeUpdateAvailable?: boolean;
}

export interface GitConfigStatus {
  gitInstalled: boolean;
  gitconfigExists: boolean;
  name?: string;
  email?: string;
  version?: string;
  path?: string;
}

export interface GitConfigOptions {
  name: string;
  email: string;
}

export interface SshKeyStatus {
  keysExist: boolean;
  publicKeyPath?: string;
  privateKeyPath?: string;
  publicKeyContent?: string;
  valid: boolean;
  configExists?: boolean;
  configCorrect?: boolean;
  hasGitHub?: boolean;
  hasGerrit?: boolean;
  githubCorrect?: boolean;
  gerritCorrect?: boolean;
}

export interface SshKeyGenerateOptions {
  filename: string;
  passphrase: string;
}

export interface InstConfigStatus {
  exists: boolean;
  content: string | null;
  path: string;
  actualFileContent?: string | null;
}

export interface InstCliProxyStatus {
  instDomain: string | null;
  resolverFileExists: boolean;
  resolverFilePath: string | null;
  dockerComposeExists: boolean;
  dockerComposePath: string;
  traefikRunning: boolean;
  traefikContainerCount: number;
  configured: boolean;
}

export interface BuildRunStatus {
  instDomain: string | null;
  canvasUrl: string | null;
  siteAccessible: boolean;
  configured: boolean;
  folderName: string | null;
}

export interface ActionEntry {
  id: string;
  url: string;
  timestamp: number;
  logs: string[];
  status: 'pending' | 'running' | 'completed' | 'failed';
}

export interface SettingsAPI {
  getSettings: () => Promise<Settings>;
  setSetting: <K extends keyof Settings>(key: K, value: Settings[K]) => Promise<void>;
  getVersion: () => Promise<string>;
  getSystemStatus: () => Promise<SystemStatus>;
  checkDockerStatus: () => Promise<DockerStatus>;
  installDocker: () => Promise<InstallResult>;
  updateDocker: () => Promise<InstallResult>;
  reinstallDocker: () => Promise<InstallResult>;
  onReinstallOutput: (callback: (output: string) => void) => () => void;
  getSetupWizardState: () => Promise<SetupWizardState>;
}

export interface ActionsAPI {
  getAll: () => Promise<ActionEntry[]>;
  get: (id: string) => Promise<ActionEntry | undefined>;
  delete: (id: string) => Promise<void>;
  clear: () => Promise<void>;
  onActionAdded: (callback: (action: ActionEntry) => void) => () => void;
}

export interface SetupWizardAPI {
  getState: () => Promise<SetupWizardState>;
  setState: (state: SetupWizardState) => Promise<void>;
  getSteps: () => Promise<SetupWizardStepConfig[]>;
  validateStep: (stepId: string) => Promise<StepValidationResult>;
  getArtifactoryStatus: () => Promise<ArtifactoryStatus>;
  setupArtifactory: (options: ArtifactorySetupOptions) => Promise<InstallResult>;
  getDockerStatus: () => Promise<DockerStatus>;
  installDocker: () => Promise<InstallResult>;
  updateDocker: () => Promise<InstallResult>;
  reinstallDocker: () => Promise<InstallResult>;
  onReinstallOutput: (callback: (output: string) => void) => () => void;
  getInstCliStatus: () => Promise<InstCliStatus>;
  buildInstCli: () => Promise<InstallResult>;
  onBuildOutput: (callback: (output: string) => void) => () => void;
  getGitConfigStatus: () => Promise<GitConfigStatus>;
  updateGitConfig: (options: GitConfigOptions) => Promise<InstallResult>;
  getSshKeyStatus: (publicKeyPath?: string) => Promise<SshKeyStatus>;
  generateSshKeys: (options: SshKeyGenerateOptions) => Promise<InstallResult>;
  selectPublicKeyFile: () => Promise<{ filePath: string } | null>;
  readPublicKey: (publicKeyPath: string) => Promise<{ success: boolean; content?: string; message?: string }>;
  getInstConfigStatus: () => Promise<InstConfigStatus>;
  saveInstConfig: (content: string) => Promise<InstallResult>;
  getInstCliProxyStatus: () => Promise<InstCliProxyStatus>;
  createDockerComposeOverride: () => Promise<InstallResult>;
  startProxy: () => Promise<InstallResult>;
  stopProxy: () => Promise<InstallResult>;
  onStartProxyOutput: (callback: (output: string) => void) => () => void;
  onStopProxyOutput: (callback: (output: string) => void) => () => void;
  getMultiDeploy: () => Promise<boolean>;
  setMultiDeploy: (value: boolean) => Promise<void>;
  getBuildRunStatus: (multiDeploy?: boolean) => Promise<BuildRunStatus>;
  buildAndRun: (multiDeploy?: boolean) => Promise<InstallResult>;
  cleanBuildAndRun: (multiDeploy?: boolean) => Promise<InstallResult>;
  updateTrayIcon: () => Promise<void>;
  onBuildRunOutput: (callback: (output: string) => void) => () => void;
}

export interface CareerRepoStatus {
  exists: boolean;
  isValidRepo: boolean;
  remoteUrl: string | null;
  currentBranch: string | null;
  lastCommit: {
    hash: string;
    author: string;
    date: string;
    message: string;
  } | null;
}

export interface CareerRepositoriesStatus {
  baseDirectory: string | null;
  canvasHorizon: CareerRepoStatus;
  journey: CareerRepoStatus;
  configExists: boolean;
  configured: boolean;
}

export interface CareerWizardAPI {
  getState: () => Promise<CareerSetupState>;
  setState: (state: CareerSetupState) => Promise<void>;
  getSteps: () => Promise<SetupWizardStepConfig[]>;
  validateStep: (stepId: string) => Promise<StepValidationResult>;
  cloneRepositories: (baseDirectory: string) => Promise<InstallResult>;
  onCloneOutput: (callback: (output: string) => void) => () => void;
  getRepositoriesStatus: (baseDirectory?: string) => Promise<CareerRepositoriesStatus>;
  resetRepositories: (baseDirectory: string) => Promise<InstallResult>;
  onResetOutput: (callback: (output: string) => void) => () => void;
  getConfig: () => Promise<{ exists: boolean; path: string; content?: string; baseDirectory?: string }>;
  createConfig: (config: any) => Promise<InstallResult>;
  selectCloneFolder: () => Promise<string | null>;
  saveCloneFolder: (folder: string) => Promise<void>;
  openCanvasSetup: () => Promise<void>;
  updateTrayIcon: () => Promise<void>;
  getTheme: () => Promise<string>;
  onThemeChange: (callback: (theme: string) => void) => () => void;
}

export interface LearnerHubRepoStatus {
  exists: boolean;
  isValidRepo: boolean;
  remoteUrl: string | null;
  currentBranch: string | null;
  lastCommit: {
    hash: string;
    author: string;
    date: string;
    message: string;
  } | null;
}

export interface LearnerHubRepositoriesStatus {
  baseDirectory: string | null;
  learnerHub: LearnerHubRepoStatus;
  configExists: boolean;
  configured: boolean;
}

export interface LearnerHubWizardAPI {
  getState: () => Promise<LearnerHubSetupState>;
  setState: (state: LearnerHubSetupState) => Promise<void>;
  getSteps: () => Promise<SetupWizardStepConfig[]>;
  validateStep: (stepId: string) => Promise<StepValidationResult>;
  cloneRepository: (baseDirectory: string) => Promise<InstallResult>;
  onCloneOutput: (callback: (output: string) => void) => () => void;
  getRepositoryStatus: (baseDirectory?: string) => Promise<LearnerHubRepositoriesStatus>;
  resetRepository: (baseDirectory: string) => Promise<InstallResult>;
  onResetOutput: (callback: (output: string) => void) => () => void;
  getConfig: () => Promise<{ exists: boolean; path: string; content?: string; baseDirectory?: string; learnerHubPath?: string }>;
  selectCloneFolder: () => Promise<string | null>;
  saveCloneFolder: (folder: string) => Promise<void>;
  openCanvasSetup: () => Promise<void>;
  updateTrayIcon: () => Promise<void>;
  getTheme: () => Promise<string>;
  onThemeChange: (callback: (theme: string) => void) => () => void;
}

export interface BackupEntry {
  id: string;
  containerName: string;
  notes: string;
  createdAt: string;
  filePath: string;
  fileName: string;
  isDefault?: boolean;
}

export interface BackupRestoreAPI {
  getContainers: () => Promise<string[]>;
  getBackups: () => Promise<BackupEntry[]>;
  setDefaultBackup: (id: string, isDefault: boolean) => Promise<void>;
  deleteBackup: (id: string) => Promise<void>;
  createBackup: (options: { containerName: string; notes: string }) => Promise<InstallResult>;
  onBackupOutput: (callback: (output: string) => void) => () => void;
  restoreBackup: (options: { backupFilePath: string; containerName: string }) => Promise<InstallResult>;
  onRestoreOutput: (callback: (output: string) => void) => () => void;
  selectBackupFile: () => Promise<string | null>;
  exportBackup: (backupId: string) => Promise<InstallResult>;
  importBackup: (exportedZipPath: string) => Promise<InstallResult>;
  selectExportFile: () => Promise<string | null>;
  getTheme: () => Promise<string>;
  onThemeChange: (callback: (theme: string) => void) => () => void;
}

declare global {
  interface Window {
    settingsAPI: SettingsAPI;
    setupWizardAPI: SetupWizardAPI;
    careerWizardAPI: CareerWizardAPI;
    learnerHubAPI: LearnerHubWizardAPI;
    actionsAPI: ActionsAPI;
    backupRestoreAPI: BackupRestoreAPI;
  }
}
