/**
 * ID Mapping Domain - Schemas
 */

import { z } from "zod";
import type {
  CreateMappingRequest,
  UpdateMappingRequest,
  UserIdMapping,
} from "./contract";

export const createMappingRequestSchema: z.ZodType<CreateMappingRequest> = z.object({
  userId: z.string().uuid(),
  whmcsClientId: z.number().int().positive(),
  sfAccountId: z
    .string()
    .min(1, "Salesforce account ID must be at least 1 character")
    .optional(),
});

export const updateMappingRequestSchema: z.ZodType<UpdateMappingRequest> = z.object({
  whmcsClientId: z.number().int().positive().optional(),
  sfAccountId: z
    .string()
    .min(1, "Salesforce account ID must be at least 1 character")
    .optional(),
});

export const userIdMappingSchema: z.ZodType<UserIdMapping> = z.object({
  id: z.string().uuid(),
  userId: z.string().uuid(),
  whmcsClientId: z.number().int().positive(),
  sfAccountId: z.string().nullable().optional(),
  createdAt: z.union([z.string(), z.date()]),
  updatedAt: z.union([z.string(), z.date()]),
});

// ============================================================================
// Query and Filter Schemas
// ============================================================================

export const mappingSearchFiltersSchema = z.object({
  userId: z.string().uuid().optional(),
  whmcsClientId: z.number().int().positive().optional(),
  sfAccountId: z.string().optional(),
  hasWhmcsMapping: z.boolean().optional(),
  hasSfMapping: z.boolean().optional(),
});

// ============================================================================
// Stats and Analytics Schemas
// ============================================================================

export const mappingStatsSchema = z.object({
  totalMappings: z.number().int().nonnegative(),
  whmcsMappings: z.number().int().nonnegative(),
  salesforceMappings: z.number().int().nonnegative(),
  completeMappings: z.number().int().nonnegative(),
  orphanedMappings: z.number().int().nonnegative(),
});

// ============================================================================
// Bulk Operations Schemas
// ============================================================================

export const bulkMappingOperationSchema = z.object({
  operation: z.enum(["create", "update", "delete"]),
  mappings: z.union([
    z.array(createMappingRequestSchema),
    z.array(updateMappingRequestSchema),
    z.array(z.string().uuid()),
  ]),
});

export const bulkMappingResultSchema = z.object({
  successful: z.number().int().nonnegative(),
  failed: z.number().int().nonnegative(),
  errors: z.array(
    z.object({
      index: z.number().int().nonnegative(),
      error: z.string(),
      data: z.unknown(),
    })
  ),
});

// ============================================================================
// Inferred Types from Schemas (Schema-First Approach)
// ============================================================================

export type MappingSearchFilters = z.infer<typeof mappingSearchFiltersSchema>;
export type MappingStats = z.infer<typeof mappingStatsSchema>;
export type BulkMappingOperation = z.infer<typeof bulkMappingOperationSchema>;
export type BulkMappingResult = z.infer<typeof bulkMappingResultSchema>;
