"use strict";
Object.defineProperty(exports, "__esModule", {
    value: true
});
function _export(target, all) {
    for(var name in all)Object.defineProperty(target, name, {
        enumerable: true,
        get: Object.getOwnPropertyDescriptor(all, name).get
    });
}
_export(exports, {
    get down () {
        return down;
    },
    get up () {
        return up;
    }
});
const _sequelize = require("sequelize");
async function up(query) {
    await query.createTable('reference_drug_facilities', {
        id: {
            type: `TEXT GENERATED ALWAYS AS (REPLACE("reference_drug_id"::TEXT, ';', ':') || ';' || REPLACE("facility_id", ';', ':')) STORED`
        },
        reference_drug_id: {
            type: _sequelize.DataTypes.UUID,
            allowNull: false,
            primaryKey: true,
            references: {
                model: 'reference_drugs',
                key: 'id'
            },
            onDelete: 'CASCADE'
        },
        facility_id: {
            type: _sequelize.DataTypes.STRING,
            allowNull: false,
            primaryKey: true,
            references: {
                model: 'facilities',
                key: 'id'
            },
            onDelete: 'CASCADE'
        },
        quantity: {
            type: _sequelize.DataTypes.INTEGER,
            allowNull: true
        },
        stock_status: {
            type: _sequelize.DataTypes.STRING,
            allowNull: false,
            defaultValue: 'unknown'
        },
        created_at: {
            type: _sequelize.DataTypes.DATE,
            defaultValue: _sequelize.Sequelize.fn('now'),
            allowNull: false
        },
        updated_at: {
            type: _sequelize.DataTypes.DATE,
            defaultValue: _sequelize.Sequelize.fn('now'),
            allowNull: false
        },
        deleted_at: {
            type: _sequelize.DataTypes.DATE,
            allowNull: true
        }
    });
    // Add CHECK constraint for valid stock_status values
    await query.sequelize.query(`
    ALTER TABLE reference_drug_facilities
    ADD CONSTRAINT check_stock_status_values CHECK (
      stock_status IN ('in_stock', 'out_of_stock', 'unknown', 'unavailable')
    );
  `);
    // Add CHECK constraint to ensure consistency between quantity and stock_status
    await query.sequelize.query(`
    ALTER TABLE reference_drug_facilities
    ADD CONSTRAINT check_quantity_stock_consistency CHECK (
      (stock_status = 'in_stock' AND quantity > 0) OR
      (stock_status = 'out_of_stock' AND quantity = 0) OR
      (stock_status IN ('unknown', 'unavailable') AND quantity IS NULL)
    );
  `);
    // Add indexes for common query patterns
    await query.addIndex('reference_drug_facilities', [
        'stock_status'
    ], {
        name: 'reference_drug_facilities_stock_status_idx'
    });
    await query.addIndex('reference_drug_facilities', [
        'facility_id',
        'stock_status'
    ], {
        name: 'reference_drug_facilities_facility_stock_idx'
    });
}
async function down(query) {
    await query.dropTable('reference_drug_facilities');
}

//# sourceMappingURL=1764594045328-addReferenceDrugFacilitiesTable.js.map