import { createGoalSchema, updateGoalSchema } from "@rudderhq/shared";
import { Router } from "express";
import { validate } from "../middleware/validate.js";
import { goalService, logActivity } from "../services/index.js";
import { assertCompanyAccess, getActorInfo } from "./authz.js";
export function goalRoutes(db) {
    const router = Router();
    const svc = goalService(db);
    router.get("/orgs/:orgId/goals", async (req, res) => {
        const orgId = req.params.orgId;
        assertCompanyAccess(req, orgId);
        const result = await svc.list(orgId);
        res.json(result);
    });
    router.get("/goals/:id", async (req, res) => {
        const id = req.params.id;
        const goal = await svc.getById(id);
        if (!goal) {
            res.status(404).json({ error: "Goal not found" });
            return;
        }
        assertCompanyAccess(req, goal.orgId);
        res.json(goal);
    });
    router.get("/goals/:id/dependencies", async (req, res) => {
        const id = req.params.id;
        const goal = await svc.getById(id);
        if (!goal) {
            res.status(404).json({ error: "Goal not found" });
            return;
        }
        assertCompanyAccess(req, goal.orgId);
        res.json(await svc.dependencies(goal));
    });
    router.post("/orgs/:orgId/goals", validate(createGoalSchema), async (req, res) => {
        const orgId = req.params.orgId;
        assertCompanyAccess(req, orgId);
        const goal = await svc.create(orgId, req.body);
        const actor = getActorInfo(req);
        await logActivity(db, {
            orgId,
            actorType: actor.actorType,
            actorId: actor.actorId,
            agentId: actor.agentId,
            action: "goal.created",
            entityType: "goal",
            entityId: goal.id,
            details: { title: goal.title },
        });
        res.status(201).json(goal);
    });
    router.patch("/goals/:id", validate(updateGoalSchema), async (req, res) => {
        const id = req.params.id;
        const existing = await svc.getById(id);
        if (!existing) {
            res.status(404).json({ error: "Goal not found" });
            return;
        }
        assertCompanyAccess(req, existing.orgId);
        const goal = await svc.update(id, req.body);
        if (!goal) {
            res.status(404).json({ error: "Goal not found" });
            return;
        }
        const actor = getActorInfo(req);
        await logActivity(db, {
            orgId: goal.orgId,
            actorType: actor.actorType,
            actorId: actor.actorId,
            agentId: actor.agentId,
            action: "goal.updated",
            entityType: "goal",
            entityId: goal.id,
            details: req.body,
        });
        res.json(goal);
    });
    router.delete("/goals/:id", async (req, res) => {
        const id = req.params.id;
        const existing = await svc.getById(id);
        if (!existing) {
            res.status(404).json({ error: "Goal not found" });
            return;
        }
        assertCompanyAccess(req, existing.orgId);
        const goal = await svc.remove(id);
        if (!goal) {
            res.status(404).json({ error: "Goal not found" });
            return;
        }
        const actor = getActorInfo(req);
        await logActivity(db, {
            orgId: goal.orgId,
            actorType: actor.actorType,
            actorId: actor.actorId,
            agentId: actor.agentId,
            action: "goal.deleted",
            entityType: "goal",
            entityId: goal.id,
        });
        res.json(goal);
    });
    return router;
}
//# sourceMappingURL=goals.js.map