import { CollectionConfig, PaginatedDocs } from "payload";
import { MealItem } from "@/payload-types";
import { setMealItemNutritionOnChange } from "./hooks/beforeChangeMealItems";

export const MealItems: CollectionConfig = {
  slug: 'mealItems',
  admin: {
    description: 'Items that make up a meal, such as mashed potatoes, salad, grilled chicken, etc.',
    useAsTitle: 'name',
    group: 'Nutrition',
  },
  fields: [
    {
      type: 'row',
      fields: [
        {
          name: 'name',
          type: 'text',
          admin: {
            width: '75%',
          }
        },
        {
          name: 'servingSize',
          type: 'number',
          admin: {
            description: 'in Grams',
            width: '25%',
          }
        },
      ],
    },
    {
      name: 'description',
      type: 'textarea',
    },
    {
      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: 'instructions',
      type: 'richText',
    },
    {
      name: 'neededIngredients',
      type: 'array',
      fields: [
        {
          type: 'row',
          fields: [
            {
              name: 'quantity',
              type: 'number',
              admin: {
                description: 'as 100g intervals. (value of 3 = 300g)',
                width: '25%',
              }
            },
            {
              name: 'ingredient',
              type: 'relationship',
              relationTo: 'ingredients',
              hasMany: false,
              admin: {
                width: '75%',
              }
            },
          ]
        }
      ]
    }
  ],
  hooks: {
    beforeChange: [
      setMealItemNutritionOnChange
    ]
  }
}

