"use strict";
Object.defineProperty(exports, "__esModule", {
    value: true
});
Object.defineProperty(exports, "AiDocument", {
    enumerable: true,
    get: function() {
        return AiDocument;
    }
});
const _sequelize = require("sequelize");
const _constants = require("@tamanu/constants");
const _Model = require("./Model");
const _buildSyncLookupSelect = require("../sync/buildSyncLookupSelect");
const AI_DOCUMENT_TYPES = [
    'patient_summary',
    'encounter_summary'
];
const AI_DOCUMENT_RECORD_TYPES = [
    'Patient',
    'Encounter'
];
const AI_DOCUMENT_STATUSES = [
    'generated',
    'edited',
    'discarded'
];
const AI_DOCUMENT_SOURCES = [
    'ai',
    'human'
];
let AiDocument = class AiDocument extends _Model.Model {
    static initModel(options) {
        super.init({
            id: {
                // ai_documents use a generated id derived from (type, record_type, record_id),
                // so the same logical document produces the same primary key on every facility/central,
                // simplifying sync and avoiding cross-facility id conflicts.
                // REPLACE on record_id avoids ambiguity if the source id contains the ';' separator.
                type: `TEXT GENERATED ALWAYS AS ("type" || ';' || record_type || ';' || REPLACE("record_id", ';', ':')) STORED`,
                set () {
                // any sets of the convenience generated "id" field can be ignored, so do nothing here
                }
            },
            type: {
                type: _sequelize.DataTypes.STRING,
                allowNull: false,
                primaryKey: true,
                validate: {
                    isIn: [
                        AI_DOCUMENT_TYPES
                    ]
                }
            },
            recordType: {
                type: _sequelize.DataTypes.STRING,
                allowNull: false,
                primaryKey: true,
                validate: {
                    isIn: [
                        AI_DOCUMENT_RECORD_TYPES
                    ]
                }
            },
            recordId: {
                type: _sequelize.DataTypes.STRING,
                allowNull: false,
                primaryKey: true
            },
            status: {
                type: _sequelize.DataTypes.STRING,
                allowNull: false,
                defaultValue: 'generated',
                validate: {
                    isIn: [
                        AI_DOCUMENT_STATUSES
                    ]
                }
            },
            content: {
                type: _sequelize.DataTypes.TEXT,
                allowNull: true
            },
            source: {
                type: _sequelize.DataTypes.STRING,
                allowNull: false,
                defaultValue: 'ai',
                validate: {
                    isIn: [
                        AI_DOCUMENT_SOURCES
                    ]
                }
            }
        }, {
            ...options,
            syncDirection: _constants.SYNC_DIRECTIONS.BIDIRECTIONAL
        });
    }
    static initRelations(_models) {
    // no relations yet
    }
    static buildPatientSyncFilter(patientCount, markedForSyncPatientsTable) {
        if (patientCount === 0) {
            return null;
        }
        return `
      LEFT JOIN encounters ON ai_documents.record_id = encounters.id AND ai_documents.record_type = 'Encounter'
      WHERE (
        (ai_documents.record_type = 'Patient' AND ai_documents.record_id IN (SELECT patient_id FROM ${markedForSyncPatientsTable}))
        OR (ai_documents.record_type = 'Encounter' AND encounters.patient_id IN (SELECT patient_id FROM ${markedForSyncPatientsTable}))
      )
      AND ai_documents.updated_at_sync_tick > :since
    `;
    }
    static async buildSyncLookupQueryDetails() {
        const patientIdExpr = `
      CASE
        WHEN ai_documents.record_type = 'Patient' THEN ai_documents.record_id
        WHEN ai_documents.record_type = 'Encounter' THEN encounters.patient_id
      END
    `;
        return {
            select: await (0, _buildSyncLookupSelect.buildSyncLookupSelect)(this, {
                patientId: patientIdExpr
            }),
            joins: `
        LEFT JOIN encounters ON ai_documents.record_id = encounters.id AND ai_documents.record_type = 'Encounter'
      `
        };
    }
};

//# sourceMappingURL=AiDocument.js.map