/**
 * Test Management Type Definitions
 * Based on CLQMS API Specification
 */

// Test Types
export type TestType = 'TEST' | 'PARAM' | 'CALC' | 'GROUP' | 'TITLE';
export type ResultType = 'NMRIC' | 'RANGE' | 'TEXT' | 'VSET' | 'NORES';
export type RefType = 'RANGE' | 'THOLD' | 'VSET' | 'TEXT' | 'NOREF';
export type NumRefType = 'REF' | 'CRTC' | 'VAL' | 'RERUN';
export type RangeType = 'RANGE' | 'THOLD';
export type TxtRefType = 'Normal' | 'Abnormal' | 'Critical';
export type SexType = '0' | '1' | '2';
export type SexLabel = 'All' | 'Female' | 'Male';

// Base Test Summary (from list endpoint)
export interface TestSummary {
  TestSiteID: number;
  TestSiteCode: string;
  TestSiteName: string;
  TestType: TestType;
  TestTypeLabel: string;
  SeqScr: number;
  SeqRpt: number;
  VisibleScr: number | string;
  VisibleRpt: number | string;
  CountStat: number;
  StartDate: string;
  EndDate?: string;
  DisciplineID?: number;
  DepartmentID?: number;
  DisciplineName?: string;
  DepartmentName?: string;
  IsActive?: string | number;
}

// Technical Details
export interface TechDetail {
  DisciplineID?: number;
  DepartmentID?: number;
  DisciplineName?: string;
  DepartmentName?: string;
  ResultType?: ResultType;
  RefType?: RefType;
  VSet?: string;
  Unit1?: string;
  Factor?: number;
  Unit2?: string;
  Decimal?: number;
  ReqQty?: number;
  ReqQtyUnit?: string;
  CollReq?: string;
  Method?: string;
  ExpectedTAT?: number;
}

// Calculation Details (extends TechDetail)
export interface CalcDetails extends TechDetail {
  FormulaCode?: string;
}

// Group Member
export interface GroupMember {
  TestSiteID: number;
  TestSiteCode: string;
  TestSiteName: string;
  TestType: TestType;
  Seq?: number;
}

// Test Mapping
export interface TestMapping {
  TestMapID?: number;
  HostType?: string;
  HostID?: string | number;
  HostDataSource?: string;
  HostTestCode?: string;
  HostTestName?: string;
  ClientType?: string;
  ClientID?: string | number;
  ClientDataSource?: string;
  ConDefID?: string | number;
  ClientTestCode?: string;
  ClientTestName?: string;
}

// Numeric Reference Range
export interface RefNumRange {
  RefNumID?: number;
  NumRefType: NumRefType;
  NumRefTypeLabel?: string;
  RangeType: RangeType;
  RangeTypeLabel?: string;
  Sex: SexType;
  SexLabel?: string;
  AgeStart: number;
  AgeEnd: number;
  LowSign?: string;
  LowSignLabel?: string;
  Low?: number;
  HighSign?: string;
  HighSignLabel?: string;
  High?: number;
  Flag?: string;
  Interpretation?: string;
}

// Text Reference Range
export interface RefTxtRange {
  RefTxtID?: number;
  TxtRefType: TxtRefType;
  TxtRefTypeLabel?: string;
  Sex: SexType;
  SexLabel?: string;
  AgeStart: number;
  AgeEnd: number;
  RefTxt: string;
  Flag?: string;
}

// Complete Test Detail (from GET /api/tests/:id)
export interface TestDetail {
  // Base fields
  TestSiteID: number;
  TestSiteCode: string;
  TestSiteName: string;
  TestType: TestType;
  TestTypeLabel: string;
  Description?: string;
  SiteID: number;
  SeqScr: number;
  SeqRpt: number;
  VisibleScr: number | string | boolean;
  VisibleRpt: number | string | boolean;
  CountStat: number | boolean;
  StartDate?: string;
  EndDate?: string;

  // Technical details
  DisciplineID?: number;
  DepartmentID?: number;
  ResultType?: ResultType;
  RefType?: RefType;
  VSet?: string;
  Unit1?: string;
  Factor?: number;
  Unit2?: string;
  Decimal?: number;
  ReqQty?: number;
  ReqQtyUnit?: string;
  CollReq?: string;
  Method?: string;
  ExpectedTAT?: number;

