import z from 'zod';

import { Database } from '@kit/supabase/database';

export const doctorJobSelectSchema = z.object({
  userId: z.uuid(),
  analysisOrderId: z.number(),
});
export type DoctorJobSelect = z.infer<typeof doctorJobSelectSchema>;

export const doctorJobUnselectSchema = z.object({
  analysisOrderId: z.number(),
});
export type DoctorJobUnselect = z.infer<typeof doctorJobUnselectSchema>;

export const FeedbackStatus = z.enum(['STARTED', 'DRAFT', 'COMPLETED']);
export const doctorAnalysisFeedbackFormSchema = z.object({
  feedbackValue: z.string().min(10, { message: 'doctor:feedbackLengthError' }),
  userId: z.string().uuid(),
});
export type DoctorAnalysisFeedbackForm = z.infer<
  typeof doctorAnalysisFeedbackFormSchema
>;
export const doctorAnalysisFeedbackSchema = z.object({
  feedbackValue: z.string(),
  userId: z.string().uuid(),
  analysisOrderId: z.number(),
  status: FeedbackStatus,
});

export type DoctorAnalysisFeedback = z.infer<
  typeof doctorAnalysisFeedbackSchema
>;

export type DoctorAnalysisFeedbackTable =
  Database['medreport']['Tables']['doctor_analysis_feedback']['Row'];

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 ElementSchema = 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(),
});
export type Element = z.infer<typeof ElementSchema>;

export const FeedbackSchema = z.object({
  analysis_order_id: z.number(),
  user_id: z.string(),
  doctor_user_id: z.string().nullable(),
  status: z.string(),
});
export type Feedback = z.infer<typeof FeedbackSchema>;

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

export const AccountSchema = z.object({
  name: z.string(),
  last_name: z.string().nullable(),
  id: z.string(),
  primary_owner_user_id: z.string(),
  preferred_locale: z.string().nullable(),
});
export type Account = z.infer<typeof AccountSchema>;

export const ResponseTableSchema = z.object({
  id: z.number(),
  analysis_order_id: AnalysisOrderIdSchema,
  order_number: z.string(),
  order_status: z.string(),
  user_id: z.string(),
  created_at: z.string(),
  updated_at: z.string().nullable(),
  elements: z.array(ElementSchema),
  firstSampleGivenAt: z.string().nullable(),
  patient: AccountSchema,
  order: OrderSchema,
  doctor: AccountSchema.optional().nullable(),
  feedback: FeedbackSchema.optional(),
});
export type ResponseTable = z.infer<typeof ResponseTableSchema>;

export type AnalysisResponseBase = {
  analysis_order_id: number & {
    id: number;
    medusa_order_id: string;
    analysis_element_ids: number[] | null;
  };
  created_at: string;
  id: number;
  order_number: string;
  order_status: Database['medreport']['Tables']['analysis_orders']['Row']['status'];
  updated_at: string | null;
  user_id: string;
};

export interface PaginationParams {
  page?: number;
  pageSize?: number;
}

export interface PaginatedData<T> {
  data: T[];
  pagination: {
    currentPage: number;
    totalPages: number;
    totalCount: number;
    pageSize: number;
  };
}

export interface ServerActionResponse<T> {
  success: boolean;
  data?: T;
  error?: string;
}
