{{/* SEP (Spec Enhancement Proposals) Page Content */}}
{{/* Only available for modelcontextprotocol/modelcontextprotocol */}}
{{ $context := . }}
{{ $repoKey := "" }}
{{ if reflect.IsMap . }}
  {{ with .context }}{{ $context = . }}{{ end }}
  {{ with .repoKey }}{{ $repoKey = . }}{{ end }}
{{ end }}

{{/* Load SEP data directly */}}
{{ $seps := false }}
{{ $parts := split $repoKey "/" }}
{{ if eq (len $parts) 2 }}
  {{ $owner := index $parts 0 }}
  {{ $repo := index $parts 1 }}
  {{ with index site.Data.repos $owner }}
    {{ with index . $repo }}
      {{ $seps = .seps }}
    {{ end }}
  {{ end }}
{{ end }}

{{/* Check if this is a non-MCP repo */}}
{{ $isMCPRepo := and (eq (index $parts 0) "modelcontextprotocol") (eq (index $parts 1) "modelcontextprotocol") }}

{{ if not $isMCPRepo }}
<div class="bg-base-100 border border-base-300 rounded-lg p-4">
  <p class="text-sm text-base-content/70">
    Spec Enhancement Proposals (SEPs) are only tracked for the MCP Specification repository.
    <button
      onclick="window.RepoPicker.setSelectedRepo('modelcontextprotocol', 'modelcontextprotocol')"
      class="text-primary hover:underline font-medium ml-1"
    >Switch to MCP Specification &rarr;</button>
  </p>
</div>
{{ else if not $seps }}
<div class="alert alert-info">
  <span>No SEP data available. Run the aggregation script with --sep to populate SEP metrics.</span>
</div>
{{ else }}

{{/* Build GitHub filter URLs */}}
{{ $ghBase := printf "https://github.com/%s/pulls" $repoKey }}
{{ $proposalLink := printf "%s?q=is:pr+is:open+label:SEP+label:proposal" $ghBase }}
{{ $draftLink := printf "%s?q=is:pr+is:open+label:SEP+label:draft" $ghBase }}
{{ $inReviewLink := printf "%s?q=is:pr+is:open+label:SEP+label:in-review" $ghBase }}
{{ $acceptedLink := printf "%s?q=is:pr+is:open+label:SEP+label:accepted" $ghBase }}
{{ $mergedLink := printf "%s?q=is:pr+is:merged+label:SEP" $ghBase }}
{{ $allSEPLink := printf "%s?q=is:pr+label:SEP" $ghBase }}

<!-- Summary Cards -->
<section class="mb-6">
  <h2 class="text-sm font-semibold mb-3">Overview</h2>
  <div class="grid grid-cols-2 sm:grid-cols-6 gap-3">
    {{ partial "metric-card.html" (dict "label" "Total SEPs" "value" $seps.counts.total "link" $allSEPLink) }}
    {{ partial "metric-card.html" (dict "label" "Proposals" "value" $seps.counts.proposal "descriptor" "awaiting sponsor" "link" $proposalLink) }}
    {{ partial "metric-card.html" (dict "label" "Drafts" "value" $seps.counts.draft "descriptor" "being developed" "link" $draftLink) }}
    {{ partial "metric-card.html" (dict "label" "In Review" "value" $seps.counts.inReview "descriptor" "ready for review" "link" $inReviewLink) }}
    {{ partial "metric-card.html" (dict "label" "Accepted" "value" $seps.counts.accepted "descriptor" "pending merge" "link" $acceptedLink) }}
    {{ partial "metric-card.html" (dict "label" "Merged" "value" $seps.counts.merged "descriptor" "completed" "link" $mergedLink) }}
  </div>
</section>

