import { createRoute } from "../../../modules/api-utils/createRoute";
import { timeEntries } from "../../../managers/timeEntries";
import { apiResponse } from "../../../modules/api-utils/apiResponse";
import { ContentfulStatusCode } from "hono/utils/http-status";
import { authMiddleware } from "../../middleware/authorization";

/* GET /v1/time-entry/ticket/:ticketId */
export default createRoute(
    "get",
    ["/ticket/:ticketId"],
    async (c) => {
        const ticketId = parseInt(c.req.param("ticketId"), 10);

        const data = await timeEntries.fetchByTicket(ticketId);

        const response = apiResponse.successful(
            "Time entries fetched successfully!",
            data.map((e) => e.toJson()),
        );

        return c.json(response, response.status as ContentfulStatusCode);
    },
    authMiddleware({ permissions: ["time-entry.fetch.many"] }),
);
