const { DataTypes } = require('sequelize');

module.exports = (sequelize) => {
    const DoctorHospital = sequelize.define('doctorhospital', {
        doctor_id: {
            type: DataTypes.INTEGER,
            references: {
                model: 'doctors',
                key: 'id'
            },
            onDelete: 'CASCADE',
            primaryKey: true
        },
        hospital_id: {
            type: DataTypes.INTEGER,
            references: {
                model: 'hospitals',
                key: 'id'
            },
            onDelete: 'CASCADE',
            primaryKey: true
        },
    }, {
        tableName: 'doctorhospitals',
        freezeTableName: true
    });

    /**
     * Define associations for the DoctorHospital model.
     * @param {object} models - The models to associate with.
     */
    DoctorHospital.associate = (models) => {
        /**
         * Associate DoctorHospital with Doctor
         * @param {object} models - The models to associate with.
         * @param {object} models.Doctor - The Doctor model.
         */
        DoctorHospital.belongsTo(models.Doctor, {
            foreignKey: 'doctor_id',
            as: 'doctor'
        });

        /**
         * Associate DoctorHospital with Hospital
         * @param {object} models - The models to associate with.
         * @param {object} models.Hospital - The Hospital model.
         */
        DoctorHospital.belongsTo(models.Hospital, {
            foreignKey: 'hospital_id',
            as: 'hospital'
        });
    };

    return DoctorHospital;
};
