-- name: AddFieldExtraction :one
INSERT INTO documentFieldExtractions (
    documentId,
    fileName,
    contractTitle,
    aareteDerivedAmendmentNum,
    clientName,
    payerName,
    payerState,
    providerState,
    filenameTin,
    provGroupTin,
    provGroupNpi,
    provGroupNameFull,
    provOtherTin,
    provOtherNpi,
    provOtherNameFull,
    aareteDerivedEffectiveDt,
    aareteDerivedTerminationDt,
    autoRenewalInd,
    autoRenewalTerm,
    createdBy
) VALUES (
    $1, $2, $3, $4, $5, $6, $7, $8, $9, $10,
    $11, $12, $13, $14, $15, $16, $17, $18, $19, $20
)
RETURNING *;

-- name: AddFieldExtractionArrayField :exec
INSERT INTO documentFieldExtractionArrayFields (
    fieldExtractionId,
    arrayIndex,
    exhibitTitle,
    exhibitPage,
    reimbProvTin,
    reimbProvNpi,
    reimbProvName,
    reimbEffectiveDt,
    reimbTerminationDt,
    aareteDerivedClaimTypeCd,
    aareteDerivedProduct,
    aareteDerivedLob,
    aareteDerivedProgram,
    aareteDerivedNetwork,
    aareteDerivedProvType,
    provTaxonomyCd,
    provTaxonomyCdDesc,
    provSpecialtyCd,
    provSpecialtyCdDesc,
    placeOfServiceCd,
    placeOfServiceCdDesc,
    billTypeCd,
    billTypeCdDesc,
    patientAgeMin,
    patientAgeMax,
    reimbTerm,
    lobProgramRelationship,
    lobProductRelationship,
    carveoutInd,
    carveoutCd,
    lesserOfInd,
    greaterOfInd,
    aareteDerivedReimbMethod,
    unitOfMeasure,
    reimbPctRate,
    reimbFeeRate,
    reimbConversionFactor,
    triggerCapThresholdAmt,
    triggerBaseThreshold,
    defaultInd,
    additionDesc,
    additionMaxFeeRateInc,
    additionMaxPctRateInc,
    aareteDerivedAdditionRateChangeTimeline,
    aareteDerivedFeeSchedule,
    aareteDerivedFeeScheduleVersion,
    serviceTerm,
    cpt4ProcCd,
    cpt4ProcCdDesc,
    cpt4ProcMod,
    cpt4ProcModDesc,
    revenueCd,
    revenueCdDesc,
    diagCd,
    diagCdDesc,
    ndcCd,
    ndcCdDesc,
    claimAdmitTypeCd,
    authAdmitTypeDesc,
    claimStatusCd,
    claimStatusCdDesc,
    grouperType,
    grouperCd,
    grouperCdDesc,
    grouperPctRate,
    grouperBaseRate,
    aareteDerivedGrouperVersion,
    grouperAlternativeLevelOfCare,
    grouperSeverityInd,
    grouperSeverity,
    grouperRiskOfMortalitySubclass,
    grouperTransferInd,
    grouperReadmissionsInd,
    grouperHacInd,
    outlierTerm,
    outlierFirstDollarInd,
    rangeNbrDays,
    outlierFixedLossNbrDaysThreshold,
    outlierFixedLossThreshold,
    outlierMaximum,
    outlierMaximumFrequency,
    outlierPctRate,
    outlierExclusionCd,
    outlierExclusionCdDesc,
    facilityAdjustmentTerm,
    dshInd,
    dshPctRate,
    dshFeeRate,
    imeInd,
    imePctRate,
    imeFeeRate,
    ntapInd,
    ntapPctRate,
    ntapFeeRate,
    ucInd,
    ucPctRate,
    ucFeeRate,
    gmeInd,
    gmePctRate,
    gmeFeeRate,
    rateEscalatorInd,
    rateEscalatorDesc,
    rateEscalatorMaxRateIncPct,
    rateEscalatorRateChangeTimeline,
    stopLossTerm,
    stopLossFirstDollarInd,
    stopLossRangeNbrDays,
    stopLossFixedLossThreshold,
    stopLossMaximum,
    stopLossMaximumFrequency,
    stopLossDailyMaxRate,
    stopLossPctRateOnExcessCharges,
    stopLossExclusionCd,
    stopLossExclusionDesc
) VALUES (
    $1, $2, $3, $4, $5, $6, $7, $8, $9, $10,
    $11, $12, $13, $14, $15, $16, $17, $18, $19, $20,
    $21, $22, $23, $24, $25, $26, $27, $28, $29, $30,
    $31, $32, $33, $34, $35, $36, $37, $38, $39, $40,
    $41, $42, $43, $44, $45, $46, $47, $48, $49, $50,
    $51, $52, $53, $54, $55, $56, $57, $58, $59, $60,
    $61, $62, $63, $64, $65, $66, $67, $68, $69, $70,
    $71, $72, $73, $74, $75, $76, $77, $78, $79, $80,
    $81, $82, $83, $84, $85, $86, $87, $88, $89, $90,
    $91, $92, $93, $94, $95, $96, $97, $98, $99, $100,
    $101, $102, $103, $104, $105, $106, $107, $108, $109, $110,
    $111, $112, $113, $114
);

-- name: AddFieldExtractionEntry :exec
INSERT INTO documentFieldExtractionVersions (fieldExtractionId, version, createdBy)
VALUES ($1, $2, $3);

-- name: GetCurrentFieldExtraction :one
SELECT * FROM currentFieldExtractions
WHERE documentId = $1;

-- name: GetCurrentFieldExtractionWithArrayCount :one
SELECT * FROM currentFieldExtractionsWithArrayCount
WHERE documentId = $1;

-- name: GetFieldExtractionArrayFields :many
SELECT * FROM documentFieldExtractionArrayFields
WHERE fieldExtractionId = $1
ORDER BY arrayIndex;

-- name: GetFieldExtractionHistory :many
SELECT
    dfe.id,
    dfe.documentId,
    dfev.version,
    dfev.createdBy,
    dfev.createdAt
FROM documentFieldExtractions dfe
JOIN documentFieldExtractionVersions dfev
    ON dfev.fieldExtractionId = dfe.id
WHERE dfe.documentId = $1
ORDER BY dfev.version DESC;

-- name: ValidateArrayFieldCount :one
SELECT COUNT(*) as arrayFieldCount
FROM documentFieldExtractionArrayFields
WHERE fieldExtractionId = $1;

-- name: GetFieldExtractionByID :one
SELECT * FROM documentFieldExtractions
WHERE id = $1;

-- name: GetFieldExtractionsByDocumentID :many
SELECT * FROM documentFieldExtractions
WHERE documentId = $1
ORDER BY createdAt DESC;
