import express from 'express';
import path from 'path';
import { fileURLToPath } from 'url';
import sequelize from './database.js';
import cors from 'cors';
import {
  User, Log, Club, UserClub, Member, DiaryDate, Participant, Activity, MemberNote,
  DiaryNote, DiaryTag, MemberDiaryTag, DiaryDateTag, DiaryMemberNote, DiaryMemberTag,
  PredefinedActivity, DiaryDateActivity, Match, League, Team, Group,
  GroupActivity
} from './models/index.js';
import authRoutes from './routes/authRoutes.js';
import clubRoutes from './routes/clubRoutes.js';
import diaryRoutes from './routes/diaryRoutes.js';
import memberRoutes from './routes/memberRoutes.js';
import participantRoutes from './routes/participantRoutes.js';
import activityRoutes from './routes/activityRoutes.js';
import memberNoteRoutes from './routes/memberNoteRoutes.js';
import diaryTagRoutes from './routes/diaryTagRoutes.js';
import diaryNoteRoutes from './routes/diaryNoteRoutes.js';
import diaryMemberRoutes from './routes/diaryMemberRoutes.js';
import predefinedActivityRoutes from './routes/predefinedActivityRoutes.js';
import diaryDateActivityRoutes from './routes/diaryDateActivityRoutes.js';
import matchRoutes from './routes/matchRoutes.js';
import Season from './models/Season.js';
import Location from './models/Location.js';
import groupRoutes from './routes/groupRoutes.js';
import diaryDateTagRoutes from './routes/diaryDateTagRoutes.js';
import sessionRoutes from './routes/sessionRoutes.js';

const app = express();
const port = process.env.PORT || 3000;

const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);

app.use(cors());
app.use(express.json());

app.use('/api/auth', authRoutes);
app.use('/api/clubs', clubRoutes);
app.use('/api/clubmembers', memberRoutes);
app.use('/api/diary', diaryRoutes);
app.use('/api/participants', participantRoutes);
app.use('/api/activities', activityRoutes);
app.use('/api/membernotes', memberNoteRoutes);
app.use('/api/diarynotes', diaryNoteRoutes);
app.use('/api/tags', diaryTagRoutes);
app.use('/api/diarymember', diaryMemberRoutes);
app.use('/api/predefined-activities', predefinedActivityRoutes);
app.use('/api/diary-date-activities', diaryDateActivityRoutes);
app.use('/api/matches', matchRoutes);
app.use('/api/group', groupRoutes);
app.use('/api/diarydatetags', diaryDateTagRoutes);
app.use('/api/session', sessionRoutes);

app.use(express.static(path.join(__dirname, '../frontend/dist')));

app.get('*', (req, res) => {
  res.sendFile(path.join(__dirname, '../frontend/dist/index.html'));
});

(async () => {
  try {
    await sequelize.authenticate();

    await User.sync({ alter: true });
    await Club.sync({ alter: true });
    await UserClub.sync({ alter: true });
    await Log.sync({ alter: true });
    await Member.sync({ alter: true });
    await DiaryDate.sync({ alter: true });
    await Participant.sync({ alter: true });
    await Activity.sync({ alter: true });
    await MemberNote.sync({ alter: true });
    await DiaryNote.sync({ alter: true });
    await DiaryTag.sync({ alter: true });
    await MemberDiaryTag.sync({ alter: true });
    await DiaryDateTag.sync({ alter: true });
    await DiaryMemberTag.sync({ alter: true });
    await DiaryMemberNote.sync({ alter: true });
    await PredefinedActivity.sync({ alter: true });
    await DiaryDateActivity.sync({ alter: true });
    await Season.sync({ alter: true });
    await League.sync({ alter: true });
    await Team.sync({ alter: true });
    await Location.sync({ alter: true });
    await Match.sync({ alter: true });
    await Group.sync({ alter: true });
    await GroupActivity.sync({ alter: true });

    app.listen(port, () => {
      console.log(`Server is running on http://localhost:${port}`);
    });
  } catch (err) {
    console.error('Unable to synchronize the database:', err);
  }
})();