const fs = require("fs-extra");
const getPathsObject = require("./siteMapGen");
const formatDate = require("./formatDate");

// ROBOTS.txt
// const robotsTxt = `User-agent: *
// Sitemap: https://www.talentstube.com/sitemap_local.xml
// Disallow:`;

const robotsTxt = `# crawl setup
User-agent: *
Disallow: /Login*
Disallow: /Help
Disallow: /MentionsLegales
Disallow: /Inscription
Disallow: /Cgu
Disallow: /Confidentialite
Disallow: /Candidature
Disallow: /CandidatureValidation
Disallow: /profileStep
# Sitemap
Sitemap: https://www.talentstube.com/sitemap_local.xml`

fs.writeFileSync("out/robots.txt", robotsTxt);
console.log("robots.txt saved!");

// SITEMAP.XML
const pathsObj = getPathsObject();
const today = formatDate(new Date());
const sitemapXml = `<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"> 
  ${Object.keys(pathsObj).map(
    path => `<url>
    <loc>https://www.talentstube.com${path}</loc>
    <lastmod>${
      pathsObj[path].lastModified
        ? formatDate(new Date(pathsObj[path].lastModified))
        : today
    }</lastmod>
  </url>`
  )}
</urlset>`;

// fs.writeFileSync("out/sitemap_local.xml", sitemapXml);
//console.log("sitemap_local.xml saved!");

// GOOGLE's VERIFY HTML
// const googleVerify = `google-site-verification: google8f5d91a719b65f09.html`;
// fs.mkdirSync("out/sitemap.xml");
// fs.writeFileSync("out/sitemap.xml/google8f5d91a719b65f09.html", googleVerify);
// console.log("google8f5d91a719b65f09.html saved!");

// copy `_redirects` etc.
//fs.copySync("scripts/copy-to-build/", "out/");