'use strict';

var errors = require('@backstage/errors');
var zod = require('zod');
var express = require('express');
var Router = require('express-promise-router');

function _interopDefaultCompat (e) { return e && typeof e === 'object' && 'default' in e ? e : { default: e }; }

var express__default = /*#__PURE__*/_interopDefaultCompat(express);
var Router__default = /*#__PURE__*/_interopDefaultCompat(Router);

async function createRouter({
  httpAuth: _httpAuth,
  githubWorkflowsService
}) {
  const router = Router__default.default();
  router.use(express__default.default.json());
  const listWorkflowsSchema = zod.z.object({
    hostname: zod.z.string(),
    githubRepoSlug: zod.z.string(),
    branch: zod.z.string(),
    filter: zod.z.string().optional().transform((val) => val ? val.split(",") : void 0)
  });
  const repoSchema = zod.z.object({
    hostname: zod.z.string(),
    githubRepoSlug: zod.z.string()
  });
  const startWorkflowSchema = zod.z.object({
    hostname: zod.z.string(),
    githubRepoSlug: zod.z.string(),
    workflowId: zod.z.number(),
    branch: zod.z.string(),
    inputs: zod.z.record(zod.z.unknown()).optional()
  });
  const stopWorkflowSchema = zod.z.object({
    hostname: zod.z.string(),
    githubRepoSlug: zod.z.string(),
    runId: zod.z.number()
  });
  const listJobsSchema = zod.z.object({
    hostname: zod.z.string(),
    githubRepoSlug: zod.z.string(),
    id: zod.z.coerce.number(),
    pageSize: zod.z.coerce.number().optional(),
    page: zod.z.coerce.number().optional()
  });
  const getWorkflowRunSchema = zod.z.object({
    hostname: zod.z.string(),
    githubRepoSlug: zod.z.string(),
    runId: zod.z.coerce.number()
  });
  const downloadLogsSchema = zod.z.object({
    hostname: zod.z.string(),
    githubRepoSlug: zod.z.string(),
    jobId: zod.z.coerce.number()
  });
  router.get("/workflows", async (req, res) => {
    console.log("[Backend Router] GET /workflows - Query params:", req.query);
    const parsed = listWorkflowsSchema.safeParse(req.query);
    if (!parsed.success) {
      console.error("[Backend Router] Validation error:", parsed.error);
      throw new errors.InputError(parsed.error.toString());
    }
    console.log("[Backend Router] Parsed data:", parsed.data);
    const result = await githubWorkflowsService.listWorkflows(
      parsed.data.hostname,
      parsed.data.githubRepoSlug,
      parsed.data.branch,
      parsed.data.filter
    );
    console.log("[Backend Router] Returning result with", result?.length || 0, "workflows");
    res.json(result);
  });
  router.get("/branches", async (req, res) => {
    const parsed = repoSchema.safeParse(req.query);
    if (!parsed.success) {
      throw new errors.InputError(parsed.error.toString());
    }
    const result = await githubWorkflowsService.listBranchesFromRepo(
      parsed.data.hostname,
      parsed.data.githubRepoSlug
    );
    res.json(result);
  });
  router.get("/default-branch", async (req, res) => {
    const parsed = repoSchema.safeParse(req.query);
    if (!parsed.success) {
      throw new errors.InputError(parsed.error.toString());
    }
    const result = await githubWorkflowsService.getBranchDefaultFromRepo(
      parsed.data.hostname,
      parsed.data.githubRepoSlug
    );
    res.json(result);
  });
  router.post("/start-workflow", async (req, res) => {
    const parsed = startWorkflowSchema.safeParse(req.body);
    if (!parsed.success) {
      throw new errors.InputError(parsed.error.toString());
    }
    const result = await githubWorkflowsService.startWorkflowRun(
      parsed.data.hostname,
      parsed.data.githubRepoSlug,
      parsed.data.workflowId,
      parsed.data.branch,
      parsed.data.inputs
    );
    res.json({ status: result });
  });
  router.post("/stop-workflow", async (req, res) => {
    const parsed = stopWorkflowSchema.safeParse(req.body);
    if (!parsed.success) {
      throw new errors.InputError(parsed.error.toString());
    }
    const result = await githubWorkflowsService.stopWorkflowRun(
      parsed.data.hostname,
      parsed.data.githubRepoSlug,
      parsed.data.runId
    );
    res.json({ status: result });
  });
  router.get("/jobs", async (req, res) => {
    const parsed = listJobsSchema.safeParse(req.query);
    if (!parsed.success) {
      throw new errors.InputError(parsed.error.toString());
    }
    const result = await githubWorkflowsService.listJobsForWorkflowRun(
      parsed.data.hostname,
      parsed.data.githubRepoSlug,
      parsed.data.id,
      parsed.data.pageSize,
      parsed.data.page
    );
    res.json(result);
  });
  router.get("/workflow-run", async (req, res) => {
    const parsed = getWorkflowRunSchema.safeParse(req.query);
    if (!parsed.success) {
      throw new errors.InputError(parsed.error.toString());
    }
    const result = await githubWorkflowsService.getWorkflowRunById(
      parsed.data.hostname,
      parsed.data.githubRepoSlug,
      parsed.data.runId
    );
    res.json(result);
  });
  router.get("/download-logs", async (req, res) => {
    const parsed = downloadLogsSchema.safeParse(req.query);
    if (!parsed.success) {
      throw new errors.InputError(parsed.error.toString());
    }
    const result = await githubWorkflowsService.downloadJobLogsForWorkflowRun(
      parsed.data.hostname,
      parsed.data.githubRepoSlug,
      parsed.data.jobId
    );
    res.json(result);
  });
  router.get("/environments", async (req, res) => {
    const parsed = repoSchema.safeParse(req.query);
    if (!parsed.success) {
      throw new errors.InputError(parsed.error.toString());
    }
    const result = await githubWorkflowsService.getEnvironmentsList(
      parsed.data.hostname,
      parsed.data.githubRepoSlug
    );
    res.json(result);
  });
  return router;
}

exports.createRouter = createRouter;
//# sourceMappingURL=router.cjs.js.map