<!-- SEP Proposals (no sponsor) -->
{{ if $seps.proposals }}
<section class="mb-6">
  <h2 class="text-sm font-semibold mb-1">Proposals Awaiting Sponsor</h2>
  <p class="text-[10px] text-base-content/40 mb-3">SEPs that need a maintainer to sponsor them</p>
  <div class="bg-base-100 border border-base-300 rounded-lg overflow-hidden">
    <table class="table table-xs w-full table-fixed">
      <thead>
        <tr class="border-b border-base-300">
          <th class="bg-base-200/50 text-xs font-medium w-16">PR</th>
          <th class="bg-base-200/50 text-xs font-medium">Title</th>
          <th class="bg-base-200/50 text-xs font-medium w-28">Author</th>
          <th class="bg-base-200/50 text-xs font-medium w-28 text-base-content/30">Sponsor</th>
          <th class="bg-base-200/50 text-xs font-medium text-right w-24">Size</th>
          <th class="bg-base-200/50 text-xs font-medium text-right w-20">Waiting</th>
        </tr>
      </thead>
      <tbody>
        {{ range $seps.proposals }}
        <tr class="border-b border-base-300 last:border-b-0 hover:bg-base-200/50 cursor-pointer transition-colors group" onclick="window.open('{{ .url }}', '_blank')">
          <td class="text-xs font-mono whitespace-nowrap">
            <a href="{{ .url }}" target="_blank" class="text-primary hover:underline" onclick="event.stopPropagation()">#{{ .number }}</a>
          </td>
          <td class="text-xs truncate" title="{{ .title }}">
            {{ .title }}
          </td>
          <td class="text-xs text-base-content/70 truncate">
            {{ if .author }}{{ .author }}{{ else }}<span class="italic">unknown</span>{{ end }}
          </td>
          <td class="text-xs text-base-content/30">—</td>
          <td class="text-xs text-right whitespace-nowrap">
            <span class="text-success">+{{ .additions }}</span>
            <span class="text-error">-{{ .deletions }}</span>
          </td>
          <td class="text-xs text-right font-medium">
            {{ if gt .daysWaiting 30 }}
            <span class="text-error">{{ .daysWaiting }}d</span>
            {{ else if gt .daysWaiting 7 }}
            <span class="text-warning">{{ .daysWaiting }}d</span>
            {{ else }}
            {{ .daysWaiting }}d
            {{ end }}
          </td>
        </tr>
        {{ end }}
      </tbody>
    </table>
  </div>
</section>
{{ end }}

<!-- SEP Drafts (has sponsor) -->
{{ if $seps.drafts }}
<section class="mb-6">
  <h2 class="text-sm font-semibold mb-1">Drafts in Development</h2>
  <p class="text-[10px] text-base-content/40 mb-3">SEPs with a sponsor actively working on them</p>
  <div class="bg-base-100 border border-base-300 rounded-lg overflow-hidden">
    <table class="table table-xs w-full table-fixed">
      <thead>
        <tr class="border-b border-base-300">
          <th class="bg-base-200/50 text-xs font-medium w-16">PR</th>
          <th class="bg-base-200/50 text-xs font-medium">Title</th>
          <th class="bg-base-200/50 text-xs font-medium w-28">Author</th>
          <th class="bg-base-200/50 text-xs font-medium w-28">Sponsor</th>
          <th class="bg-base-200/50 text-xs font-medium text-right w-24">Size</th>
          <th class="bg-base-200/50 text-xs font-medium text-right w-20">Age</th>
        </tr>
      </thead>
      <tbody>
        {{ range $seps.drafts }}
        <tr class="border-b border-base-300 last:border-b-0 hover:bg-base-200/50 cursor-pointer transition-colors group" onclick="window.open('{{ .url }}', '_blank')">
          <td class="text-xs font-mono whitespace-nowrap">
            <a href="{{ .url }}" target="_blank" class="text-primary hover:underline" onclick="event.stopPropagation()">#{{ .number }}</a>
          </td>
          <td class="text-xs truncate" title="{{ .title }}">
            {{ .title }}
          </td>
          <td class="text-xs text-base-content/70 truncate">
            {{ if .author }}{{ .author }}{{ else }}<span class="italic">unknown</span>{{ end }}
          </td>
          <td class="text-xs text-base-content/70 truncate">
            {{ if .sponsor }}<span class="text-info">{{ .sponsor }}</span>{{ else }}<span class="text-base-content/30">—</span>{{ end }}
          </td>
          <td class="text-xs text-right whitespace-nowrap">
            <span class="text-success">+{{ .additions }}</span>
            <span class="text-error">-{{ .deletions }}</span>
          </td>
          <td class="text-xs text-right font-medium">
            {{ if gt .daysWaiting 30 }}
            <span class="text-error">{{ .daysWaiting }}d</span>
            {{ else if gt .daysWaiting 7 }}
            <span class="text-warning">{{ .daysWaiting }}d</span>
            {{ else }}
            {{ .daysWaiting }}d
            {{ end }}
          </td>
        </tr>
        {{ end }}
      </tbody>
    </table>
  </div>
