// Auto-generated DailyLog routes
import type { FastifyInstance } from "fastify";
import { authenticate } from "../middleware/auth.js";
import { DailyLogService } from "../services/daily_log.js";
import type { CreateDailyLogInput, UpdateDailyLogInput } from "../types/index.js";

const service = new DailyLogService();

export async function daily_logsRoutes(app: FastifyInstance): Promise<void> {
  // All routes require authentication
  app.addHook("onRequest", authenticate);

  // GET /api/daily_logs — list
  app.get("/", async (request, reply) => {
    const items = service.list();
    return { data: items };
  });

  // GET /api/daily_logs/:id — get by id
  app.get<{ Params: { id: string } }>("/:id", async (request, reply) => {
    const item = service.getById(request.params.id);
    if (!item) {
      return reply.status(404).send({ error: "Not Found", message: "DailyLog not found", statusCode: 404 });
    }
    return { data: item };
  });

  // POST /api/daily_logs — create
  app.post<{ Body: CreateDailyLogInput }>("/", async (request, reply) => {
    const item = service.create(request.body);
    return reply.status(201).send({ data: item });
  });

  // PUT /api/daily_logs/:id — update
  app.put<{ Params: { id: string }; Body: UpdateDailyLogInput }>("/:id", async (request, reply) => {
    const item = service.update(request.params.id, request.body);
    if (!item) {
      return reply.status(404).send({ error: "Not Found", message: "DailyLog not found", statusCode: 404 });
    }
    return { data: item };
  });

  // DELETE /api/daily_logs/:id — delete
  app.delete<{ Params: { id: string } }>("/:id", async (request, reply) => {
    const deleted = service.delete(request.params.id);
    if (!deleted) {
      return reply.status(404).send({ error: "Not Found", message: "DailyLog not found", statusCode: 404 });
    }
    return reply.status(204).send();
  });
}
