import express from 'express';
import axios from 'axios';
import { authenticateApiKey, apiLimiter } from '../middleware/midware.js';
const TrendingNewRoutes = express.Router();

const protectedUsers = [6477856957, 1191668125, 1448273246, 1054295664, 6444305696]; 

const ComboRecentTrending = async (limit) => {
  const options = {
    method: 'GET',
    url: 'https://randydev-meta-ai.hf.space/combo-recent',
    params: {
      limit: limit
    }
  };
  try {
    const response = await axios.request(options);
    return response.data;
  } catch (error) {
    console.error(error);
    return null;
  }
}

const ComboTrending = async (limit) => {
  const options = {
    method: 'GET',
    url: 'https://randydev-meta-ai.hf.space/combo-trending',
    params: {
      limit: limit
    }
  };
  try {
    const response = await axios.request(options);
    return response.data;
  } catch (error) {
    console.error(error);
    return null;
  }
}

const ComboSearchTrending = async (query, limit) => {
  const options = {
    method: 'GET',
    url: 'https://randydev-meta-ai.hf.space/combo-search',
    params: {
      query: query,
      limit: limit
    }
  };
  try {
    const response = await axios.request(options);
    return response.data;
  } catch (error) {
    console.error(error);
    return null;
  }
}

/**
 * @swagger
 * /api/v1/trending/combo-search:
 *   get:
 *     summary: Combo Search Trending
 *     tags: [Trending]
 *     parameters:
 *       - in: query
 *         name: query
 *         required: true
 *         description: null
 *         schema:
 *           type: string
 *       - in: query
 *         name: limit
 *         required: true
 *         description: null
 *         schema:
 *           type: number
 *       - in: header
 *         name: x-api-key
 *         required: true
 *         description: API key for authentication
 *         schema:
 *           type: string
 *     responses:
 *       200:
 *         description: Success
 */
TrendingNewRoutes.get("/api/v1/trending/combo-search", authenticateApiKey, apiLimiter, async (req, res) => {
    const limit = req.query.limit;
    const q = req.query.query;
    if (!q) {
        return res.status(400).json({ error: "Invalid or missing username" });
    }
    try {
        const result = await ComboSearchTrending(q);
        res.json(result);
    } catch (error) {
        res.status(500).json({ error: "Failed to fetch user info" });
    }
});

/**
 * @swagger
 * /api/v1/trending/combo-recent:
 *   get:
 *     summary: Combo Recent Trending
 *     tags: [Trending]
 *     parameters:
 *       - in: query
 *         name: limit
 *         required: true
 *         description: null
 *         schema:
 *           type: number
 *       - in: header
 *         name: x-api-key
 *         required: true
 *         description: API key for authentication
 *         schema:
 *           type: string
 *     responses:
 *       200:
 *         description: Success
 */
TrendingNewRoutes.get("/api/v1/trending/combo-recent", authenticateApiKey, apiLimiter, async (req, res) => {
    const limit = req.query.limit;
    if (!limit) {
        return res.status(400).json({ error: "Invalid or missing username" });
    }
    try {
        const result = await ComboRecentTrending(limit);
        res.json(result);
    } catch (error) {
        res.status(500).json({ error: "Failed to fetch user info" });
    }
});

/**
 * @swagger
 * /api/v1/trending/combo:
 *   get:
 *     summary: Combo Trending
 *     tags: [Trending]
 *     parameters:
 *       - in: query
 *         name: limit
 *         required: true
 *         description: null
 *         schema:
 *           type: number
 *       - in: header
 *         name: x-api-key
 *         required: true
 *         description: API key for authentication
 *         schema:
 *           type: string
 *     responses:
 *       200:
 *         description: Success
 */
TrendingNewRoutes.get("/api/v1/trending/combo", authenticateApiKey, apiLimiter, async (req, res) => {
    const limit = req.query.limit;
    if (!limit) {
        return res.status(400).json({ error: "Invalid or missing username" });
    }
    try {
        const result = await ComboTrending(limit);
        res.json(result);
    } catch (error) {
        res.status(500).json({ error: "Failed to fetch user info" });
    }
});

export { TrendingNewRoutes };