import { z } from 'zod';

export const statesSchema = z
  .object({
    id: z.string(),
    name: z.string(),
    terminal: z.boolean(),
  })
  .passthrough();

export const serviceItemsSchema = z
  .object({
    id: z.string(),
    name: z.string(),
  })
  .passthrough();

export const userSchema = z
  .object({
    id: z.number(),
    firstName: z.string(),
    lastName: z.string(),
    // Optional so URL-minimized payloads can drop it (full CDN URLs add up
    // quickly across many selected users and risk the ~2 KB URL limit).
    // Keeps backward compatibility with old bookmarked URLs that still embed it.
    profilePicture: z.string().nullish(),
    employeeInternalId: z.string(),
  })
  .passthrough();

export const collaboratorTypeSchema = z
  .object({
    title: z.string(),
    id: z.number(),
  })
  .passthrough();

export const agentGroupSchema = z
  .object({
    id: z.string(),
    name: z.string(),
  })
  .passthrough();

// Schema for the agent workspace ticket drawer filters
export const filterFormValuesSchema = z.object({
  statesIn: z.array(statesSchema).default([]),
  serviceItemsIn: z.array(serviceItemsSchema).default([]),
  creationDateFrom: z.date().nullable().default(null),
  creationDateTo: z.date().nullable().default(null),
  initiatorsIn: z.array(userSchema).default([]),
  initiatorCollaboratorTypesIn: z.array(collaboratorTypeSchema).default([]),
  agentsIn: z.array(userSchema).default([]),
});

const dateOptionsSchema = z
  .object({
    lastSevenDays: z.boolean().default(false),
    lastFourteenDays: z.boolean().default(false),
    lastThirtyDays: z.boolean().default(false),
    dateRange: z.boolean().default(false),
  })
  .optional();

// Schema for the Approvals page Pending tab. Used in both the agent
// workspace `Approvals.tsx` and the service portal `ApprovalsPortal.tsx`.
export const pendingApprovalsFiltersSchema = z.object({
  parentTaskCatalogItemsIn: z.array(serviceItemsSchema).optional(),
  parentTaskInitiatorsIn: z.array(userSchema).optional(),
  createdAtFrom: z.date().nullable().default(null),
  createdAtTo: z.date().nullable().default(null),
  dateOptions: dateOptionsSchema,
});

// Schema for the Approvals page History tab. Used in both the agent
// workspace `Approvals.tsx` and the service portal `ApprovalsPortal.tsx`.
export const historyApprovalsFiltersSchema = z.object({
  parentTaskCatalogItemsIn: z.array(serviceItemsSchema).optional(),
  statesIn: z.array(statesSchema).optional(),
  parentTaskInitiatorsIn: z.array(userSchema).optional(),
  completedAtFrom: z.date().nullable().default(null),
  completedAtTo: z.date().nullable().default(null),
  dateOptions: dateOptionsSchema,
});

export type PendingApprovalsFormValues = z.infer<
  typeof pendingApprovalsFiltersSchema
>;
export type HistoryApprovalsFormValues = z.infer<
  typeof historyApprovalsFiltersSchema
>;

// Schema for the subtasks page drawer filters. Mirrors `subtasksFormSchema`
// in form/schemas.ts but without form-only `.refine` validations so it stays
// a pure ZodObject usable by createFilterParser.
export const subtasksFiltersSchema = z.object({
  statesIn: z.array(statesSchema).optional(),
  parentTaskCatalogItemsIn: z.array(serviceItemsSchema).optional(),
  parentTaskInitiatorsIn: z.array(userSchema).optional(),
  createdAtFrom: z.date().nullable().default(null),
  createdAtTo: z.date().nullable().default(null),
  dateOptions: z
    .object({
      lastSevenDays: z.boolean().default(false),
      lastFourteenDays: z.boolean().default(false),
      lastThirtyDays: z.boolean().default(false),
      dateRange: z.boolean().default(false),
    })
    .optional(),
});

// Schema for dashboard filters (CollaboratorPanelFilters)
export const collaboratorPanelFiltersSchema = z.object({
  serviceItemsIn: z.array(serviceItemsSchema).optional(),
  agentsIn: z.array(userSchema).optional(),
  agentGroupsIn: z.array(agentGroupSchema).optional(),
  creationDateFrom: z.date().nullable().optional(),
  creationDateTo: z.date().nullable().optional(),
  dateOptions: z.record(z.boolean()).optional(),
});

// Schema for coordinator task filters
export const coordinatorTaskFiltersSchema = z.object({
  serviceItemsIn: z.array(serviceItemsSchema).optional(),
  statesIn: z.array(statesSchema).optional(),
  agentsIn: z.array(userSchema).optional(),
  agentGroupsIn: z.array(agentGroupSchema).optional(),
  initiatorsIn: z.array(userSchema).optional(),
  collaboratorTypesIn: z.array(collaboratorTypeSchema).optional(),
  creationDateFrom: z.date().nullable().optional(),
  creationDateTo: z.date().nullable().optional(),
  dateOptions: z.record(z.boolean()).optional(),
});

export type FilterType = 'agent' | 'coordinator';
