/// <reference path="../.astro/types.d.ts" />

interface ProcessedAuditTrail {
  totalTime: number;
  avgConfidence: number;
  stepCount: number;
  highConfidenceSteps: number;
  lowConfidenceSteps: number;
  phases: Array<{
    name: string;
    icon: string;
    displayName: string;
    avgConfidence: number;
    totalTime: number;
    entries: CompressedAuditEntry[];
  }>;
  summary: {
    analysisQuality: 'excellent' | 'good' | 'fair' | 'poor';
    keyInsights: string[];
    potentialIssues: string[];
  };
}

interface CompressedAuditEntry {
  timestamp: number;
  phase: string;
  action: string;
  inputSummary: string;
  outputSummary: string;
  confidence: number;
  processingTimeMs: number;
  metadata: Record<string, any>;
}

declare global {
  interface Window {
    // Theme utilities
    themeUtils: {
      initTheme: () => void;
      toggleTheme: () => void;
      getStoredTheme: () => string;
      getSystemTheme: () => string;
      applyTheme: (theme: string) => void;
      updateThemeToggle: (theme: string) => void;
    };

    // Tool utilities - THESE WERE MISSING
    toolsData: any[];
    createToolSlug: (toolName: string) => string;
    findToolByIdentifier: (tools: any[], identifier: string) => any | undefined;
    isToolHosted: (tool: any) => boolean;

    // Modal and UI utilities
    showToolDetails: (toolName: string, modalType?: string) => void;
    hideToolDetails: (modalType?: string) => void;
    hideAllToolDetails: () => void;
    toggleKbEntry: (entryId: string) => void;
    toggleDomainAgnosticSection: (sectionId: string) => void;
    modalHideInProgress?: boolean;

    // AI interface utilities
    restoreAIResults?: () => void;
    switchToAIView?: () => void;

    // Filter and search utilities - THESE WERE MISSING
    clearTagFilters?: () => void;
    clearAllFilters?: () => void;
    applyScenarioSearch?: (scenarioId: string) => void;
    selectPhase?: (phase: string) => void;
    selectApproach?: (approach: string) => void;
    prioritizeSearchResults: (tools: any[], searchTerm: string) => any[];
    applyFilters?: () => void; // THIS WAS MISSING

    // Navigation utilities
    navigateToGrid?: (toolName: string) => void;
    navigateToMatrix?: (toolName: string) => void;
    toggleAllScenarios?: () => void;
    showShareDialog?: (shareButton: Element) => void;

    // Scroll utilities
    scrollToElement: (element: Element | null, options?: ScrollIntoViewOptions) => void;
    scrollToElementById: (elementId: string, options?: ScrollIntoViewOptions) => void;
    scrollToElementBySelector: (selector: string, options?: ScrollIntoViewOptions) => void;

    // Authentication utilities
    checkClientAuth: (context?: string) => Promise<{authenticated: boolean; authRequired: boolean; expires?: string}>;
    requireClientAuth: (callback?: () => void, returnUrl?: string, context?: string) => Promise<boolean>;
    showIfAuthenticated: (selector: string, context?: string) => Promise<void>;
    setupAuthButtons: (selector?: string) => void;

    // Sharing utilities
    shareArticle: (button: HTMLElement, url: string, title: string) => Promise<void>;
    shareCurrentArticle: (button: HTMLElement) => Promise<void>;

    // Audit trail utilities
    auditService?: {
      processAuditTrail: (rawAuditTrail: any[]) => ProcessedAuditTrail | null;
      formatDuration: (ms: number) => string;
      getConfidenceColor: (confidence: number) => string;
      getActionDisplayName: (action: string) => string;
      isEnabled: () => boolean;
    };

    AuditUtils?: {
      formatDuration: (ms: number) => string;
      getConfidenceColor: (confidence: number) => string;
      escapeHtml: (text: string) => string;
      sanitizeText: (text: string) => string;
    };

    AuditTrailRenderer?: new (
      containerId: string, 
      options?: {
        title?: string;
        collapsible?: boolean;
        defaultExpanded?: boolean;
      }
    ) => {
      containerId: string;
      options: {
        title: string;
        collapsible: boolean;
        defaultExpanded: boolean;
      };
      render: (processedAudit: ProcessedAuditTrail | null) => void;
    };
  }

  // EventTarget extension for better typing
  interface EventTarget {
    closest?: (selector: string) => Element | null;
  }
}

declare module 'js-yaml' {
  export function load(str: string): any;
}

export {};
