<script setup lang="ts">
interface List {
  id: string;
  name: string;
}
interface Task {
  id: string;
  listId: string;
  title: string;
  description: string | null;
  source: string;
  consumed: boolean;
  createdAt: string;
}

const { auth, api } = useAuth();

const lists = ref<List[]>([]);
const selectedId = ref<string | null>(null);
const tasks = ref<Task[]>([]);
const loadingLists = ref(true);
const loadingTasks = ref(false);
const error = ref<string | null>(null);

const title = ref("");
const description = ref("");
const showNote = ref(false);
const adding = ref(false);
const titleInput = ref<HTMLInputElement | null>(null);

const selectedList = computed(() => lists.value.find((l) => l.id === selectedId.value) ?? null);

const today = new Intl.DateTimeFormat(undefined, {
  weekday: "long",
  month: "long",
  day: "numeric",
}).format(new Date());

async function refreshLists() {
  loadingLists.value = true;
  error.value = null;
  try {
    lists.value = await api<List[]>("/lists");
    if (lists.value.length && !lists.value.some((l) => l.id === selectedId.value)) {
      await selectList(lists.value[0]!.id);
    }
  } catch (e) {
    error.value = (e as Error).message;
  } finally {
    loadingLists.value = false;
  }
}

async function selectList(id: string) {
  selectedId.value = id;
  await refreshTasks();
}

async function refreshTasks() {
  if (!selectedId.value) return;
  loadingTasks.value = true;
  error.value = null;
  try {
    tasks.value = await api<Task[]>(`/lists/${selectedId.value}/tasks`);
  } catch (e) {
    error.value = (e as Error).message;
  } finally {
    loadingTasks.value = false;
  }
}

async function addTask() {
  const t = title.value.trim();
  if (!t || !selectedId.value || adding.value) return;
  adding.value = true;
  error.value = null;
  try {
    const created = await api<Task>("/tasks", {
      method: "POST",
      headers: { "content-type": "application/json" },
      body: JSON.stringify({
        title: t,
        description: description.value.trim() || undefined,
        listId: selectedId.value,
      }),
    });
    tasks.value = [...tasks.value, created];
    title.value = "";
    description.value = "";
    showNote.value = false;
    titleInput.value?.focus();
  } catch (e) {
    error.value = (e as Error).message;
  } finally {
    adding.value = false;
  }
}

function toggleNote() {
  showNote.value = !showNote.value;
  if (!showNote.value) description.value = "";
}

onMounted(() => {
  // The auth plugin gates before mount, so this is normally authenticated.
  // Safety net: if not, drive login instead of calling the API (no 401 banner).
  if (auth.isAuthenticated) refreshLists();
  else auth.login();
});
</script>

<template>
  <div class="app">
    <header class="masthead">
      <div class="masthead-row">
        <p class="brand">ClaudeDo</p>
        <div class="who" v-if="auth.user">
          <span class="email">{{ auth.user.email || auth.user.name }}</span>
          <button class="link" @click="auth.logout()">Sign out</button>
        </div>
      </div>
      <h1>Inbox</h1>
      <p class="date">{{ today }}<template v-if="tasks.length"> · {{ tasks.length }} {{ tasks.length === 1 ? "task" : "tasks" }}</template></p>
    </header>

    <main class="content">
      <p v-if="error" class="error" role="alert">{{ error }}</p>

      <template v-if="loadingLists">
        <p class="muted">Loading lists…</p>
      </template>

      <template v-else-if="!lists.length">
        <div class="empty">
          <p class="empty-mark">◍</p>
          <p class="empty-title">No lists yet</p>
          <p class="muted">Sync your lists from the ClaudeDo desktop app, then add tasks here.</p>
        </div>
      </template>

      <template v-else>
        <nav class="lists" aria-label="Lists">
          <button
            v-for="l in lists"
            :key="l.id"
            class="chip"
            :class="{ active: l.id === selectedId }"
            @click="selectList(l.id)"
          >
            {{ l.name }}
          </button>
        </nav>

        <section class="tasks" aria-label="Tasks">
          <p v-if="loadingTasks" class="muted">Loading…</p>
          <div v-else-if="!tasks.length" class="empty">
            <p class="empty-mark">✓</p>
            <p class="empty-title">All clear</p>
            <p class="muted">Nothing in {{ selectedList?.name }} yet. Capture something below.</p>
          </div>
          <ul v-else class="task-list">
            <li v-for="(t, i) in tasks" :key="t.id" class="task" :style="{ '--i': i }">
              <p class="task-title">{{ t.title }}</p>
              <p v-if="t.description" class="task-desc">{{ t.description }}</p>
            </li>
          </ul>
        </section>
      </template>
    </main>

    <form v-if="lists.length" class="composer" @submit.prevent="addTask">
      <input
        v-if="showNote"
        v-model="description"
        class="input note"
        type="text"
        placeholder="Add a note…"
        autocomplete="off"
        enterkeyhint="done"
        aria-label="Task description"
      />
      <div class="capture">
        <button
          class="note-toggle"
          type="button"
          :class="{ on: showNote }"
          :aria-pressed="showNote"
          aria-label="Toggle note field"
          @click="toggleNote"
        >
          ✎
        </button>
        <input
          ref="titleInput"
          v-model="title"
          class="input"
          type="text"
          :placeholder="`Add to ${selectedList?.name ?? 'list'}…`"
          autocomplete="off"
          enterkeyhint="done"
          aria-label="Task title"
        />
        <button class="add" type="submit" :disabled="!title.trim() || adding" aria-label="Add task">
          <span v-if="adding" class="spin" aria-hidden="true"></span>
          <span v-else aria-hidden="true">↑</span>
        </button>
      </div>
    </form>
  </div>
