"use client";
import { useWords } from "@/components/Words";
import Link from "next/link";
import { useRouter } from "next/navigation";
import { useEffect, useState, useTransition } from "react";
import { useLiveIsland } from "@/lib/live";
import { ChevronDown, ChevronRight, ChevronsDown, ChevronsUp, Lock } from "lucide-react";
import { moveTaskCard, proposalAction } from "@/app/(app)/actions";
import { reorderTaskInline } from "@/app/(app)/board-actions";
import { Button } from "@/components/ui/button";
import { Badge } from "@/components/ui/badge";
import { AgentBadge } from "@/components/AgentBadge";
import { cn } from "@/lib/utils";
import { useT } from "@/components/I18n";
import { m } from "@peppa/shared/i18n";
export type Column = "backlog" | "in_progress" | "review" | "done";
export interface Card {
key: string;
/** PEP-197: the task id (the buttons of «Предложено» send ids). */
id?: number;
title: string;
status: string;
column: Column;
/** Epic label (PEP-66), if the story has an open epic. */
epic?: string | null;
/** tasks.md progress of a story in work (PEP-33). */
progress?: CardProgress | null;
/** PEP-42: "ждёт PEP-3" while a predecessor isn't in review/done. */
blocked?: string | null;
/** PEP-75: the persona working on the story right now. */
agent?: string | null;
/** PEP-84 / PEP-198: the analyst still holds the story ("уточняется" / "ждёт ваших ответов"). */
refining?: string | null;
/** PEP-127: accepted over a conflict — the agent updates the changes, the story closes by itself. */
accepted?: string | null;
/** PEP-150: a delay on the platform's side — nothing is needed from the owner. */
delayed?: string | null;
/** PEP-247: in work without an agent for a lawful reason (pause, no subscription, budget). */
hold?: string | null;
/** PEP-182: place in the queue for a free agent (in work, no agent yet). */
queued?: number | null;
/** PEP-197: a story of an agent the owner has not decided on — shown in «Предложено», not in the backlog. */
proposed?: boolean;
/** PEP-197: «Поломка» / «Важно» / «Мелочь» on stories of the acceptance agent. */
severity?: "breakage" | "important" | "minor" | null;
/** PEP-197: made by an agent, not by a person. */
agentMade?: boolean;
}
export interface CardProgress {
done: number;
total: number;
current: string | null;
}
/** «7/12» bar and the item the agent is on (PEP-33). */
export function ProgressLine({ p, className }: { p: CardProgress; className?: string }) {
const t = useT();
const w = useWords();
const pct = p.total ? Math.round((p.done / p.total) * 100) : 0;
return (
{p.done}/{p.total}
{p.current && {p.current}}
);
}
const DONE_COLLAPSED = "peppa.board.doneCollapsed";
const COLUMNS: Array<{ id: Column; title: string; hint: string; dot: string }> = [
{ id: "backlog", title: m("Бэклог"), hint: "", dot: "bg-muted-foreground" },
{ id: "in_progress", title: m("В работе"), hint: m("сюда — запустить агента"), dot: "bg-info" },
{ id: "review", title: m("Ревью"), hint: "", dot: "bg-ok" },
{ id: "done", title: m("Готово"), hint: "", dot: "bg-ok" },
];
const STATUS: Record = {
proposing: { label: m("план"), tone: "primary" },
awaiting_approval: { label: m("ждёт PM"), tone: "primary" },
blocked: { label: m("блок"), tone: "danger" },
in_progress: { label: m("агент"), tone: "info" },
};
const NEXT: Partial> = {
backlog: { to: "in_progress", label: m("В работу →") },
review: { to: "done", label: m("Принять ✓") },
};
/**
* Story board: drag a card to another column (or use its button) to move it — the server
* starts/stops agents; drag it onto a card of its own column to change the order (= priority
* for the dispatcher, PEP-34). «Готово» can be collapsed (remembered in the browser).
*/
export function Board({ cards: server, back }: { cards: Card[]; back: string }) {
// PEP-225: status and title changes from the live channel apply in place — a card moves to the
// column of its new status at once; fresh server data (a re-render) replaces the patches.
const [patches, setPatches] = useState