import { z } from "zod";
import { ResourceType } from "../types/enums.js";

export const WeekdayAvailabilitySchema = z.object({
  monday: z.number().min(0).max(24),
  tuesday: z.number().min(0).max(24),
  wednesday: z.number().min(0).max(24),
  thursday: z.number().min(0).max(24),
  friday: z.number().min(0).max(24),
  saturday: z.number().min(0).max(24).optional(),
  sunday: z.number().min(0).max(24).optional(),
});

export const SkillEntrySchema = z.object({
  skill: z.string().min(1).max(100),
  category: z.string().max(100).optional(),
  proficiency: z.union([z.literal(1), z.literal(2), z.literal(3), z.literal(4), z.literal(5)]),
  yearsExperience: z.number().min(0).max(50).optional(),
  certified: z.boolean().optional(),
  isMainSkill: z.boolean().optional(),
});

export const CreateResourceSchema = z.object({
  eid: z.string().min(1).max(50),
  displayName: z.string().min(1).max(200),
  email: z.string().email(),
  chapter: z.string().max(100).optional(),
  lcrCents: z.number().int().min(0),
  ucrCents: z.number().int().min(0),
  currency: z.string().length(3).default("EUR"),
  chargeabilityTarget: z.number().min(0).max(100).default(80),
  availability: WeekdayAvailabilitySchema.default({
    monday: 8,
    tuesday: 8,
    wednesday: 8,
    thursday: 8,
    friday: 8,
  }),
  skills: z.array(SkillEntrySchema).default([]),
  dynamicFields: z.record(z.string(), z.unknown()).default({}),
  blueprintId: z.string().optional(),
  portfolioUrl: z.string().url().optional().or(z.literal("")),
  roleId: z.string().optional(),
  postalCode: z.string().max(10).optional(),
  federalState: z.string().max(5).optional(),
  countryId: z.string().optional(),
  metroCityId: z.string().optional(),
  orgUnitId: z.string().optional(),
  managementLevelGroupId: z.string().optional(),
  managementLevelId: z.string().optional(),
  resourceType: z.nativeEnum(ResourceType).optional(),
  chgResponsibility: z.boolean().optional(),
  rolledOff: z.boolean().optional(),
  departed: z.boolean().optional(),
  enterpriseId: z.string().max(100).optional(),
  clientUnitId: z.string().optional(),
  fte: z.number().min(0.01).max(1).optional(),
});

export const UpdateResourceSchema = CreateResourceSchema.partial().extend({
  isActive: z.boolean().optional(),
});

export type CreateResourceInput = z.infer<typeof CreateResourceSchema>;
export type UpdateResourceInput = z.infer<typeof UpdateResourceSchema>;
export type WeekdayAvailabilityInput = z.infer<typeof WeekdayAvailabilitySchema>;
export type SkillEntryInput = z.infer<typeof SkillEntrySchema>;
