{{ $url := .Get "url" }}
{{ $title := .Get "title" }}
{{ $description := .Get "description" }}
{{ $image := .Get "image" }}
{{ $favicon := .Get "favicon" }}
{{ $provider := .Get "provider" }}
{{ $noFetch := .Get "noFetch" | default false }}

{{ $cardClasses := "flex flex-col md:flex-row relative overflow-hidden rounded-lg border border-neutral-300 dark:border-neutral-600 hover:border-neutral-400 dark:hover:border-neutral-500 transition-colors not-prose" }}

{{ $parsedURL := urls.Parse $url }}
{{ $domain := $parsedURL.Host }}

{{ $fetchedTitle := "" }}
{{ $fetchedDescription := "" }}
{{ $fetchedImage := "" }}
{{ $fetchedFavicon := "" }}
{{ $fetchedProvider := "" }}

{{ if and (not $noFetch) $url }}
  {{ with resources.GetRemote $url }}
    {{ $content := .Content }}
    
    {{ $ogTitleTags := findRE `<meta[^>]+property=["']og:title["'][^>]*>` $content 1 }}
    {{ with index $ogTitleTags 0 }}
      {{ $fetchedTitle = . | replaceRE `.*content=["']([^"']*)["'].*` `$1` }}
    {{ end }}

    {{ $ogDescTags := findRE `<meta[^>]+property=["']og:description["'][^>]*>` $content 1 }}
    {{ with index $ogDescTags 0 }}
      {{ $fetchedDescription = . | replaceRE `.*content=["']([^"']*)["'].*` `$1` }}
    {{ end }}

    {{ $ogImageTags := findRE `<meta[^>]+property=["']og:image["'][^>]*>` $content 1 }}
    {{ with index $ogImageTags 0 }}
      {{ $fetchedImage = . | replaceRE `.*content=["']([^"']*)["'].*` `$1` }}
    {{ end }}

    {{ $ogSiteNameTags := findRE `<meta[^>]+property=["']og:site_name["'][^>]*>` $content 1 }}
    {{ with index $ogSiteNameTags 0 }}
      {{ $fetchedProvider = . | replaceRE `.*content=["']([^"']*)["'].*` `$1` }}
    {{ end }}
  {{ end }}
{{ end }}

{{ $finalTitle := default $fetchedTitle $title }}
{{ $finalDescription := default $fetchedDescription $description }}
{{ $finalImage := default $fetchedImage $image }}
{{ $finalFavicon := default $fetchedFavicon $favicon }}
{{ $finalProvider := default $fetchedProvider $provider }}

{{ if not $finalProvider }}
  {{ $finalProvider = $domain }}
{{ end }}

{{ if not $finalFavicon }}
  {{ $finalFavicon = printf "https://%s/favicon.ico" $domain }}
{{ end }}

<a href="{{ $url }}" target="_blank" rel="noopener noreferrer" class="{{ $cardClasses }}">
  {{ with $finalImage }}
    <div class="flex-none md:w-48 lg:w-64">
      <img src="{{ . }}" alt="{{ $finalTitle }}" class="w-full h-48 md:h-full object-cover" loading="lazy" decoding="async" onerror="this.style.display='none'">
    </div>
  {{ end }}

  <div class="flex flex-col justify-between p-4 min-w-0">
    <div>
      <h3 class="text-lg font-semibold text-neutral-800 dark:text-neutral-200 line-clamp-2 mb-1">
        {{ $finalTitle | default $domain }}
        <span class="text-xs text-neutral-400 dark:text-neutral-500 align-top">&#8599;</span>
      </h3>

      {{ with $finalDescription }}
        <p class="text-sm text-neutral-600 dark:text-neutral-400 line-clamp-2 mb-2">{{ . }}</p>
      {{ end }}
    </div>

    <div class="flex items-center gap-2 text-xs text-neutral-500 dark:text-neutral-500">
      {{ with $finalFavicon }}
        <img src="{{ . }}" alt="" class="w-4 h-4" loading="lazy" onerror="this.style.display='none'">
      {{ end }}
      <span class="truncate">{{ $finalProvider }}</span>
    </div>
  </div>
</a>
