import { NutritionTargetModel } from '../../schemas/nutrition-target.schema.js';

export class NutritionTargetRepository {
  public async findByUser(userId: string, householdId: string) {
    return NutritionTargetModel.findOne({ userId, householdId, isActive: true }).lean().exec();
  }

  public async findAllByUser(userId: string, householdId: string) {
    return NutritionTargetModel.find({ userId, householdId }).sort({ createdAt: -1 }).lean().exec();
  }

  public async create(data: Record<string, unknown>) {
    const doc = new NutritionTargetModel(data);
    const saved = await doc.save();
    return saved.toObject();
  }

  public async deactivateAllForUser(userId: string, householdId: string) {
    return NutritionTargetModel.updateMany(
      { userId, householdId, isActive: true },
      { $set: { isActive: false } },
    ).exec();
  }

  public async update(
    id: string,
    userId: string,
    householdId: string,
    data: Record<string, unknown>,
  ) {
    return NutritionTargetModel.findOneAndUpdate(
      { _id: id, userId, householdId },
      { $set: data },
      { new: true, lean: true },
    ).exec();
  }
}
