import 'dotenv/config';
import 'reflect-metadata';
import express from 'express';
import cors from 'cors';
import https from 'https';
import fs from 'fs';
import { AppDataSource } from './data-source';
import { Student } from './entity/Student';
import { Request, Response, NextFunction } from 'express';
import clientRouter from './routes/client';
import classRouter from './routes/class';

const app = express();
const port = 3000;

const sslOptions = {
  key: fs.readFileSync('cert/cloudapi-key.key'),
  cert: fs.readFileSync('cert/cloudapi-cert.cert')
};

app.use(cors());
app.use(express.json());
app.options('*', cors()); 
app.use(express.static('uploads'));

app.get('/', (req: Request, res: Response) => {
  res.send('Course API is running 🚀');
});

app.use('/', clientRouter);
app.use('/', classRouter);

AppDataSource.initialize()
  .then(() => {
    console.log('📦 Data Source has been initialized!');
    https.createServer(sslOptions, app).listen(port, () => {
      console.log(`🚀 Server is running at http://localhost:${port}`);
    });
  })
  .catch((error) => {
    console.error('❌ Error during Data Source initialization:', error);
  });