</section>
{{ end }}

<!-- SEPs In Review -->
{{ if $seps.inReview }}
<section class="mb-6">
  <h2 class="text-sm font-semibold mb-1">SEPs In Review</h2>
  <p class="text-[10px] text-base-content/40 mb-3">SEPs ready for community and maintainer review</p>
  <div class="bg-base-100 border border-base-300 rounded-lg overflow-hidden">
    <table class="table table-xs w-full table-fixed">
      <thead>
        <tr class="border-b border-base-300">
          <th class="bg-base-200/50 text-xs font-medium w-16">PR</th>
          <th class="bg-base-200/50 text-xs font-medium">Title</th>
          <th class="bg-base-200/50 text-xs font-medium w-28">Author</th>
          <th class="bg-base-200/50 text-xs font-medium w-28">Sponsor</th>
          <th class="bg-base-200/50 text-xs font-medium text-right w-24">Reviews</th>
          <th class="bg-base-200/50 text-xs font-medium text-right w-20">Waiting</th>
        </tr>
      </thead>
      <tbody>
        {{ range $seps.inReview }}
        <tr class="border-b border-base-300 last:border-b-0 hover:bg-base-200/50 cursor-pointer transition-colors group" onclick="window.open('{{ .url }}', '_blank')">
          <td class="text-xs font-mono whitespace-nowrap">
            <a href="{{ .url }}" target="_blank" class="text-primary hover:underline" onclick="event.stopPropagation()">#{{ .number }}</a>
          </td>
          <td class="text-xs truncate" title="{{ .title }}">
            {{ .title }}
          </td>
          <td class="text-xs text-base-content/70 truncate">
            {{ if .author }}{{ .author }}{{ else }}<span class="italic">unknown</span>{{ end }}
          </td>
          <td class="text-xs text-base-content/70 truncate">
            {{ if .sponsor }}<span class="text-info">{{ .sponsor }}</span>{{ else }}<span class="text-base-content/30">—</span>{{ end }}
          </td>
          <td class="text-xs text-right">
            {{ if gt .reviewCount 0 }}
            <span class="text-success">{{ .reviewCount }}</span>
            {{ else }}
            <span class="text-base-content/40">0</span>
            {{ end }}
          </td>
          <td class="text-xs text-right font-medium">
            {{ if gt .daysWaiting 14 }}
            <span class="text-error">{{ .daysWaiting }}d</span>
            {{ else if gt .daysWaiting 7 }}
            <span class="text-warning">{{ .daysWaiting }}d</span>
            {{ else }}
            {{ .daysWaiting }}d
            {{ end }}
          </td>
        </tr>
        {{ end }}
      </tbody>
    </table>
  </div>
</section>
{{ end }}

