import { FastifyRequest, FastifyReply } from 'fastify';
import { WeightType } from '@prisma/client';

export const createWeightLog = async (request: FastifyRequest, reply: FastifyReply) => {
    const { id } = request.params as { id: string }; // batchId
    const { weightType, weight, unit, notes } = request.body as any;
    const user = request.user as { id: string };

    const log = await request.server.prisma.weightLog.create({
        data: {
            batchId: id,
            weightType: weightType as WeightType,
            weight: parseFloat(weight),
            unit: unit || 'g',
            notes,
            loggedBy: user.id
        },
        include: {
            user: {
                select: { name: true }
            }
        }
    });

    return log;
};

export const getBatchWeights = async (request: FastifyRequest, reply: FastifyReply) => {
    const { id } = request.params as { id: string };

    const logs = await request.server.prisma.weightLog.findMany({
        where: { batchId: id },
        orderBy: { createdAt: 'desc' },
        include: {
            user: {
                select: { name: true }
            }
        }
    });

    return logs;
};
