'use strict';

const fs = require('fs');
const path = require('path');
const Sequelize = require('sequelize');
const basename = path.basename(__filename);
const env = process.env.NODE_ENV || 'development';
const config = require(__dirname + '/../config/config.js')[env];
const db = {};

let sequelize;
if (config.use_env_variable) {
  sequelize = new Sequelize(process.env[config.use_env_variable], config);
} else {
  sequelize = new Sequelize(config.database, config.username, config.password, config);
}

// ✅ Load and register all models
db.TeamYear = require('./teamyear')(sequelize, Sequelize.DataTypes);
db.OurTeamMember = require('./ourteammember')(sequelize, Sequelize.DataTypes);
db.EventYear = require('./EventYear')(sequelize, Sequelize.DataTypes);
db.Event = require('./event')(sequelize, Sequelize.DataTypes);
db.TeamMember = require('./teamMember')(sequelize, Sequelize.DataTypes);
db.HomeImage = require('./homeImage')(sequelize, Sequelize.DataTypes);
db.Announcement = require('./announcement')(sequelize, Sequelize.DataTypes);
db.Blog = require('./blog')(sequelize, Sequelize.DataTypes);
db.GalleryImage = require('./galleryimage')(sequelize, Sequelize.DataTypes);
db.CalendarEvent = require('./calendarEvent')(sequelize, Sequelize.DataTypes);
db.ContactInfo = require('./contactinfo')(sequelize, Sequelize.DataTypes);
db.MembershipForm = require('./membershipForm')(sequelize, Sequelize.DataTypes);
db.Popup = require('./popup')(sequelize, Sequelize.DataTypes);
db.Admin = require('./admin')(sequelize, Sequelize.DataTypes);


// ✅ Set up associations if defined
if (db.TeamYear.associate) db.TeamYear.associate(db);
if (db.OurTeamMember.associate) db.OurTeamMember.associate(db);
if (db.EventYear.associate) db.EventYear.associate(db);
if (db.Event.associate) db.Event.associate(db);
if (db.TeamMember.associate) db.TeamMember.associate(db);
// (No associations needed for HomeImage, Announcement, etc. unless defined)

db.sequelize = sequelize;
db.Sequelize = Sequelize;

module.exports = db;
