import type { Prisma } from "@capakraken/db";
import {
  CreateCalculationRuleSchema,
  UpdateCalculationRuleSchema,
} from "@capakraken/shared";
import { z } from "zod";

type CreateCalculationRuleInput = z.infer<typeof CreateCalculationRuleSchema>;
type UpdateCalculationRuleInput = Omit<z.infer<typeof UpdateCalculationRuleSchema>, "id">;

export function buildCalculationRuleCreateData(
  input: CreateCalculationRuleInput,
): Prisma.CalculationRuleUncheckedCreateInput {
  return {
    name: input.name,
    triggerType: input.triggerType,
    costEffect: input.costEffect,
    chargeabilityEffect: input.chargeabilityEffect,
    ...(input.description !== undefined ? { description: input.description } : {}),
    ...(input.projectId !== undefined ? { projectId: input.projectId } : {}),
    ...(input.orderType !== undefined ? { orderType: input.orderType as never } : {}),
    ...(input.costReductionPercent !== undefined ? { costReductionPercent: input.costReductionPercent } : {}),
    priority: input.priority,
    isActive: input.isActive,
  };
}

export function buildCalculationRuleUpdateData(
  input: UpdateCalculationRuleInput,
): Prisma.CalculationRuleUncheckedUpdateInput {
  return {
    ...(input.name !== undefined ? { name: input.name } : {}),
    ...(input.description !== undefined ? { description: input.description } : {}),
    ...(input.triggerType !== undefined ? { triggerType: input.triggerType } : {}),
    ...(input.projectId !== undefined ? { projectId: input.projectId } : {}),
    ...(input.orderType !== undefined ? { orderType: input.orderType as never } : {}),
    ...(input.costEffect !== undefined ? { costEffect: input.costEffect } : {}),
    ...(input.costReductionPercent !== undefined ? { costReductionPercent: input.costReductionPercent } : {}),
    ...(input.chargeabilityEffect !== undefined ? { chargeabilityEffect: input.chargeabilityEffect } : {}),
    ...(input.priority !== undefined ? { priority: input.priority } : {}),
    ...(input.isActive !== undefined ? { isActive: input.isActive } : {}),
  };
}
