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


const sql = "SELECT * FROM podcasts WHERE status=1 and podcasts='Нравится слушать'";

const title ='Нравится слушать';
const atom_link = 'https://blogbaster.xyz/api/podcasts/like-to-listen/';
const email = 'crapsh@gmail.com';
const nameCompanies= 'BlogBaster';
const description = 'Мы молодая команда из Краснодара, создаем подкасты для молодежи';
const webSite = 'https://blogbaster.xyz';
const urlImg = 'https://blogbaster.xyz/img/Like-to-listen.jpg';
const lastBuildDate = 'Fri, 06 May 2022 15:30:47 +0000';

interface Rows {
  episode: number;
  podcasts: string;
  title: string;
  description: string;
  guid: string;
  pubDate: string;
  duration: number;
  urlImg: string;
  fileSize: number;
  audio: string;
}

const item = (data: Rows) => {
  let items =`
  <item>
    <itunes:episodeType>full</itunes:episodeType>
    <itunes:episode>`+data.episode+`</itunes:episode>
    <itunes:season>1</itunes:season>
    <title>`+data.podcasts+': '+data.title+`</title>
    <description>`+data.description+`</description>
    <googleplay:description>`+data.description+`</googleplay:description>
    <itunes:summary>`+data.description+`</itunes:summary>
    <guid isPermaLink="false">`+data.guid+`</guid>
    <pubDate>`+data.pubDate+`</pubDate>
    <itunes:duration>`+data.duration+`</itunes:duration>
    <link>`+webSite+'/podcast/'+data.guid+`</link>
    <googleplay:image href="/img/`+data.urlImg+`"/>
    <itunes:image href="/img/`+data.urlImg+`"/>
    <googleplay:explicit>no</googleplay:explicit>
    <itunes:explicit>false</itunes:explicit>
    <enclosure url="'https://blogbaster.xyz/audio/`+data.audio+`" length="`+data.fileSize+`" type="audio/mpeg"/>
    <media:title>`+data.podcasts+': '+data.title+`</media:title>
    <media:content url="https://blogbaster.xyz/audio/`+data.audio+`" fileSize="`+data.fileSize+`" type="audio/mpeg" medium="audio" isDefault="true" expression="full" bitrate="320" channels="2" duration="`+data.duration+`" lang="ru"/>
    <media:thumbnail url="/img/`+data.urlImg+`" width="150" height="150"/>
    <media:copyright url="`+webSite+`">© 2022 `+nameCompanies+`</media:copyright>
    <creativeCommons:license>https://creativecommons.org/licenses/by/4.0/</creativeCommons:license>
  </item>`;
  return(items)
}

const getRssXml = (data: Array<string>) => 
 `<?xml version="1.0" encoding="UTF-8"?>
    <rss version="2.0"
      xmlns:atom="http://www.w3.org/2005/Atom"
      xmlns:googleplay="http://www.google.com/schemas/play-podcasts/1.0/play-podcasts.xsd"
      xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd"
      xmlns:content="http://purl.org/rss/1.0/modules/content/"
      xmlns:media="http://search.yahoo.com/mrss/"
      xmlns:creativeCommons="http://backend.userland.com/creativeCommonsRssModule"
    >
   <channel>
     <title>`+title+`</title>
     <atom:link href="`+atom_link+`" rel="self" type="application/rss+xml"/>
     <googleplay:owner>`+email+`</googleplay:owner>
     <itunes:owner>
       <itunes:name>`+nameCompanies+`</itunes:name>
       <itunes:email>`+email+`</itunes:email>
     </itunes:owner>
     <googleplay:author>`+nameCompanies+`</googleplay:author>
     <itunes:author>`+nameCompanies+`</itunes:author>
     <description>`+description+`</description>
     <googleplay:description>`+description+`</googleplay:description>
     <itunes:summary>`+description+`</itunes:summary>
     <image>
       <link>`+webSite+'/'+webSite+`</link>
       <title>`+nameCompanies+`</title>
       <url>`+webSite+'/'+urlImg+`</url>
     </image>
     <googleplay:image href="`+webSite+'/'+urlImg+`"/>
     <itunes:image href="`+webSite+'/'+urlImg+`"/>
     <itunes:category text="Society &amp; Culture">
       <itunes:category text="Places &amp; Travel"/>
     </itunes:category>
     <itunes:category text="Science">
       <itunes:category text="Social Sciences"/>
     </itunes:category>
     <language>ru</language>
     <link>`+webSite+`</link>
     <copyright>© 2022 `+nameCompanies+`</copyright>
     <itunes:type>episodic</itunes:type>
     <googleplay:explicit>no</googleplay:explicit>
     <itunes:explicit>no</itunes:explicit>
     <lastBuildDate>`+lastBuildDate+`</lastBuildDate>
     <googleplay:block>yes</googleplay:block>
   `+data.map((rows : any) => item(rows))+`
   </channel>
 </rss>`;

export default function handler(
  req: NextApiRequest,
  res: NextApiResponse
) {
  const userAgent = req.headers['user-agent'];
  console.log(userAgent);
  Select(sql, function(data : any){
    res.setHeader('Content-Type', 'text/xml');
    res.status(200).send(getRssXml(data));
  })

}
