import { MemoryCacheAdapter } from "../cache/memory-cache.js";
import { PgMasterRepository } from "./repository.js";
import { MasterDataService } from "./service.js";
import { CreateBodySchema, IdParamSchema, ListQuerySchema, UpdateBodySchema, } from "./types.js";
// Instantiated once per module registration
const service = new MasterDataService(new PgMasterRepository(), new MemoryCacheAdapter());
export async function registerMasterDataRoutes(app) {
    // GET /api/master-data?domain=...&page=...&limit=...&q=...&parentId=...&isActive=...
    app.get("/api/master-data", async (request, reply) => {
        const parsed = ListQuerySchema.safeParse(request.query);
        if (!parsed.success) {
            return reply.code(400).send({ message: parsed.error.message });
        }
        const { domain, page, limit, q, parentId, isActive } = parsed.data;
        try {
            const result = await service.list(domain, { page, limit, q, parentId, isActive });
            return reply.send(result);
        }
        catch (err) {
            const message = err.message;
            if (message.includes("Unknown master-data domain")) {
                return reply.code(400).send({ message });
            }
            throw err;
        }
    });
    // GET /api/master-data/:domain/:id
    app.get("/api/master-data/:domain/:id", async (request, reply) => {
        const parsed = IdParamSchema.safeParse(request.params);
        if (!parsed.success) {
            return reply.code(400).send({ message: parsed.error.message });
        }
        const { domain, id } = parsed.data;
        try {
            const record = await service.getById(domain, id);
            if (!record)
                return reply.code(404).send({ message: `${domain} with id ${id} not found` });
            return reply.send(record);
        }
        catch (err) {
            const message = err.message;
            if (message.includes("Unknown master-data domain")) {
                return reply.code(400).send({ message });
            }
            throw err;
        }
    });
    // POST /api/master-data/:domain
    app.post("/api/master-data/:domain", async (request, reply) => {
        const domainParsed = IdParamSchema.pick({ domain: true }).safeParse(request.params);
        if (!domainParsed.success) {
            return reply.code(400).send({ message: domainParsed.error.message });
        }
        const bodyParsed = CreateBodySchema.safeParse(request.body);
        if (!bodyParsed.success) {
            return reply.code(400).send({ message: bodyParsed.error.message });
        }
        const { domain } = domainParsed.data;
        try {
            const record = await service.create(domain, bodyParsed.data);
            return reply.code(201).send(record);
        }
        catch (err) {
            const message = err.message;
            if (message.includes("Unknown master-data domain")) {
                return reply.code(400).send({ message });
            }
            throw err;
        }
    });
    // PATCH /api/master-data/:domain/:id
    app.patch("/api/master-data/:domain/:id", async (request, reply) => {
        const paramsParsed = IdParamSchema.safeParse(request.params);
        if (!paramsParsed.success) {
            return reply.code(400).send({ message: paramsParsed.error.message });
        }
        const bodyParsed = UpdateBodySchema.safeParse(request.body);
        if (!bodyParsed.success) {
            return reply.code(400).send({ message: bodyParsed.error.message });
        }
        const { domain, id } = paramsParsed.data;
        try {
            const record = await service.update(domain, id, bodyParsed.data);
            if (!record)
                return reply.code(404).send({ message: `${domain} with id ${id} not found` });
            return reply.send(record);
        }
        catch (err) {
            const message = err.message;
            if (message.includes("Unknown master-data domain")) {
                return reply.code(400).send({ message });
            }
            throw err;
        }
    });
    // DELETE /api/master-data/:domain/:id
    app.delete("/api/master-data/:domain/:id", async (request, reply) => {
        const parsed = IdParamSchema.safeParse(request.params);
        if (!parsed.success) {
            return reply.code(400).send({ message: parsed.error.message });
        }
        const { domain, id } = parsed.data;
        try {
            const deleted = await service.softDelete(domain, id);
            if (!deleted)
                return reply.code(404).send({ message: `${domain} with id ${id} not found` });
            return reply.send({ success: true });
        }
        catch (err) {
            const message = err.message;
            if (message.includes("Unknown master-data domain")) {
                return reply.code(400).send({ message });
            }
            throw err;
        }
    });
}
