import type { AppliedDataSemantic } from '../Semantics.js'
import { SemanticType } from '../Semantics.js'

/**
 * Configuration for the Summary semantic.
 * Summary is a simple semantic without configuration options.
 */
export interface SummaryConfig {
  /**
   * Custom metadata for the summary field.
   */
  metadata?: Record<string, unknown>
  /**
   * Index signature to allow additional properties.
   */
  [key: string]: unknown
}

/**
 * Default configuration for the Summary semantic.
 */
export const DEFAULT_SUMMARY_CONFIG: SummaryConfig = {}

/**
 * Creates a Summary semantic application.
 * @param config - Optional configuration (not used for simple semantic)
 * @returns AppliedDataSemantic for Summary
 */
export const createSummarySemantic = (config?: SummaryConfig): AppliedDataSemantic => ({
  id: SemanticType.Summary,
  config: config ? { ...DEFAULT_SUMMARY_CONFIG, ...config } : { ...DEFAULT_SUMMARY_CONFIG },
})

/**
 * Type guard to check if a semantic is a Summary semantic.
 * @param semantic - The semantic to check
 * @returns True if the semantic is a Summary semantic
 */
export const isSummarySemantic = (
  semantic: AppliedDataSemantic
): semantic is AppliedDataSemantic & { id: SemanticType.Summary } => semantic.id === SemanticType.Summary

/**
 * Extracts the configuration from a Summary semantic application.
 * @param semantic - The semantic application
 * @returns The Summary configuration or undefined if not a Summary semantic
 */
export const getSummaryConfig = (semantic: AppliedDataSemantic): SummaryConfig | undefined => {
  if (!isSummarySemantic(semantic)) {
    return undefined
  }
  return semantic.config as SummaryConfig | undefined
}

/**
 * Validates Summary semantic configuration.
 * @returns True if the configuration is valid
 */
export const validateSummaryConfig = (): boolean => {
  // No validation needed for simple semantic
  return true
}

/**
 * Merges Summary configurations, with the second config taking precedence.
 * @param base - The base configuration
 * @param override - The configuration to merge on top
 * @returns Merged configuration
 */
export const mergeSummaryConfig = (base: SummaryConfig, override: SummaryConfig): SummaryConfig => {
  // No merging needed for simple semantic
  return { ...base, ...override }
}
