import {
  CreateCalculationRuleSchema,
  UpdateCalculationRuleSchema,
} from "@capakraken/shared";
import { z } from "zod";
import { findUniqueOrThrow } from "../db/helpers.js";
import { PROJECT_BRIEF_SELECT } from "../db/selects.js";
import { createAuditEntry } from "../lib/audit.js";
import type { TRPCContext } from "../trpc.js";
import {
  buildCalculationRuleCreateData,
  buildCalculationRuleUpdateData,
} from "./calculation-rule-support.js";

export const CalculationRuleIdInputSchema = z.object({
  id: z.string(),
});

export const CreateCalculationRuleInputSchema = CreateCalculationRuleSchema;
export const UpdateCalculationRuleInputSchema = UpdateCalculationRuleSchema;

type CalculationRuleProcedureContext = Pick<TRPCContext, "db" | "dbUser">;

function withAuditUser(userId: string | undefined) {
  return userId ? { userId } : {};
}

export async function listCalculationRules(ctx: CalculationRuleProcedureContext) {
  return ctx.db.calculationRule.findMany({
    orderBy: [{ priority: "desc" }, { name: "asc" }],
    include: { project: { select: PROJECT_BRIEF_SELECT } },
  });
}

export async function getCalculationRuleById(
  ctx: CalculationRuleProcedureContext,
  input: z.infer<typeof CalculationRuleIdInputSchema>,
) {
  return findUniqueOrThrow(
    ctx.db.calculationRule.findUnique({
      where: { id: input.id },
      include: { project: { select: PROJECT_BRIEF_SELECT } },
    }),
    "CalculationRule",
  );
}

export async function listActiveCalculationRules(ctx: CalculationRuleProcedureContext) {
  return ctx.db.calculationRule.findMany({
    where: { isActive: true },
    orderBy: [{ priority: "desc" }],
  });
}

export async function createCalculationRule(
  ctx: CalculationRuleProcedureContext,
  input: z.infer<typeof CreateCalculationRuleInputSchema>,
) {
  const rule = await ctx.db.calculationRule.create({
    data: buildCalculationRuleCreateData(input),
  });

  void createAuditEntry({
    db: ctx.db,
    entityType: "CalculationRule",
    entityId: rule.id,
    entityName: rule.name,
    action: "CREATE",
    ...withAuditUser(ctx.dbUser?.id),
    after: rule as unknown as Record<string, unknown>,
    source: "ui",
  });

  return rule;
}

export async function updateCalculationRule(
  ctx: CalculationRuleProcedureContext,
  input: z.infer<typeof UpdateCalculationRuleInputSchema>,
) {
  const { id, ...data } = input;
  const before = await findUniqueOrThrow(
    ctx.db.calculationRule.findUnique({ where: { id } }),
    "CalculationRule",
  );

  const updated = await ctx.db.calculationRule.update({
    where: { id },
    data: buildCalculationRuleUpdateData(data),
  });

  void createAuditEntry({
    db: ctx.db,
    entityType: "CalculationRule",
    entityId: id,
    entityName: updated.name,
    action: "UPDATE",
    ...withAuditUser(ctx.dbUser?.id),
    before: before as unknown as Record<string, unknown>,
    after: updated as unknown as Record<string, unknown>,
    source: "ui",
  });

  return updated;
}

export async function deleteCalculationRule(
  ctx: CalculationRuleProcedureContext,
  input: z.infer<typeof CalculationRuleIdInputSchema>,
) {
  const rule = await findUniqueOrThrow(
    ctx.db.calculationRule.findUnique({ where: { id: input.id } }),
    "CalculationRule",
  );
  await ctx.db.calculationRule.delete({ where: { id: input.id } });

  void createAuditEntry({
    db: ctx.db,
    entityType: "CalculationRule",
    entityId: input.id,
    entityName: rule.name,
    action: "DELETE",
    ...withAuditUser(ctx.dbUser?.id),
    before: rule as unknown as Record<string, unknown>,
    source: "ui",
  });

  return { success: true };
}
