-- Create documentFieldExtractionArrayFields table for 1:N array fields
CREATE TABLE documentFieldExtractionArrayFields (
    id uuid PRIMARY KEY DEFAULT uuid_generate_v7(),
    fieldExtractionId uuid NOT NULL,
    arrayIndex smallint NOT NULL,

    -- Array-value fields (112 fields total - 1:N relationship)
    -- All fields at same arrayIndex form one "row" of the array

    -- Exhibit information
    exhibitTitle text,
    exhibitPage text,

    -- Reimbursement provider information
    reimbProvTin text,
    reimbProvNpi text,
    reimbProvName text,
    reimbEffectiveDt date,
    reimbTerminationDt date,

    -- Claim and product codes
    aareteDerivedClaimTypeCd text,
    aareteDerivedProduct text,
    aareteDerivedLob text,
    aareteDerivedProgram text,
    aareteDerivedNetwork text,
    aareteDerivedProvType text,

    -- Provider taxonomy and specialty
    provTaxonomyCd text,
    provTaxonomyCdDesc text,
    provSpecialtyCd text,
    provSpecialtyCdDesc text,

    -- Service location
    placeOfServiceCd text,
    placeOfServiceCdDesc text,
    billTypeCd text,
    billTypeCdDesc text,

    -- Patient demographics
    patientAgeMin text,
    patientAgeMax text,

    -- Reimbursement terms
    reimbTerm text,
    lobProgramRelationship text,
    lobProductRelationship text,

    -- Carveout and payment logic
    carveoutInd boolean,
    carveoutCd text,
    lesserOfInd boolean,
    greaterOfInd boolean,
    aareteDerivedReimbMethod text,
    unitOfMeasure text,

    -- Reimbursement rates
    reimbPctRate numeric(10,4),
    reimbFeeRate numeric(12,2),
    reimbConversionFactor numeric(12,4),
    triggerCapThresholdAmt numeric(12,2),
    triggerBaseThreshold numeric(12,2),

    -- Default and addition
    defaultInd boolean,
    additionDesc text,
    additionMaxFeeRateInc numeric(12,2),
    additionMaxPctRateInc numeric(10,4),
    aareteDerivedAdditionRateChangeTimeline text,

    -- Fee schedule
    aareteDerivedFeeSchedule text,
    aareteDerivedFeeScheduleVersion text,

    -- Service codes
    serviceTerm text,
    cpt4ProcCd text,
    cpt4ProcCdDesc text,
    cpt4ProcMod text,
    cpt4ProcModDesc text,
    revenueCd text,
    revenueCdDesc text,
    diagCd text,
    diagCdDesc text,
    ndcCd text,
    ndcCdDesc text,

    -- Claim admit and status
    claimAdmitTypeCd text,
    authAdmitTypeDesc text,
    claimStatusCd text,
    claimStatusCdDesc text,

    -- Grouper information
    grouperType text,
    grouperCd text,
    grouperCdDesc text,
    grouperPctRate numeric(10,4),
    grouperBaseRate numeric(12,2),
    aareteDerivedGrouperVersion text,
    grouperAlternativeLevelOfCare text,
    grouperSeverityInd boolean,
    grouperSeverity text,
    grouperRiskOfMortalitySubclass text,
    grouperTransferInd boolean,
    grouperReadmissionsInd boolean,
    grouperHacInd boolean,

    -- Outlier terms
    outlierTerm text,
    outlierFirstDollarInd boolean,
    rangeNbrDays text,
    outlierFixedLossNbrDaysThreshold numeric(10,2),
    outlierFixedLossThreshold numeric(12,2),
    outlierMaximum numeric(12,2),
    outlierMaximumFrequency numeric(10,2),
    outlierPctRate numeric(10,4),
    outlierExclusionCd text,
    outlierExclusionCdDesc text,

    -- Facility adjustments
    facilityAdjustmentTerm text,
    dshInd boolean,
    dshPctRate numeric(10,4),
    dshFeeRate numeric(12,2),
    imeInd boolean,
    imePctRate numeric(10,4),
    imeFeeRate numeric(12,2),
    ntapInd boolean,
    ntapPctRate numeric(10,4),
    ntapFeeRate numeric(12,2),
    ucInd boolean,
    ucPctRate numeric(10,4),
    ucFeeRate numeric(12,2),
    gmeInd boolean,
    gmePctRate numeric(10,4),
    gmeFeeRate numeric(12,2),

    -- Rate escalator
    rateEscalatorInd boolean,
    rateEscalatorDesc text,
    rateEscalatorMaxRateIncPct numeric(10,4),
    rateEscalatorRateChangeTimeline numeric(10,2),

    -- Stop loss
    stopLossTerm text,
    stopLossFirstDollarInd boolean,
    stopLossRangeNbrDays numeric(10,2),
    stopLossFixedLossThreshold numeric(12,2),
    stopLossMaximum numeric(12,2),
    stopLossMaximumFrequency numeric(10,2),
    stopLossDailyMaxRate numeric(12,2),
    stopLossPctRateOnExcessCharges numeric(10,4),
    stopLossExclusionCd text,
    stopLossExclusionDesc text,

    FOREIGN KEY (fieldExtractionId) REFERENCES documentFieldExtractions(id),

    -- Ensure unique index per extraction
    UNIQUE (fieldExtractionId, arrayIndex),

    -- Ensure arrayIndex is non-negative
    CHECK (arrayIndex >= 0)
);

-- Indexes for efficient array field queries
CREATE INDEX idx_documentfieldextractionarrayfields_fieldextractionid
    ON documentFieldExtractionArrayFields(fieldExtractionId);

-- Composite index for efficient ordered retrieval
CREATE INDEX idx_documentfieldextractionarrayfields_id_index
    ON documentFieldExtractionArrayFields(fieldExtractionId, arrayIndex);
