import { Show } from 'solid-js'
import { getSourceTypeInfo, formatSourceAttribution, getOriginalPostUrl } from '~/lib/source-integrations'
import { SOURCE_TYPES, type SourceType } from '~/lib/shared-schemas'

interface SourceAttributionProps {
  sourceType: string
  sourceUri?: string | null
  sourceData?: string | null
  class?: string
}

export function SourceAttribution(props: SourceAttributionProps) {
  const sourceType = () => props.sourceType as SourceType
  const info = () => getSourceTypeInfo(sourceType())
  const attribution = () => formatSourceAttribution(sourceType(), props.sourceUri || undefined)
  const originalUrl = () => getOriginalPostUrl(sourceType(), props.sourceUri || undefined, props.sourceData || undefined)
  
  // Don't show attribution for native Askimut content
  const shouldShow = () => sourceType() !== SOURCE_TYPES.ASKIMUT && attribution()
  
  return (
    <Show when={shouldShow()}>
      <div class={`source-attribution ${props.class || ''}`}>
        <span class="source-icon" style={{ color: info().color }}>
          {info().icon}
        </span>
        <Show 
          when={originalUrl()}
          fallback={<span class="source-text">{attribution()}</span>}
        >
          <a 
            href={originalUrl()!} 
            target="_blank" 
            rel="noopener noreferrer"
            class="source-link"
          >
            {attribution()}
          </a>
        </Show>
      </div>
    </Show>
  )
}
