import express from 'express';
import cors from 'cors';
import { readFileSync } from 'fs';
import { createServer as createHttpsServer } from 'https';
import { fileURLToPath } from 'url';
import { dirname, join } from 'path';

import db from './db.js';
import { initTransporter } from './email/transporter.js';
import { startImapPolling } from './email/imap.js';

import setupRoutes from './routes/setup.js';
import authRoutes from './routes/auth.js';
import userRoutes from './routes/users.js';
import boardRoutes from './routes/boards.js';
import cardRoutes from './routes/cards.js';
import notificationRoutes from './routes/notifications.js';
import emailRoutes from './routes/email.js';

const __dirname = dirname(fileURLToPath(import.meta.url));
const app = express();
const PORT = process.env.PORT || 3001;

app.use(cors());
app.use(express.json({ limit: '10mb' }));

app.use('/api/setup', setupRoutes);
app.use('/api/auth', authRoutes);
app.use('/api/users', userRoutes);
app.use('/api/boards', boardRoutes);
app.use('/api/cards', cardRoutes);
app.use('/api/notifications', notificationRoutes);
app.use('/api/email', emailRoutes);

app.use('/api', (req, res) => res.status(404).json({ error: 'Not found' }));

const clientDist = join(__dirname, '..', 'client', 'dist');
app.use(express.static(clientDist));
app.get('*', (req, res) => {
  if (req.path.startsWith('/api')) return res.status(404).json({ error: 'Not found' });
  res.sendFile(join(clientDist, 'index.html'));
});

const SSL_KEY = process.env.SSL_KEY || '';
const SSL_CERT = process.env.SSL_CERT || '';

async function startServer() {
  const hasEmail = !!db.prepare('SELECT 1 FROM email_config WHERE id = 1').get();
  if (hasEmail) {
    const ok = await initTransporter();
    if (ok) console.log('✉️  Email configured and verified');
    startImapPolling();
  }

  if (SSL_KEY && SSL_CERT) {
    const server = createHttpsServer({
      key: readFileSync(SSL_KEY),
      cert: readFileSync(SSL_CERT),
    }, app);
    server.listen(PORT, () => console.log(`🚀 TeamFlow server running on https://localhost:${PORT}`));
  } else {
    app.listen(PORT, () => console.log(`🚀 TeamFlow server running on http://localhost:${PORT}`));
  }
}

startServer();
