import type { ComponentId } from "@fragments-sdk/core";
import type { UniversalComponentFact } from "../ucf/types.js";
import type { ScanSource, ScanSourceFile } from "../source/types.js";
import type { ComponentCallSite, ObservedPropKind } from "./types.js";

export interface ReactComponentAdoptionIdentityInput {
  componentName: string;
  filePath: string;
}

export interface ReactComponentAdoptionIdentity {
  componentKey: string;
  componentId: ComponentId;
}

export interface AnalyzeReactComponentAdoptionInput {
  source: ScanSource;
  files: readonly ScanSourceFile[];
  ucfs: readonly UniversalComponentFact[];
  onProgress?(progress: { filesProcessed: number; totalFiles: number }): void;
  createIdentity(
    input: ReactComponentAdoptionIdentityInput,
  ): ReactComponentAdoptionIdentity;
}

export interface ReactComponentAdoptionDefinition {
  componentName: string;
  componentKey: string;
  componentId: ComponentId;
  filePath: string;
  exportNames: string[];
  publicExports: number;
  localNames: string[];
}

export interface ReactComponentAdoptionRef {
  componentName: string;
  displayName: string;
  filePath: string;
  componentKey: string;
}

export interface ReactComponentAdoptionTopImporter {
  filePath: string;
  line: number;
  kind: "jsx";
  confidence: "confirmed";
}

export interface ReactComponentAdoptionCallSite {
  filePath: string;
  line: number;
  column: number;
  props: Array<{
    name: string;
    kind: ObservedPropKind;
    value?: string;
  }>;
}

export interface ReactComponentAdoptionPropUsage {
  name: string;
  total: number;
  dynamic: number;
  spread: number;
  staticValues: Array<{ value: string; count: number }>;
}

export interface ReactComponentAdoptionComponent {
  componentName: string;
  componentKey: string;
  componentId: ComponentId;
  filePath: string;
  totalImporters: number;
  totalCallSites: number;
  topImporters: ReactComponentAdoptionTopImporter[];
  callSites: ReactComponentAdoptionCallSite[];
  propUsage: ReactComponentAdoptionPropUsage[];
  usedBy: ReactComponentAdoptionRef[];
  uses: ReactComponentAdoptionRef[];
  publicExports: number;
  confidence: "confirmed" | "likely" | "review";
  reviewReasons: string[];
  candidateForReview: boolean;
}

export interface ReactComponentAdoptionUnresolvedImport {
  filePath: string;
  line: number;
  column: number;
  elementName: string;
  local: string;
  imported: string;
  source: string;
  reason: "unresolved";
}

export interface ReactComponentAdoptionAnalysis {
  definitions: ReactComponentAdoptionDefinition[];
  components: ReactComponentAdoptionComponent[];
  callSites: ComponentCallSite[];
  unresolvedImports: ReactComponentAdoptionUnresolvedImport[];
}
