/**
 * Maximum task-title length. Titles are terse action prompts, not descriptions
 * (the longest seed is ~38 chars), so 64 is generous — a full sentence like
 * "Reply to the landlord about the broken heating before Friday" (60) — while
 * staying short enough that even the 2×2 home-screen widget, the binding
 * constraint, renders the longest allowed title *in full* and readably (≈5
 * lines, shrink-to-fit, no truncation). Chosen empirically: the small card holds
 * ~70 chars before it has to truncate, so 64 leaves a safe margin. Enforced both
 * on the capture input and in the local store (`src/data/task-store.ts`).
 */
export const TITLE_MAX_LENGTH = 64;

export type GlideDecision = 'clear' | 'resistant';

export type GlideTaskStatus = 'open' | 'ready' | 'done';

/**
 * A task's identity. Glide is local-first, so this is a client-generated UUID
 * (see `src/data/ids.ts`) rather than a server key — a task is identifiable the
 * instant it's created offline, which is what lets a future sync layer reconcile
 * without a round-trip. Opaque: never shown to the user.
 */
export type TaskId = string;

/**
 * The whole domain, distilled. A task is just a title and the resistance it has
 * accrued — everything else (priority, ordering) is an emergent property of the
 * swipe loop, never declared by the user.
 */
export type GlideTask = {
  id: TaskId;
  title: string;
  resistance: 1 | 2 | 3 | 4 | 5;
  status: GlideTaskStatus;
  exposureCount: number;
};

/**
 * The minimal shape `toGlideTask` reads. The persisted `LocalTask`
 * (`src/data/task-store.ts`) satisfies it structurally, so the domain stays free
 * of any storage-layer import while still mapping cleanly from the store.
 */
export type GlideTaskSource = {
  id: TaskId;
  title: string;
  resistance: number;
  status: GlideTaskStatus;
  exposureCount: number;
};

function clampResistance(value: number): GlideTask['resistance'] {
  return Math.min(5, Math.max(1, Math.round(value))) as GlideTask['resistance'];
}

export function toGlideTask(task: GlideTaskSource): GlideTask {
  return {
    id: task.id,
    title: task.title,
    resistance: clampResistance(task.resistance),
    status: task.status,
    exposureCount: task.exposureCount,
  };
}
