// forcing reload
import prisma from '../lib/prisma';

export class PlanService {
    static async getPlans(userId: string) {
        const plans = await prisma.workoutPlan.findMany({
            where: { userId },
            include: {
                planExercises: {
                    include: { exercise: true },
                    orderBy: { order: 'asc' }
                }
            },
            orderBy: { createdAt: 'desc' }
        });

        return plans.map((p: any) => ({
            ...p,
            steps: p.planExercises.map((pe: any) => ({
                id: pe.id,
                exerciseId: pe.exerciseId,
                exerciseName: pe.exercise.name,
                exerciseType: pe.exercise.type,
                isWeighted: pe.isWeighted,
                restTimeSeconds: pe.restTime
            }))
        }));
    }

    static async savePlan(userId: string, data: any) {
        const { id, name, description, steps } = data;

        await prisma.$transaction(async (tx) => {
            let plan = await tx.workoutPlan.findUnique({ where: { id } });

            if (plan) {
                await tx.workoutPlan.update({
                    where: { id },
                    data: { name, description }
                });
                await tx.planExercise.deleteMany({ where: { planId: id } });
            } else {
                await tx.workoutPlan.create({
                    data: {
                        id,
                        userId,
                        name,
                        description
                    }
                });
            }

            if (steps && steps.length > 0) {
                await tx.planExercise.createMany({
                    data: steps.map((step: any, index: number) => ({
                        planId: id,
                        exerciseId: step.exerciseId,
                        order: index,
                        isWeighted: step.isWeighted || false,
                        restTime: step.restTimeSeconds
                    }))
                });
            }
        });

        const savedPlan = await prisma.workoutPlan.findUnique({
            where: { id },
            include: {
                planExercises: {
                    include: { exercise: true },
                    orderBy: { order: 'asc' }
                }
            }
        });

        if (!savedPlan) throw new Error("Plan failed to save");

        return {
            ...savedPlan,
            steps: savedPlan.planExercises.map((pe: any) => ({
                id: pe.id,
                exerciseId: pe.exerciseId,
                exerciseName: pe.exercise.name,
                exerciseType: pe.exercise.type,
                isWeighted: pe.isWeighted,
                restTimeSeconds: pe.restTime
            }))
        };
    }

    static async deletePlan(userId: string, id: string) {
        await prisma.workoutPlan.delete({
            where: { id, userId }
        });
    }
}
