import type { Prisma } from "@capakraken/db";
import {
  CreateHolidayCalendarEntrySchema,
  CreateHolidayCalendarSchema,
  UpdateHolidayCalendarEntrySchema,
  UpdateHolidayCalendarSchema,
} from "@capakraken/shared";
import { z } from "zod";

type CreateHolidayCalendarInput = z.infer<typeof CreateHolidayCalendarSchema>;
type UpdateHolidayCalendarInput = z.infer<typeof UpdateHolidayCalendarSchema>;
type CreateHolidayCalendarEntryInput = z.infer<typeof CreateHolidayCalendarEntrySchema>;
type UpdateHolidayCalendarEntryInput = z.infer<typeof UpdateHolidayCalendarEntrySchema>;

export const holidayCalendarEntryOrderBy = [
  { date: "asc" },
  { name: "asc" },
] as const;

export const holidayCalendarDetailInclude = {
  country: { select: { id: true, code: true, name: true } },
  metroCity: { select: { id: true, name: true } },
  entries: { orderBy: holidayCalendarEntryOrderBy },
} as const;

export const holidayCalendarListInclude = {
  ...holidayCalendarDetailInclude,
  _count: { select: { entries: true } },
} as const;

export function buildHolidayCalendarCreateData(
  input: CreateHolidayCalendarInput & {
    normalizedScope: {
      stateCode: string | null;
      metroCityId: string | null;
    };
  },
): Prisma.HolidayCalendarUncheckedCreateInput {
  return {
    name: input.name,
    scopeType: input.scopeType,
    countryId: input.countryId,
    ...(input.normalizedScope.stateCode ? { stateCode: input.normalizedScope.stateCode } : {}),
    ...(input.normalizedScope.metroCityId ? { metroCityId: input.normalizedScope.metroCityId } : {}),
    isActive: input.isActive ?? true,
    priority: input.priority ?? 0,
  };
}

export function buildHolidayCalendarUpdateData(input: {
  data: UpdateHolidayCalendarInput;
  resolvedScope: {
    stateCode: string | null;
    metroCityId: string | null;
  };
}): Prisma.HolidayCalendarUncheckedUpdateInput {
  return {
    ...(input.data.name !== undefined ? { name: input.data.name } : {}),
    ...(input.data.stateCode !== undefined ? { stateCode: input.resolvedScope.stateCode } : {}),
    ...(input.data.metroCityId !== undefined ? { metroCityId: input.resolvedScope.metroCityId } : {}),
    ...(input.data.isActive !== undefined ? { isActive: input.data.isActive } : {}),
    ...(input.data.priority !== undefined ? { priority: input.data.priority } : {}),
  };
}

export function buildHolidayCalendarEntryCreateData(input: {
  data: CreateHolidayCalendarEntryInput;
  date: Date;
}): Prisma.HolidayCalendarEntryUncheckedCreateInput {
  return {
    holidayCalendarId: input.data.holidayCalendarId,
    date: input.date,
    name: input.data.name,
    isRecurringAnnual: input.data.isRecurringAnnual ?? false,
    ...(input.data.source ? { source: input.data.source } : {}),
  };
}

export function buildHolidayCalendarEntryUpdateData(input: {
  data: UpdateHolidayCalendarEntryInput;
  date: Date;
}): Prisma.HolidayCalendarEntryUncheckedUpdateInput {
  return {
    ...(input.data.date !== undefined ? { date: input.date } : {}),
    ...(input.data.name !== undefined ? { name: input.data.name } : {}),
    ...(input.data.isRecurringAnnual !== undefined ? { isRecurringAnnual: input.data.isRecurringAnnual } : {}),
    ...(input.data.source !== undefined ? { source: input.data.source ?? null } : {}),
  };
}
