import express from 'express';
const app = express()

import * as swaggerUi from 'swagger-ui-express';
import * as swaggerDocs from './swagger.js';
import * as cheerio from 'cheerio';
import * as lifestyle from './lifestyle.js';

import { OpenaiRes, tebakgambar, AnimeHentai } from './scrapper.js';
import { CheckMilWare } from './midware.js';

import { router as exampleRouter } from './routes/example.js';

const CheckMilWares = new CheckMilWare();

app.use('/api', exampleRouter);
app.use('/docs', swaggerUi.serve, swaggerUi.setup(swaggerDocs));

app.get('/', (req, res) => {
  res.redirect('https://t.me/RendyProjects');
});

app.use(async (req, res, next) => {
  await CheckMilWares.handle(req, res, next);
});

app.get('/api/test', async (req, res) => {
  try {
    res.send("Hello world")
  } catch (error) {
    res.status(401).json({error: error.message})
  }
})

app.get('/api/hentai-anime', async (req, res) => {
  try {
    const result = await scrapper.AnimeHentai();
    if (result) {
      res.json({ result });
    } else {
      res.status(404).json({ error: "No result found." });
    }
  } catch (error) {
    res.status(500).json({ error: error.message });
  }
});

app.get('/api/tebakgambar', async (req, res) => {
  try {
    const result = await scrapper.tebakgambar();
    if (result) {
      res.json({ result });
    } else {
      res.status(404).json({ error: "No result found." });
    }
  } catch (error) {
    res.status(500).json({ error: error.message });
  }
});


app.get('/api/gpt-old', async (req, res) => {
  try {
    const query = req.query.query;
    const results = await scrapper.OpenaiRes(query);
    res.json({ results });
  } catch (error) {
    res.status(401).json({ error: error.message });
  }
});

lifestyle.startServer(app);