<script setup lang="ts">
import type { RuleHeader } from "@/api/rulebooks";
import UsageBadge from "@/components/UsageBadge.vue";

/** The dead-weight nudge for a RULE — two remedies, not one, which is the
 *  whole reason this advice is per-kind. A snippet nobody opens should
 *  probably go. A rule nobody opens may be perfectly good and simply firing on
 *  the wrong thing, so "delete it" would be the wrong nudge half the time and
 *  the operator has to be the one who picks. */
const RULE_DEAD_WEIGHT =
  "Kept arriving without being read. Either its trigger fires on the wrong " +
  "work — reword “when to apply” so it says when — or it is not wanted here. " +
  "Until one or the other, it takes a slot in every write it matches.";

defineProps<{ topicId: number; rules: RuleHeader[] }>();
const emit = defineEmits<{
  "open-rule": [id: number];
  "create-rule": [topicId: number];
}>();
</script>

<template>
  <section class="pane">
    <header><h2>Rules</h2></header>
    <ul>
      <li v-for="r in rules" :key="r.id" @click="emit('open-rule', r.id)">
        <div class="title">
          {{ r.title }}
          <!-- Only conditional is marked: always-on is the default and
               badging every row would say nothing. -->
          <span v-if="r.tier === 'conditional'" class="rule-chip" title="Arrives when its trigger fires, rather than in every session">conditional</span>
          <!-- Present only on a rule carrying a check, so the chip's very
               presence says "this one asserts a fact that can go false". -->
          <span
            v-if="r.last_verified"
            class="rule-chip check-chip"
            :class="{ unchecked: r.last_verified === 'never' }"
            :title="r.last_verified === 'never'
              ? 'Asserts a fact nobody has confirmed yet'
              : `Check last passed ${r.last_verified}`"
          >{{ r.last_verified === "never" ? "unverified" : `checked ${r.last_verified}` }}</span>
          <UsageBadge :usage="r.usage" :dead-weight-advice="RULE_DEAD_WEIGHT" />
        </div>
        <div class="statement">{{ r.statement }}</div>
        <div v-if="r.when_to_apply || r.updated_at" class="meta">
          <span v-if="r.when_to_apply" class="trigger">{{ r.when_to_apply }}</span>
          <span v-if="r.updated_at" class="age" :title="`Last changed ${r.updated_at}`">{{ r.updated_at }}</span>
        </div>
      </li>
    </ul>
    <button class="new-rule" @click="emit('create-rule', topicId)">+ New rule</button>
  </section>
</template>

<style src="@/assets/rules-shared.css" />
<style scoped>
ul { list-style: none; padding: 0; margin: 1rem 0; }
li {
  padding: 0.75rem;
  cursor: pointer;
  border-radius: 6px;
  border-left: 2px solid var(--fs-accent);
  margin-bottom: 0.5rem;
  background: rgba(255, 255, 255, 0.02);
}
li:hover { background: var(--fs-surface-hover); }
.title { font-family: Fraunces, serif; font-style: italic; font-size: 1.05em; }
.statement { font-size: 0.9em; opacity: 0.8; margin-top: 0.25rem; }
.meta { display: flex; align-items: baseline; gap: 0.5rem; margin-top: 0.35rem; font-size: 0.75em; }
.trigger { flex: 1; min-width: 0; color: var(--fs-text-secondary); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.age { color: var(--fs-text-tertiary); font-variant-numeric: tabular-nums; flex-shrink: 0; }
/* Only the departures from .rule-chip (rules-shared.css) live here. */
.check-chip { font-variant-numeric: tabular-nums; }
/* No age-graded colour on purpose. The sweep is already ordered by urgency, so
   a red/amber ramp would restate the ordering AND require an invented "stale
   after N days" threshold — a magic number nobody could defend and the first
   thing to go out of date. Only "never" is marked, because it is categorically
   different from a date rather than a worse one. */
.check-chip.unchecked { font-style: italic; color: var(--fs-text-tertiary); }
.new-rule { cursor: pointer; }
</style>
