import DirectusToMarkdown from '@resilien/directus-to-markdown'
import urlslug from 'url-slug'

const filterDraft = process.env.DRAFT && process.env.DRAFT == 'true' ? '' : { draft: { _eq: 'false' } }
const filterAssociation = process.env.DRAFT && process.env.DRAFT == 'true' ? '' : { display_website: { _eq: 'true' } }

const config = {
  collections: {
    Pages: {
      readByQueryOption: {
        fields: ['title', 'description', 'slug', 'image', 'image_credit', 'content', 'menu_display', 'sort', 'identifier', 'menu_title', 'parent', 'layout', 'aliases', 'draft'],
        filterDraft
      },
      pathBuilder: (page) => {
        page.path = page.parent ? 'index.md' : '_index.md'
        if (!page.layout) delete page.layout
        if (!page.image) delete page.image
        if (!page.image_credit) delete page.image_credit
        if (!page.aliases) delete page.aliases

        if (page.menu_display) {
          page.menu = {
            main: {
              name: page.menu_title,
              weight: page.sort
            }
          }
          if (page.identifier) {
            page.menu.main.identifier = page.identifier
          }
          if (page.parent) {
            page.menu.main.parent = page.parent
          }
        }
        return `./content${page.slug}`;
      },
      deleteFields: ['path', 'slug', 'menu_display', 'sort', 'identifier', 'menu_title', 'parent'],
    },
    actualites: {
      readByQueryOption: {
        fields: ['title', 'date', 'image', 'image_credit', 'vignette', 'vignette_credit', 'description', 'auteur', 'draft', 'content', 'event'],
        filterDraft,
        limit: 1000
      },
      pathBuilder: (article) => {
        if (!article.image) delete article.image
        if (!article.image_credit) delete article.image_credit
        if (!article.vignette) delete article.vignette
        if (!article.vignette_credit) delete article.vignette_credit
        const [year, month, day] = article.date.split("-")
        return `./content/actualites/${year}/${month}/${day}-${urlslug(article.title, { remove: /\./g })}`;
      },
      deleteFields: [],
    },
    Association: {
      readByQueryOption: {
        fields: [
          'denomination',
          'title',
          'description',
          'mail',
          'telephone',
          'site',
          'facebook',
          'bureau.sort',
          'bureau.Contact_id.*',
          'content',
        ],
        filterAssociation
      },
      pathBuilder: (association) => {
        if (!association.title) {
          association.title = association.denomination
        }
        console.log("Import de l'association « " + association.title + " »")
        if (association.bureau && association.bureau.length > 0) {
          const bureau = []
          for (const contact of association.bureau) {
            const detail = contact.Contact_id
            if (detail) {
              let c = {
                denomination: detail.denomination,
                prenom: detail.prenom,
                nom: detail.nom,
              }
              if (detail.mail && detail.display_mail) {
                c.mail = detail.mail
              }
              if (detail.telephone && detail.display_telephone) {
                c.telephone = detail.telephone
              }
              bureau.push(c)
            } else {
              console.log("problème sur un contact")
            }
          }
          association.bureau = bureau
        } else {
          delete association.bureau
        }

        return `./content/associations/${urlslug(association.denomination)}`;
      },
      deleteFields: [],
    },
  }
}

new DirectusToMarkdown(config).export();