</template>

<style scoped>
.app {
  --bg: #f4f1ea;
  --card: #fffdf8;
  --border: #e3ddd0;
  --text: #211c15;
  --muted: #837a6b;
  --accent: #c75b39;
  --accent-soft: #f3e3dc;
  --accent-contrast: #fffdf8;
  --danger: #b42318;
  --shadow: 0 1px 2px rgb(33 28 21 / 0.05), 0 4px 16px rgb(33 28 21 / 0.04);
  min-height: 100dvh;
  display: flex;
  flex-direction: column;
  background: var(--bg);
  color: var(--text);
  font-family: "Spline Sans", system-ui, sans-serif;
  -webkit-tap-highlight-color: transparent;
}

/* ——— Masthead: editorial, mobile-first ——— */
.masthead {
  padding: max(1rem, env(safe-area-inset-top)) 1.25rem 0.75rem;
}
.masthead-row {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 1rem;
}
.brand {
  margin: 0;
  font-size: 0.7rem;
  font-weight: 600;
  letter-spacing: 0.18em;
  text-transform: uppercase;
  color: var(--accent);
}
.masthead h1 {
  margin: 0.4rem 0 0;
  font-family: "Fraunces", Georgia, serif;
  font-weight: 600;
  font-size: clamp(2.1rem, 9vw, 2.75rem);
  line-height: 1.05;
  letter-spacing: -0.02em;
}
.date {
  margin: 0.3rem 0 0;
  font-size: 0.85rem;
  color: var(--muted);
}
.who {
  display: flex;
  align-items: baseline;
  gap: 0.6rem;
  min-width: 0;
}
.email {
  display: none;
  font-size: 0.78rem;
  color: var(--muted);
  max-width: 12rem;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.link {
  border: 0;
  background: none;
  color: var(--muted);
  font-size: 0.78rem;
  font-weight: 600;
  padding: 0.5rem 0;
  cursor: pointer;
  touch-action: manipulation;
}
.link:active {
  color: var(--accent);
}

.content {
  flex: 1;
  padding: 0.5rem 1.25rem 1rem;
  display: flex;
  flex-direction: column;
  gap: 0.9rem;
  overflow-y: auto;
  overscroll-behavior-y: contain;
}

/* ——— List chips: edge-to-edge swipe row ——— */
.lists {
  display: flex;
  gap: 0.5rem;
  overflow-x: auto;
  margin: 0 -1.25rem;
  padding: 0.25rem 1.25rem;
  scrollbar-width: none;
  scroll-snap-type: x proximity;
}
.lists::-webkit-scrollbar {
  display: none;
}
.chip {
  flex: 0 0 auto;
  scroll-snap-align: start;
  min-height: 44px;
  padding: 0.55rem 1.1rem;
  border-radius: 999px;
  border: 1px solid var(--border);
  background: var(--card);
  color: var(--text);
  font-family: inherit;
  font-size: 0.9rem;
  font-weight: 600;
  cursor: pointer;
  touch-action: manipulation;
  transition: background 0.15s, border-color 0.15s, color 0.15s, transform 0.1s;
}
.chip:active {
  transform: scale(0.96);
}
.chip.active {
  background: var(--accent);
  border-color: var(--accent);
  color: var(--accent-contrast);
}

/* ——— Tasks: paper cards with staggered entrance ——— */
.task-list {
  list-style: none;
  margin: 0;
  padding: 0;
  display: flex;
  flex-direction: column;
  gap: 0.6rem;
}
.task {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 0.9rem 1rem;
  box-shadow: var(--shadow);
  animation: rise 0.35s cubic-bezier(0.2, 0.7, 0.3, 1) both;
  animation-delay: calc(var(--i, 0) * 35ms);
}
@keyframes rise {
  from {
    opacity: 0;
    transform: translateY(10px);
  }
  to {
    opacity: 1;
    transform: none;
  }
}
.task-title {
  margin: 0;
  font-size: 1rem;
  font-weight: 500;
  line-height: 1.35;
}
.task-desc {
  margin: 0.35rem 0 0;
  font-size: 0.875rem;
  color: var(--muted);
  line-height: 1.45;
  white-space: pre-wrap;
}

.empty {
  margin-top: 14vh;
  text-align: center;
  padding: 0 1.5rem;
}
.empty-mark {
  margin: 0 0 0.5rem;
  font-size: 2rem;
  color: var(--accent);
}
.empty-title {
  font-family: "Fraunces", Georgia, serif;
  font-size: 1.35rem;
  font-weight: 600;
  margin: 0 0 0.35rem;
}
.muted {
  color: var(--muted);
  font-size: 0.9rem;
  line-height: 1.5;
  margin: 0;
}
.error {
  background: #fef3f2;
  color: var(--danger);
  border: 1px solid #fecdca;
  border-radius: 12px;
  padding: 0.65rem 0.85rem;
  font-size: 0.875rem;
  margin: 0;
}

/* ——— Composer: thumb-zone capture bar ——— */
.composer {
  position: sticky;
  bottom: 0;
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
  padding: 0.65rem 1rem calc(0.65rem + env(safe-area-inset-bottom));
  background: color-mix(in srgb, var(--bg) 88%, transparent);
  backdrop-filter: blur(12px);
  -webkit-backdrop-filter: blur(12px);
  border-top: 1px solid var(--border);
}
.capture {
  display: flex;
  align-items: center;
  gap: 0.5rem;
}
.input {
  flex: 1;
  width: 100%;
  min-width: 0;
  min-height: 50px;
  padding: 0.7rem 1rem;
  border: 1px solid var(--border);
  border-radius: 999px;
  /* 16px floor prevents iOS focus zoom */
  font-size: 1rem;
  font-family: inherit;
  background: var(--card);
  color: var(--text);
  box-shadow: var(--shadow);
}
.input:focus {
  outline: 2px solid var(--accent);
  outline-offset: 1px;
  border-color: transparent;
}
.input.note {
  min-height: 44px;
  font-size: 1rem;
  animation: rise 0.2s ease both;
}
.note-toggle {
  flex: 0 0 auto;
  width: 50px;
  height: 50px;
  border-radius: 50%;
  border: 1px solid var(--border);
  background: var(--card);
  color: var(--muted);
  font-size: 1.1rem;
  cursor: pointer;
  touch-action: manipulation;
  transition: background 0.15s, color 0.15s;
}
.note-toggle.on {
  background: var(--accent-soft);
  border-color: var(--accent);
  color: var(--accent);
}
.add {
  flex: 0 0 auto;
  width: 50px;
  height: 50px;
  border: 0;
  border-radius: 50%;
  background: var(--accent);
  color: var(--accent-contrast);
  font-size: 1.3rem;
  font-weight: 700;
  cursor: pointer;
  touch-action: manipulation;
  display: grid;
  place-items: center;
  transition: transform 0.1s, opacity 0.15s;
}
.add:active:not(:disabled) {
  transform: scale(0.92);
}
.add:disabled {
  opacity: 0.4;
  cursor: not-allowed;
}
.spin {
  width: 18px;
  height: 18px;
  border: 2px solid color-mix(in srgb, var(--accent-contrast) 35%, transparent);
  border-top-color: var(--accent-contrast);
  border-radius: 50%;
  animation: spin 0.7s linear infinite;
}
@keyframes spin {
  to {
    transform: rotate(360deg);
  }
}

/* ——— Larger screens: the enhancement, not the default ——— */
@media (min-width: 640px) {
  .masthead,
  .content,
  .composer {
    padding-left: max(1.25rem, calc(50vw - 19rem));
    padding-right: max(1.25rem, calc(50vw - 19rem));
  }
  .email {
    display: inline;
  }
  .lists {
    margin: 0;
    padding: 0.25rem 0;
  }
}

@media (prefers-reduced-motion: reduce) {
  .task,
  .input.note {
    animation: none;
  }
}

@media (prefers-color-scheme: dark) {
  .app {
    --bg: #181410;
    --card: #221d17;
    --border: #352d24;
    --text: #f0eae0;
    --muted: #9d9281;
    --accent: #e0795a;
    --accent-soft: #3a261f;
    --accent-contrast: #1d130f;
    --shadow: 0 1px 2px rgb(0 0 0 / 0.3), 0 4px 16px rgb(0 0 0 / 0.2);
  }
  .error {
    background: #2a1815;
    border-color: #5a2a22;
    color: #ff9a8d;
  }
}
</style>
