export type SchemetaPlacement = {
  x: number | null;
  y: number | null;
  rotation?: number;
  locked?: boolean;
};

export type SchemetaInstance = {
  ref: string;
  symbol?: string;
  part?: string;
  properties?: Record<string, unknown>;
  placement: SchemetaPlacement;
};

export type SchemetaModel = {
  meta?: Record<string, unknown>;
  symbols?: Record<string, unknown>;
  instances: SchemetaInstance[];
  nets?: Array<Record<string, unknown>>;
  constraints?: Record<string, unknown>;
  annotations?: Array<Record<string, unknown>>;
};

export type CompileResult = {
  api_version?: string;
  schema_version?: string;
  request_id?: string;
  ok: boolean;
  errors: Array<Record<string, unknown>>;
  warnings: Array<Record<string, unknown>>;
  layout?: Record<string, unknown>;
  layout_metrics?: Record<string, unknown>;
  render_mode_used?: string;
  svg?: string;
};

export type SelectionSlice = {
  selectedRefs: string[];
  selectedNet: string | null;
  selectedPin: { ref: string; pin: string } | null;
};

export type ViewportSlice = {
  scale: number;
  panX: number;
  panY: number;
};

export type UiFlagsSlice = {
  showLabels: boolean;
  isolateNet: boolean;
  isolateComponent: boolean;
  renderMode: string;
  jsonError: string | null;
};

export type LifecycleSlice = {
  isCompiling: boolean;
  lastError: string | null;
  lastAction: string | null;
};

export type StateSnapshot = {
  model: SchemetaModel | null;
  compileResult: CompileResult | null;
  selection: SelectionSlice;
  viewport: ViewportSlice;
  uiFlags: UiFlagsSlice;
};

export type StoreState = {
  model: SchemetaModel | null;
  compileResult: CompileResult | null;
  selection: SelectionSlice;
  viewport: ViewportSlice;
  uiFlags: UiFlagsSlice;
  lifecycle: LifecycleSlice;
  history: {
    past: Array<{ label: string; snapshot: StateSnapshot }>;
    future: Array<{ label: string; snapshot: StateSnapshot }>;
    limit: number;
  };
  transaction: {
    active: boolean;
    label: string | null;
    before: StateSnapshot | null;
    dirty: boolean;
  };
};

export type SchemetaStore = {
  getState(): StoreState;
  subscribe(listener: (next: StoreState, prev: StoreState, action: string) => void): () => void;
  actions: {
    setModel(model: unknown): void;
    setCompileResult(compileResult: unknown): void;
    setSelection(selection: Partial<SelectionSlice>): void;
    moveComponent(ref: string, placement: Partial<SchemetaPlacement>): void;
    setViewport(viewport: Partial<ViewportSlice>): void;
    setUiFlags(uiFlags: Partial<UiFlagsSlice>): void;
    setLifecycle(lifecycle: Partial<LifecycleSlice>): void;
    beginCompile(action: string): void;
    completeCompile(action: string): void;
    failCompile(action: string, error: unknown): void;
    applyJsonText(jsonText: string): { ok: true } | { ok: false; error: string };
    beginTransaction(label?: string): boolean;
    commitTransaction(): boolean;
    rollbackTransaction(): boolean;
    undo(): void;
    redo(): void;
  };
};

export function createInitialState(overrides?: Partial<StoreState>): StoreState;
export function createSchemetaStore(initialState?: Partial<StoreState>): SchemetaStore;
