import * as z from 'zod';

export const AnalysisOrderIdSchema = z.object({
  id: z.number(),
  medusa_order_id: z.string(),
  analysis_element_ids: z.array(z.number()).nullable(),
});
export type AnalysisOrderId = z.infer<typeof AnalysisOrderIdSchema>;

export const DoctorFeedbackSchema = z
  .object({
    id: z.number(),
    analysis_order_id: z.number(),
    doctor_user_id: z.string().nullable(),
    user_id: z.string(),
    value: z.string().nullable(),
    created_at: z.string(),
    updated_at: z.string().nullable(),
    created_by: z.string(),
    updated_by: z.string().nullable(),
    status: z.string(),
  })
  .optional()
  .nullable();
export type DoctorFeedback = z.infer<typeof DoctorFeedbackSchema>;

export const OrderSchema = z.object({
  title: z.string(),
  isPackage: z.boolean(),
  analysisOrderId: z.number(),
});
export type Order = z.infer<typeof OrderSchema>;

export const PatientSchema = z.object({
  userId: z.string(),
  accountId: z.string(),
  firstName: z.string(),
  lastName: z.string().nullable(),
  personalCode: z.string().nullable(),
  phone: z.string().nullable(),
  email: z.string().nullable(),
  height: z.number().optional().nullable(),
  weight: z.number().optional().nullable(),
  preferred_locale: z.string().nullable(),
});
export type Patient = z.infer<typeof PatientSchema>;

export const AnalysisResponsesSchema = z.object({
  user_id: z.string(),
  analysis_order_id: AnalysisOrderIdSchema,
});
export type AnalysisResponses = z.infer<typeof AnalysisResponsesSchema>;

export const AnalysisResponseSchema = z.object({
  id: z.number(),
  analysis_response_id: z.number(),
  analysis_element_original_id: z.string(),
  unit: z.string().nullable(),
  response_value: z.number(),
  response_time: z.string(),
  norm_upper: z.number().nullable(),
  norm_upper_included: z.boolean().nullable(),
  norm_lower: z.number().nullable(),
  norm_lower_included: z.boolean().nullable(),
  norm_status: z.number().nullable(),
  created_at: z.string(),
  updated_at: z.string().nullable(),
  analysis_name: z.string().nullable(),
  analysis_responses: AnalysisResponsesSchema,
  comment: z.string().nullable(),
  latestPreviousAnalysis: z
    .object({
      id: z.number(),
      analysis_response_id: z.number(),
      analysis_element_original_id: z.string(),
      unit: z.string().nullable(),
      response_value: z.number(),
      response_time: z.string(),
      norm_upper: z.number().nullable(),
      norm_upper_included: z.boolean().nullable(),
      norm_lower: z.number().nullable(),
      norm_lower_included: z.boolean().nullable(),
      norm_status: z.number().nullable(),
      created_at: z.string(),
      updated_at: z.string().nullable(),
      analysis_name: z.string().nullable(),
      comment: z.string().nullable(),
    })
    .optional()
    .nullable(),
});
export type AnalysisResponse = z.infer<typeof AnalysisResponseSchema>;

export const AnalysisResultDetailsSchema = z.object({
  analysisResponse: z.array(AnalysisResponseSchema),
  order: OrderSchema,
  doctorFeedback: DoctorFeedbackSchema,
  patient: PatientSchema,
});
export type AnalysisResultDetails = z.infer<typeof AnalysisResultDetailsSchema>;
