import { z } from "zod";
import type { Messages } from "@/shared/i18n/config";

// SCRUM-303 — Configuration form. Uses the FLAT PUT shape (tier_healthy /
// tier_at_risk / target_actions / target_logins); the page maps the nested GET
// response onto these on load.
export const createHealthConfigSchema = (t: Messages) =>
  z.object({
    weights: z
      .object({
        activity: z.number().min(0).max(100),
        adoption: z.number().min(0).max(100),
        frequency: z.number().min(0).max(100),
        completeness: z.number().min(0).max(100),
        growth: z.number().min(0).max(100),
      })
      .refine(
        (w) =>
          Math.round(w.activity + w.adoption + w.frequency + w.completeness + w.growth) === 100,
        { message: t.validation.weightsMustTotal100, path: ["activity"] }
      ),
    target_actions: z.number().min(1),
    target_logins: z.number().min(1),
    growth_band: z.number().min(1).max(100),
    tier_healthy: z.number().min(0).max(100),
    tier_at_risk: z.number().min(0).max(100),
    module_thresholds: z.record(z.string(), z.number().min(0)),
    completeness_items: z.array(z.string()),
    alert_thresholds: z.record(z.string(), z.number().min(0)),
    // "custom" is a per-view choice, not a persistable default (backend rejects it).
    default_period: z.enum(["month", "week", "3months"]),
  });

export type HealthConfigFormValues = z.infer<ReturnType<typeof createHealthConfigSchema>>;
