import type { TojuPluginManifest } from '../../../../shared-kernel';

export type PluginRuntimeState =
  | 'discovered'
  | 'validated'
  | 'blocked'
  | 'loading'
  | 'ready'
  | 'loaded'
  | 'failed'
  | 'unloading'
  | 'unloaded'
  | 'disabled';

export type PluginValidationSeverity = 'error' | 'warning';

export interface PluginValidationIssue {
  message: string;
  path: string;
  severity: PluginValidationSeverity;
}

export interface PluginManifestValidationResult {
  issues: PluginValidationIssue[];
  manifest?: TojuPluginManifest;
  valid: boolean;
}

export interface RegisteredPlugin {
  enabled: boolean;
  error?: string;
  loadIndex?: number;
  manifest: TojuPluginManifest;
  sourcePath?: string;
  state: PluginRuntimeState;
  validationIssues: PluginValidationIssue[];
}

export interface PluginLoadCandidate {
  enabled?: boolean;
  manifest: TojuPluginManifest;
}

export interface PluginLoadBlocker {
  message: string;
  pluginId: string;
  reason: 'conflict' | 'cycle' | 'disabled' | 'duplicate' | 'missingDependency' | 'validation';
}

export interface PluginLoadOrderResult {
  blocked: PluginLoadBlocker[];
  ordered: TojuPluginManifest[];
}

export interface LocalPluginManifestDescriptor {
  discoveredAt: number;
  entrypointPath?: string;
  pluginRootUrl?: string;
  manifest: unknown;
  manifestPath: string;
  pluginRoot: string;
  readmePath?: string;
}

export interface LocalPluginDiscoveryError {
  manifestPath?: string;
  message: string;
  pluginRoot?: string;
}

export interface LocalPluginDiscoveryResult {
  errors: LocalPluginDiscoveryError[];
  plugins: LocalPluginManifestDescriptor[];
  pluginsPath: string;
}

export interface LocalPluginRegistrationResult {
  discovery: LocalPluginDiscoveryResult;
  errors: LocalPluginDiscoveryError[];
  registered: RegisteredPlugin[];
}
