/**
 * Utility types for handling Date to ISO string serialization in HTTP JSON responses
 */

export type DateISOString = string

/**
 * Recursively converts Date types to DateISOString for JSON serialization
 * This handles the fact that Date objects are serialized as ISO strings in HTTP JSON responses
 */
export type DateToString<T> =
  T extends Date ?
    DateISOString :
    T extends (infer U)[] ?
      DateToString<U>[] :
      T extends Array<infer U> ?
        Array<DateToString<U>> :
        T extends Record<string, any> ?
          T extends (...args: any[]) => any ?
            T :
              {
                [K in keyof T]: DateToString<T[K]>
              } :
          T

// type a = {
//   createdAt: Date
//   updatedAt: Date
//   someNested?: {
//     dateField: Date
//     anotherField: string
//   }
//   additionalField: string

// }

// type b = DateToString<a>
/**
 * Converts Drizzle InferSelectModel Date types to DateISOString for HTTP responses
 * Use this for types that will be sent over HTTP as JSON
 */
export type SerializedModel<T> = DateToString<T>

/**
 * Converts Drizzle InferInsertModel Date types to DateISOString for HTTP requests
 * Use this for types that will be received from HTTP as JSON
 */
export type SerializedInsertModel<T> = DateToString<T>

/**
 * Helper type for API responses that contain serialized models
 */
export type ApiResponse<T> = SerializedModel<T>

/**
 * Helper type for API requests that contain serialized models
 */
export type ApiRequest<T> = SerializedInsertModel<T>
