import { CollectionConfig } from "payload";
import { setMealNutritionOnChange } from "./hooks/beforeChangeMeal";

export const Meals: CollectionConfig = {
  slug: 'meals',
  admin: {
    useAsTitle: 'name',
    group: 'Nutrition',
  },
  fields: [
    {
      name: 'name',
      type: 'text',
    },
    {
      name: 'description',
      type: 'textarea'
    },
    {
      name: 'excludeItemInstructions',
      type: 'checkbox',
    },
    {
      name: 'instructions',
      type: 'richText',
    },
    {
      type: 'row',
      fields: [
        {
          name: 'calories',
          type: 'number',
          admin: {
            description: 'per Serving, calculated from ingredients',
            width: '25%',
          },
        },
        {
          name: 'protein',
          type: 'number',
          admin: {
            description: 'per Serving, calculated from ingredients',
            width: '25%',
          },
        },
        {
          name: 'carbohydrates',
          type: 'number',
          admin: {
            description: 'per Serving, calculated from ingredients',
            width: '25%',
          },
        },
        {
          name: 'fat',
          type: 'number',
          admin: {
            description: 'per Serving, calculated from ingredients',
            width: '25%',
          },
        },
      ]
    },
    {
      type: 'row',
      fields: [
        {
          name: 'canBeKosher',
          type: 'checkbox',
          admin: {
            description: 'Calculated from ingredients. Is capable of being vertified Kosher. Not a declaration that ingredient is Kosher.',
            width: '20%',
          },
        },
        {
          name: 'canBeHalal',
          type: 'checkbox',
          admin: {
            description: 'Calculated from ingredients. Is capable of being vertified Halal. Not a declaration that ingredient is Halal.',
            width: '20%',
          },
        },
        {
          name: 'canBeVegan',
          type: 'checkbox',
          admin: {
            description: 'Calculated from ingredients. Is capable of being vertified Vegan. Not a declaration that ingredient is Vegan.',
            width: '20%',
          },
        },
        {
          name: 'canBeVegetarian',
          type: 'checkbox',
          admin: {
            description: 'Calculated from ingredients. Is capable of being vertified Vegetarian. Not a declaration that ingredient is Vegetarian.',
            width: '20%',
          },
        },
        {
          name: 'canBeGlutenFree',
          type: 'checkbox',
          admin: {
            description: 'Calculated from ingredients. Is capable of being vertified Gluten Free. Not a declaration that ingredient is Gluten Free.',
            width: '20%',
          },
        },
      ],
    },
    {
      name: 'items',
      type: 'array',
      fields: [
        {
          type: 'row',
          fields: [
            {
              name: 'item',
              type: 'relationship',
              relationTo: 'mealItems',
              hasMany: false,
              admin: {
                width: '75%',
              },
            },
            {
              name: 'quantity',
              type: 'number',
              admin: {
                description: 'as Servings',
                width: '25%',
              }
            }
          ]
        },
      ],
    },
  ],
  hooks: {
    beforeChange: [
      setMealNutritionOnChange
    ]
  }
}
