import type {
  AnalysisOrderStatus,
  NormStatus,
} from '@kit/shared/types/medipost-analysis';
import type { Database } from '@kit/supabase/database';

import type { AnalysisOrder } from './analysis-orders';

export type UserAnalysisElement =
  Database['medreport']['Tables']['analysis_response_elements']['Row'];
export type UserAnalysisResponse =
  Database['medreport']['Tables']['analysis_responses']['Row'] & {
    elements: UserAnalysisElement[];
  };
export type UserAnalysis = UserAnalysisResponse[];

export type AnalysisResultsQuery = {
  id: number;
  analysis_order_id: number;
  order_number: string;
  order_status: string;
  user_id: string;
  created_at: string;
  updated_at: string | null;
  elements: {
    unit: string;
    norm_lower: number;
    norm_upper: number;
    norm_status: number;
    analysis_name: string;
    response_time: string;
    response_value: number;
    response_value_is_negative: boolean;
    response_value_is_within_norm: boolean;
    norm_lower_included: boolean;
    norm_upper_included: boolean;
    status: string;
    analysis_element_original_id: string;
    original_response_element: {
      UuringuElement: {
        UuringIdOID: string;
        UuringId: string;
        TLyhend: string;
        KNimetus: string;
        UuringNimi: string;
        UuringuKommentaar: string | null;
        TellijaUuringId: number;
        TeostajaUuringId: string;
        UuringOlek: keyof typeof AnalysisOrderStatus;
        Mootyhik: string | null;
        Kood: {
          HkKood: number;
          HkKoodiKordaja: number;
          Koefitsient: number;
          Hind: number;
        };
        UuringuVastus: {
          VastuseVaartus: string;
          VastuseAeg: string;
          NormiStaatus: keyof typeof NormStatus;
          ProoviJarjenumber: number;
        };
        UuringuTaitjaAsutuseJnr: number;
      };
      UuringuKommentaar: string | null;
    };
  }[];
  order: {
    status: string;
    medusa_order_id: string;
    created_at: string;
  };
  summary: {
    id: number;
    value: string;
    status: string;
    user_id: string;
    created_at: string;
    created_by: string;
    updated_at: string | null;
    updated_by: string;
    doctor_user_id: string | null;
    analysis_order_id: number;
    doctor_analysis_feedback: {
      id: number;
      status: string;
      user_id: string;
      created_at: string;
      created_by: string;
    }[];
  } | null;
};

export type AnalysisResultsDetailsElementNested = {
  analysisElementOriginalId: string;
  analysisName?: string;
} & Pick<
  AnalysisResultDetailsElementResults,
  | 'unit'
  | 'normLower'
  | 'normUpper'
  | 'normStatus'
  | 'responseTime'
  | 'responseValue'
  | 'responseValueIsNegative'
  | 'responseValueIsWithinNorm'
  | 'normLowerIncluded'
  | 'normUpperIncluded'
  | 'status'
  | 'analysisElementOriginalId'
>;

export enum AnalysisResultLevel {
  NORMAL = 0,
  WARNING = 1,
  CRITICAL = 2,
}

export enum AnalysisStatus {
  QUEUED = 1,
  PENDING = 2,
  ONGOING = 3,
  COMPLETED = 4,
  REFUSED = 5,
  CANCELLED = 6,
}

export type AnalysisResultDetailsElementResults = {
  unit: string | null;
  normLower: number | null;
  normUpper: number | null;
  normStatus: AnalysisResultLevel | null;
  responseTime: string | null;
  responseValue: number | null;
  responseValueIsNegative: boolean | null;
  responseValueIsWithinNorm: boolean | null;
  normLowerIncluded: boolean;
  normUpperIncluded: boolean;
  status: AnalysisStatus;
  analysisElementOriginalId: string;
  nestedElements: AnalysisResultsDetailsElementNested[];
  labComment?: string | null;
};

export type AnalysisResultDetailsElement = {
  analysisIdOriginal: string;
  isWaitingForResults: boolean;
  analysisName: string;
  results?: AnalysisResultDetailsElementResults;
};

export type AnalysisResultDetailsMapped = {
  id: number;
  order: {
    medusaOrderId: string;
    createdAt: string;
  } & Pick<AnalysisOrder, 'status'>;
  orderedAnalysisElements: AnalysisResultDetailsElement[];
  summary: {
    id: number;
    status: string;
    user_id: string;
    created_at: string;
    created_by: string;
    value?: string;
  } | null;
};
