// Next.js API route support: https://nextjs.org/docs/api-routes/introduction
import type { NextApiRequest, NextApiResponse } from 'next'
import Select from "../../../../../server/db/select";
import Insert from "../../../../../server/db/insert";
import { getRssXml, Items } from '../../../../../сomponents/podcasts';

const id = 1;

const sql_company = "SELECT * FROM podcasts, company WHERE podcasts.id="+id;
const sql_items = "SELECT * FROM `podcasts`, items, company WHERE podcasts.id=items.id_podcasts and podcasts.id="+id;

const sql_userAgent = "INSERT INTO views (id_podcasts, userAgent,ip) VALUES (?,?,?)";

export default function handler(
  req: NextApiRequest,
  res: NextApiResponse
) {
  const views = [id, req.headers['user-agent'], req.socket.remoteAddress]
  console.log(views);
  Select(sql_company, function(data_company : any){
    Select(sql_items, function(data_items : any){
      res.setHeader('Content-Type', 'text/xml');
      res.status(200).send(getRssXml(data_company[0])+data_items.map((rows : any) => Items(rows))+'</channel> </rss>');
    })
    
  })
  Insert(sql_userAgent, views as string[], function(message){
    console.log(message);
  })
} 





