require('dotenv').config();
const { sequelize } = require('./config/database');
const {
  Stop, RouteStop, SegmentPrice, BookingSegment,
  Stop: StopModel, RouteStop: RouteStopModel, SegmentPrice: SegmentPriceModel, BookingSegment: BookingSegmentModel,
} = require('./models');

const run = async () => {
  try {
    await sequelize.sync({ alter: true });
    console.log('[Migrate] Tables synced');

    const stopCount = await StopModel.count();
    if (stopCount === 0) {
      const stops = await StopModel.bulkCreate([
        { name: 'Windhoek', country: 'Namibia', is_border: false },
        { name: 'Gobabis', country: 'Namibia', is_border: false },
        { name: 'Buitepos', country: 'Namibia', is_border: true },
        { name: 'Charles Hill', country: 'Botswana', is_border: true },
        { name: 'Tsootsha', country: 'Botswana', is_border: false },
        { name: 'Karakubis', country: 'Botswana', is_border: false },
        { name: 'Ghanzi', country: 'Botswana', is_border: false },
        { name: 'Gaborone', country: 'Botswana', is_border: false },
      ]);
      console.log(`[Migrate] Created ${stops.length} stops`);
    }

    console.log('[Migrate] Complete!');
    process.exit(0);
  } catch (error) {
    console.error('[Migrate] Error:', error.message);
    process.exit(1);
  }
};

run();
