/**
 * Coordination Router — merge queue operations
 */

import { z } from 'zod';
import type { ProcedureBuilder } from '../trpc.js';
import { requireCoordinationManager } from './_helpers.js';

export function coordinationProcedures(publicProcedure: ProcedureBuilder) {
  return {
    queueMerge: publicProcedure
      .input(z.object({ taskId: z.string().min(1) }))
      .mutation(async ({ ctx, input }) => {
        const coordinationManager = requireCoordinationManager(ctx);
        await coordinationManager.queueMerge(input.taskId);
        return { success: true };
      }),

    processMerges: publicProcedure
      .input(z.object({
        targetBranch: z.string().default('main'),
      }))
      .mutation(async ({ ctx, input }) => {
        const coordinationManager = requireCoordinationManager(ctx);
        const results = await coordinationManager.processMerges(input.targetBranch);
        return { results };
      }),

    getMergeQueueStatus: publicProcedure
      .query(async ({ ctx }) => {
        const coordinationManager = requireCoordinationManager(ctx);
        return coordinationManager.getQueueState();
      }),

    getNextMergeable: publicProcedure
      .query(async ({ ctx }) => {
        const coordinationManager = requireCoordinationManager(ctx);
        return coordinationManager.getNextMergeable();
      }),
  };
}
