module Handler.SiteMap where

import Import
import Yesod.Sitemap
import Form

import Database.Persist.Sql
import qualified Data.Text as T

getSiteMapR :: Handler TypedContent
getSiteMapR = do
  let marksq = "SELECT DISTINCT ?? FROM mark"
              <> " INNER JOIN generation g ON mark.id = g.mark_id"
              <> " INNER JOIN lk_tag l ON g.id = l.generation"
              <> " INNER JOIN text_advise ta ON l.text_advise_id = ta.id"
              <> " WHERE ta.hidden = 'f' AND ta.promo IS NOT NULL"

  marks <- runDB $ rawSql marksq []
  markUrls <- pure $ fmap markToRoute marks
  homeUrl <- pure $ HomeR
  aboutUrl <- pure $ AboutR

  tas <- getReviewData
  taUrls <- pure $ fmap taToRoute tas
  
  sitemapList $ fmap toSitemap $ homeUrl : aboutUrl : (markUrls ++ taUrls)

markToRoute :: Entity Mark -> Route App
markToRoute mark = ModelsR (markUrl $ entityVal mark)

taToRoute :: (Single Text, Single Text) -> Route App
taToRoute (Single a,Single b) = ReviewR a b

toSitemap :: Route App -> SitemapUrl (Route App)
toSitemap x =
  SitemapUrl x Nothing (Just Daily) (Just 1.0)