  // Calculated test specific
  FormulaCode?: string;

  // Nested data
  testdefcal?: CalcDetails[];
  testdefgrp?: GroupMember[];
  testmap?: TestMapping[];
  testdeftech?: TechDetail[];
  refnum?: RefNumRange[];
  reftxt?: RefTxtRange[];
}

// Create/Update Test Payload
export interface CreateTestPayload {
  SiteID: number;
  TestSiteCode: string;
  TestSiteName: string;
  TestType: TestType;
  Description?: string;
  SeqScr?: number;
  SeqRpt?: number;
  VisibleScr?: number | boolean;
  VisibleRpt?: number | boolean;
  CountStat?: number | boolean;
  StartDate?: string;

  // Nested details based on TestType
  details?: {
    // Technical (TEST/PARAM/CALC)
    DisciplineID?: number;
    DepartmentID?: number;
    ResultType?: ResultType;
    RefType?: RefType;
    VSet?: string;
    Unit1?: string;
    Factor?: number;
    Unit2?: string;
    Decimal?: number;
    ReqQty?: number;
    ReqQtyUnit?: string;
    CollReq?: string;
    Method?: string;
    ExpectedTAT?: number;

    // CALC only
    FormulaCode?: string;

    // GROUP only
    members?: number[];
  };

  // Reference ranges (TEST/PARAM)
  refnum?: Omit<RefNumRange, 'RefNumID' | 'NumRefTypeLabel' | 'RangeTypeLabel' | 'SexLabel' | 'LowSignLabel' | 'HighSignLabel'>[];
  reftxt?: Omit<RefTxtRange, 'RefTxtID' | 'TxtRefTypeLabel' | 'SexLabel'>[];

  // Mappings (all types)
  testmap?: TestMapping[];
}

// Update Test Payload (includes TestSiteID)
export interface UpdateTestPayload extends CreateTestPayload {
  TestSiteID: number;
}

// Form State (for Svelte 5 runes)
export interface TestFormState {
  TestSiteID?: number;
  TestSiteCode: string;
  TestSiteName: string;
  TestType: TestType;
  Description?: string;
  SiteID: number;
  SeqScr: number;
  SeqRpt: number;
  VisibleScr: boolean;
  VisibleRpt: boolean;
  CountStat: boolean;
  StartDate?: string;
  details: {
    DisciplineID?: number;
    DepartmentID?: number;
    ResultType?: ResultType;
    RefType?: RefType;
    VSet?: string;
    Unit1?: string;
    Factor?: number;
    Unit2?: string;
    Decimal?: number;
    ReqQty?: number;
    ReqQtyUnit?: string;
    CollReq?: string;
    Method?: string;
    ExpectedTAT?: number;
    FormulaCode?: string;
    members?: number[];
  };
  refnum: RefNumRange[];
  reftxt: RefTxtRange[];
  testmap: TestMapping[];
}

// Tab Configuration
export interface TabConfig {
  id: string;
  label: string;
  icon: string;
  isVisible: (testType: TestType) => boolean;
}

// Test Type Configuration (for UI display)
export interface TestTypeConfig {
  label: string;
  badgeClass: string;
  description: string;
}

// API Response Types
export interface ApiResponse<T> {
  status: 'success' | 'created' | 'error';
  message: string;
  data: T;
}

export interface TestListResponse extends ApiResponse<TestSummary[]> {
  pagination?: {
    page: number;
    perPage: number;
    total: number;
  };
}

export interface TestDetailResponse extends ApiResponse<TestDetail> {}

export interface CreateTestResponse extends ApiResponse<{ TestSiteId: number }> {
  status: 'created';
}

export interface UpdateTestResponse extends ApiResponse<{ TestSiteId: number }> {}

export interface DeleteTestResponse extends ApiResponse<{ TestSiteId: number; EndDate: string }> {}

// Filter Options
export interface TestFilterOptions {
  TestType?: TestType;
  VisibleScr?: number;
  VisibleRpt?: number;
  search?: string;
}
