import config from 'config/general';

export default {
  categories: {
    detailMethod: 'categoriesGetOne',
    addMethod: 'categoriesAddOne',
    editMethod: 'categoriesEditOne',
    deleteMethod: 'categoriesDeleteOne',
    listMethod: 'categoriesGetMany',
    titleList: 'Categories',
    inputModel: {
      ressourceType: 'articles',
    },
    headers: [{ title: 'Title', key: `title.${config.defaultLanguage}` }],
    model: {
      section: {
        type: 'section',
        label: 'Categorie',
        fields: [
          { type: 'text', label: 'Id', name: '_id', noUpdate: true },
          {
            type: 'text',
            label: 'Ressource',
            name: 'ressourceType',
            initialValue: 'articles',
          },
          { type: 'text', label: 'Id', name: '_id', noUpdate: true },
          { type: 'multi', label: 'title', name: 'title', required: true },
          {
            type: 'color',
            label: 'Color',
            name: 'colorCode',
            saveValue: 'hex',
          },
          { type: 'multiwysiwyg', label: 'Content', name: 'content' },
        ],
      },
    },
  },
  articles: {
    detailMethod: 'article',
    addMethod: 'articleCreateOne',
    editMethod: 'articleEditOne',
    deleteMethod: 'articleDeleteOne',
    titleList: 'Articles',
    fieldsToKeep: [
      'title',
      'content',
      'categoryIds',
      'authorId',
      'cover',
      'thumbnail',
      'seo',
      'urls',
      'access',
      'teaser',
    ],
    headers: [
      { title: 'Title', key: `title.${config.defaultLanguage}` },
      { title: 'Status', type: 'status', key: 'published', width: 150 },
      { title: 'Publication Date', key: 'publicationDate', type: 'date' },
      { title: 'Views', key: 'viewsCount' },
    ],
    transformFunction: obj => ({
      infos: {
        info1: { label: 'Published', active: obj.published },
        info4: { label: 'Not Published', active: !obj.published },
      },
    }),
    model: {
      info: {
        type: 'section',
        label: 'Infos',
        fields: [
          { type: 'text', label: 'Id', name: '_id', noUpdate: true },
          { type: 'multi', label: 'Title', name: 'title' },
          {
            type: 'picture',
            displayValue: 'large',
            apollo: true,
            asObject: true,
            label: 'Cover',
            name: 'cover',
          },
          {
            type: 'picture',
            displayValue: 'large',
            apollo: true,
            asObject: true,
            label: 'Thumbnail',
            name: 'thumbnail',
          },
          { type: 'multiarea', label: 'Teaser', name: 'teaser' },
          {
            type: 'multiwysiwyg',
            label: 'Content',
            name: 'content',
            apollo: true,
          },
        ],
      },
      more: {
        type: 'section',
        label: 'Options',
        fields: [
          { type: 'multi', label: 'URLS', name: 'urls' },
          {
            type: 'bool',
            label: 'Published',
            name: 'published',
            noUpdate: true,
            noCreate: true,
          },
          {
            type: 'date',
            label: 'publication date',
            name: 'publicationDate',
            noUpdate: true,
            noCreate: true,
          },
          { type: 'separator', label: 'SEO' },
          { type: 'multi', label: 'title', name: 'seo.title' },
          { type: 'multi', label: 'description', name: 'seo.description' },
          { type: 'multi', label: 'keywords', name: 'seo.keywords' },
          !config.noSEOThumbnail
            ? {
                type: 'picture',
                displayValue: 'large',
                apollo: true,
                asObject: true,
                label: 'thumbnail',
                name: 'seo.thumbnail',
              }
            : null,
        ],
      },
      category: {
        type: 'section',
        label: 'Categories',
        fields: [
          {
            type: 'select',
            label: 'Categories',
            name: 'categoryIds',
            noDetail: true,
            multiple: true,
            api: {
              resource: 'categoriesGetMany',
            },
            inputs: {
              ressourceType: 'articles',
            },
            modelValues: {
              value: '_id',
              text: { value: 'title', translatable: true },
            },
            noFilterApi: true,
          },
          {
            noUpdate: true,
            noCreate: true,
            type: 'list',
            label: 'Categories',
            name: 'getCategories',
            fields: [
              {
                noMargin: true,
                type: 'multi',
                name: 'title',
                label: 'title',
              },
            ],
          },
        ],
      },
      author: {
        type: 'section',
        label: 'Author',
        fields: [
          {
            type: 'select',
            label: 'Author',
            name: 'authorId',
            noDetail: true,
            api: {
              resource: 'authors',
            },
            modelValues: {
              value: '_id',
              text: 'firstName',
              complementary: 'lastName',
            },
            noFilterApi: true,
          },
          {
            type: 'string',
            label: 'Author',
            name: 'getAuthor.firstName',
            noUpdate: true,
            noCreate: true,
          },
          {
            type: 'string',
            label: 'Author',
            name: 'getAuthor.lastName',
            noUpdate: true,
            noCreate: true,
          },
          {
            type: 'string',
            label: 'Author',
            name: 'getAuthor.email',
            noUpdate: true,
            noCreate: true,
          },
        ],
      },
      stats: {
        type: 'section',
        label: 'Stats',
        noCreate: true,
        noUpdate: true,
        fields: [{ type: 'string', label: 'Views', name: 'viewsCount' }],
      },
    },
  },
  authors: {
    detailMethod: 'author',
    editMethod: 'editAuthor',
    addMethod: 'addAuthor',
    fieldsToAvoidUpdate: ['email'],
    titleList: 'Authors',
    treatData: data => {
      if (data.email) data.email = data.email.replace(/\s/g, '');
      return data;
    },
    headers: [
      { title: 'Email', key: 'email' },
      { title: 'FirstName', name: 'firstName' },
      { title: 'LastName', name: 'lastName' },
    ],
    model: {
      section: {
        type: 'section',
        label: 'Author',
        fields: [
          { type: 'text', label: 'Id', name: '_id', noUpdate: true },
          {
            type: 'string',
            label: 'email',
            name: 'email',
            required: true,
            noUpdate: true,
          },
          {
            type: 'string',
            label: 'firstName',
            name: 'firstName',
            required: true,
          },
          {
            type: 'string',
            label: 'lastName',
            name: 'lastName',
            required: true,
          },
          {
            type: 'string',
            label: 'password',
            name: 'password',
            required: true,
            noQuery: true,
            noUpdate: true,
          },
          {
            type: 'string',
            label: 'password confirmation',
            name: 'passwordConfirmation',
            required: true,
            noQuery: true,
            noUpdate: true,
          },
          {
            type: 'picture',
            label: 'profile picture',
            name: 'profilePicture',
            required: true,
            apollo: true,
            asObject: true,
            displayValue: 'large',
          },
          { type: 'multiarea', label: 'Bio', name: 'bio' },
        ],
      },
    },
  },
};
