// EMAIL MODEL
const emailModel = [
  {
    type: 'section',
    label: 'Content',
    opened: true,
    fields: [
      { type: 'key', label: '_id', name: '_id', noUpdate: true },
      {
        type: 'text',
        label: 'type',
        name: 'type',
        initialValue: 'emails',
        noUpdate: true,
        noMutation: true,
      },
      { type: 'string', label: 'key', name: 'key', noUpdate: true },
      { type: 'multi', label: 'subject', name: 'subject' },
      { type: 'multicode', label: 'body', name: 'body' },
      { type: 'bool', label: 'Custom', name: 'custom' },
    ],
  },
  {
    type: 'section',
    label: 'Settings',
    fields: [
      { type: 'multi', label: 'template id', name: 'templateId' },
      { type: 'string', label: 'Email from', name: 'fromEmail' },
      { type: 'string', label: 'from name', name: 'fromName', required: true },
      { type: 'string', label: 'reply to email', name: 'replyToEmail', required: true },
      { type: 'tags', label: 'CCI', name: 'cci' },
      { type: 'tags', label: 'Available Fields', name: 'availableFields', noUpdate: true },
    ],
  },
];

const ACModel = [
  {
    type: 'section',
    label: 'Info',
    fields: [
      { type: 'key', label: '_id', name: '_id', noUpdate: true },
      { type: 'string', label: 'field Name', name: 'fieldName' },
      { type: 'string', inputType: 'number', label: 'Ac field id', name: 'acFieldId' },
      { type: 'json', label: 'Field Value Map', name: 'acFieldValueMap' },
      { type: 'json', label: 'Field List Map', name: 'acFieldListMap' },
    ],
  },
];

export default {
  email: {
    detailMethod: 'emailsSettingsGetOne',
    editMethod: 'emailsSettingsEditOne',
    listMethod: 'emailsSettingsGetMany',
    // deleteMethod: 'emailsSettingsDeleteOne',
    noCreate: true,
    noSearch: true,
    addTitle: 'Add email',
    editTitle: 'Edit email',
    titleList: 'Emails',
    headers: [{ title: 'Email ID', key: `key` }],
    model: emailModel,
  },
  activeCampaign: {
    detailMethod: 'acSettingsGetOne',
    addMethod: 'acSettingsAddOne',
    editMethod: 'acSettingsEditOne',
    listMethod: 'acSettingsGetMany',
    deleteMethod: 'acSettingsDeleteOne',
    titleList: 'Active Campaign',
    noSearch: true,
    headers: [
      { title: 'Field name', key: `fieldName` },
      { title: 'Field id', key: 'acFieldId' },
    ],
    treatData: data => {
      // Only fix on create first time
      if (data.acFieldValueMap) {
        data.acFieldValueMap = JSON.parse(data.acFieldValueMap);
      } else data.acFieldValueMap = {};
      if (data.acFieldListMap) {
        data.acFieldListMap = JSON.parse(data.acFieldListMap);
      } else data.acFieldListMap = {};
      if (data.acFieldId) data.acFieldId = parseFloat(data.acFieldId, 10);
      return data;
    },
    model: ACModel,
  },
};
