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

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="tier-chip" title="Arrives when its trigger fires, rather than in every session">conditional</span>
        </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; }
.tier-chip {
  margin-left: 0.4rem;
  font-family: var(--fs-font-body);
  font-style: normal;
  font-size: 0.62rem;
  color: var(--fs-text-secondary);
  background: var(--fs-surface-raised);
  border-radius: var(--fs-radius-pill);
  padding: 0.05rem 0.4rem;
  vertical-align: middle;
}
.new-rule { cursor: pointer; }
</style>
