import { nowIso } from "../../shared/index.js";
import { listTeamInbox } from "./mailbox.js";
import { readTeamLaunchLedger } from "./launch-ledger.js";
import { listTeamTasks } from "./task-board.js";
export async function buildTeamRosterSnapshot(store, runId, runtime, currentMember) {
    const [tasks, launches] = await Promise.all([
        listTeamTasks(store, runId),
        readTeamLaunchLedger(store, runId),
    ]);
    const latestLaunchByRole = new Map();
    for (const entry of launches)
        latestLaunchByRole.set(entry.role_id, entry);
    const openTasks = tasks.filter((task) => task.status !== "completed" && task.status !== "cancelled");
    const members = await Promise.all(runtime.members.map(async (member) => rosterMember(store, runId, member, openTasks, latestLaunchByRole.get(String(member.name)))));
    return {
        schema_version: 1,
        generated_at: nowIso(),
        run_id: runId,
        team_id: runtime.team_id,
        team_name: runtime.team_name,
        runtime_status: runtime.status,
        current_member: currentMember,
        members,
        open_tasks: openTasks.slice(0, 20).map((task) => ({
            team_task_id: task.team_task_id,
            title: task.title,
            status: task.status,
            owner_agent: task.owner_agent,
            priority: task.priority,
        })),
    };
}
export function renderTeamRosterContext(snapshot) {
    return `<system-reminder>
[SEED team context]
[SEED team roster]
Team: ${snapshot.team_name ?? snapshot.team_id ?? snapshot.run_id}
Runtime: ${snapshot.runtime_status}
Current member: ${snapshot.current_member ?? "Lab-Representative/root"}
Members:
${snapshot.members.length ? snapshot.members.map(renderRosterMemberLine).join("\n") : "- none"}
Open tasks:
${snapshot.open_tasks.length ? snapshot.open_tasks.map((task) => `- ${task.team_task_id} ${task.status} owner:${task.owner_agent ?? "unclaimed"} p${task.priority} ${task.title}`).join("\n") : "- none"}

Use seed_team_roster for a fresh full roster, seed_team_inbox for messages, and seed_team_ack after reading messages that are no longer actionable.
</system-reminder>`;
}
async function rosterMember(store, runId, member, openTasks, latestLaunch) {
    const unread = await listTeamInbox(store, runId, String(member.name), { includeAcknowledged: false });
    return {
        role_id: String(member.name),
        role_label: member.role_label ?? String(member.name),
        role: member.role,
        status: member.status,
        capability_profile: member.capability_profile ? String(member.capability_profile) : undefined,
        opencode_agent: member.opencode_agent ? String(member.opencode_agent) : member.resolved_agent ? String(member.resolved_agent) : undefined,
        session_id: member.session_id,
        background_id: member.background_id,
        launch_status: member.launch_status,
        latest_launch_status: latestLaunch?.status,
        write_surface: member.write_surface ?? latestLaunch?.write_surface,
        read_paths: member.read_paths ?? latestLaunch?.read_paths ?? [],
        write_paths: member.write_paths ?? latestLaunch?.write_paths ?? [],
        unread_messages: unread.length,
        assigned_task_ids: openTasks.filter((task) => task.owner_agent === member.name).map((task) => task.team_task_id),
    };
}
function renderRosterMemberLine(member) {
    const unread = member.unread_messages > 0 ? ` unread:${member.unread_messages}` : "";
    const tasks = member.assigned_task_ids.length ? ` tasks:${member.assigned_task_ids.join(",")}` : "";
    const paths = member.write_paths.length ? ` writes:${member.write_paths.join(",")}` : "";
    const launch = member.latest_launch_status ? ` launch:${member.latest_launch_status}` : "";
    return `- ${member.role_id} (${member.role_label}) role:${member.role} status:${member.status} capability:${member.capability_profile ?? "unspecified"} opencode:${member.opencode_agent ?? "unknown"} surface:${member.write_surface ?? "read_only_or_coordination_only"}${launch}${unread}${tasks}${paths}`;
}
//# sourceMappingURL=roster.js.map