<!-- Accepted SEPs (pending merge) -->
{{ if $seps.accepted }}
<section class="mb-6">
  <h2 class="text-sm font-semibold mb-1">Accepted SEPs</h2>
  <p class="text-[10px] text-base-content/40 mb-3">SEPs approved and awaiting merge</p>
  <div class="bg-base-100 border border-base-300 rounded-lg overflow-hidden">
    <table class="table table-xs w-full table-fixed">
      <thead>
        <tr class="border-b border-base-300">
          <th class="bg-base-200/50 text-xs font-medium w-16">PR</th>
          <th class="bg-base-200/50 text-xs font-medium">Title</th>
          <th class="bg-base-200/50 text-xs font-medium w-28">Author</th>
          <th class="bg-base-200/50 text-xs font-medium w-28">Sponsor</th>
          <th class="bg-base-200/50 text-xs font-medium text-right w-24">Size</th>
          <th class="bg-base-200/50 text-xs font-medium text-right w-20">Waiting</th>
        </tr>
      </thead>
      <tbody>
        {{ range $seps.accepted }}
        <tr class="border-b border-base-300 last:border-b-0 hover:bg-base-200/50 cursor-pointer transition-colors group" onclick="window.open('{{ .url }}', '_blank')">
          <td class="text-xs font-mono whitespace-nowrap">
            <a href="{{ .url }}" target="_blank" class="text-primary hover:underline" onclick="event.stopPropagation()">#{{ .number }}</a>
          </td>
          <td class="text-xs truncate" title="{{ .title }}">
            {{ .title }}
          </td>
          <td class="text-xs text-base-content/70 truncate">
            {{ if .author }}{{ .author }}{{ else }}<span class="italic">unknown</span>{{ end }}
          </td>
          <td class="text-xs text-base-content/70 truncate">
            {{ if .sponsor }}<span class="text-info">{{ .sponsor }}</span>{{ else }}<span class="text-base-content/30">—</span>{{ end }}
          </td>
          <td class="text-xs text-right whitespace-nowrap">
            <span class="text-success">+{{ .additions }}</span>
            <span class="text-error">-{{ .deletions }}</span>
          </td>
          <td class="text-xs text-right font-medium">
            {{ .daysWaiting }}d
          </td>
        </tr>
        {{ end }}
      </tbody>
    </table>
  </div>
</section>
{{ end }}

<!-- Merged SEPs -->
{{ if $seps.merged }}
<section class="mb-6">
  <h2 class="text-sm font-semibold mb-1">Merged SEPs</h2>
  <p class="text-[10px] text-base-content/40 mb-3">Successfully completed spec enhancements</p>
  <div class="bg-base-100 border border-base-300 rounded-lg overflow-hidden">
    <table class="table table-xs w-full table-fixed">
      <thead>
        <tr class="border-b border-base-300">
          <th class="bg-base-200/50 text-xs font-medium w-16">PR</th>
          <th class="bg-base-200/50 text-xs font-medium">Title</th>
          <th class="bg-base-200/50 text-xs font-medium w-28">Author</th>
          <th class="bg-base-200/50 text-xs font-medium w-28">Sponsor</th>
          <th class="bg-base-200/50 text-xs font-medium text-right w-24">Size</th>
          <th class="bg-base-200/50 text-xs font-medium text-right w-20">Merged</th>
        </tr>
      </thead>
      <tbody>
        {{ range $seps.merged }}
        <tr class="border-b border-base-300 last:border-b-0 hover:bg-base-200/50 cursor-pointer transition-colors group" onclick="window.open('{{ .url }}', '_blank')">
          <td class="text-xs font-mono whitespace-nowrap">
            <a href="{{ .url }}" target="_blank" class="text-primary hover:underline" onclick="event.stopPropagation()">#{{ .number }}</a>
          </td>
          <td class="text-xs truncate" title="{{ .title }}">
            {{ .title }}
          </td>
          <td class="text-xs text-base-content/70 truncate">
            {{ if .author }}{{ .author }}{{ else }}<span class="italic">unknown</span>{{ end }}
          </td>
          <td class="text-xs text-base-content/70 truncate">
            {{ if .sponsor }}<span class="text-info">{{ .sponsor }}</span>{{ else }}<span class="text-base-content/30">—</span>{{ end }}
          </td>
          <td class="text-xs text-right whitespace-nowrap">
            <span class="text-success">+{{ .additions }}</span>
            <span class="text-error">-{{ .deletions }}</span>
          </td>
          <td class="text-xs text-right text-base-content/70">
            {{ if .mergedAt }}
            {{ $mergedTime := time .mergedAt }}
            {{ $mergedTime.Format "Jan 2, 2006" }}
            {{ end }}
          </td>
        </tr>
        {{ end }}
      </tbody>
    </table>
  </div>
</section>
{{ end }}

{{ end }}
