const { sequelize } = require('../config/database');
const Symbol = require('./Symbol');
const Candle1h = require('./Candle1h');
const LivePrice = require('./LivePrice');

// Define associations
Symbol.hasMany(Candle1h, { foreignKey: 'symbolId', as: 'candles1h' });
Candle1h.belongsTo(Symbol, { foreignKey: 'symbolId', as: 'symbol' });

Symbol.hasOne(LivePrice, { foreignKey: 'symbolId', as: 'livePrice' });
LivePrice.belongsTo(Symbol, { foreignKey: 'symbolId', as: 'livePriceSymbol' });

// Sync database (only in development)
if (process.env.NODE_ENV === 'development') {
  sequelize.sync({ alter: true })
    .then(() => {
      console.log('Database synchronized successfully.');
    })
    .catch((error) => {
      console.error('Error synchronizing database:', error);
    });
}

module.exports = {
  sequelize,
  Symbol,
  Candle1h,
  LivePrice
};
