import type { CollectionConfig } from 'payload'

export const Cars: CollectionConfig = {
  slug: 'cars',
  admin: {
    useAsTitle: 'type',
  },
  fields: [
    {
      name: 'type',
      type: 'text',
      label: 'Title',
      required: true,
    },
    {
      name: 'price',
      type: 'number',
      label: 'Price',
      required: true,
    },
    {
      name: 'numberOfSeats',
      type: 'text',
      label: 'Number of seats',
      required: true,
    },
    {
      name: 'carColor',
      type: 'text',
      label: 'Car Color',
      required: true,
    },
    {
      name: 'popularity',
      type: 'number',
      label: 'Popularity (1 - min, 5 - max)',
      min: 1,
      max: 5,
      required: true,
    },
    {
      name: 'departure',
      type: 'array',
      label: 'Departure from',
      required: true,
      interfaceName: 'Departure from',
      fields: [
        {
          name: 'city',
          type: 'text',
          label: 'City',
          required: true,
        },
      ],
    },
    {
      name: 'returns',
      type: 'array',
      label: 'Return location',
      required: true,
      interfaceName: 'Return location',
      fields: [
        {
          name: 'city',
          type: 'text',
          label: 'name',
          required: true,
        },
      ],
    },
    {
      name: 'carLabels',
      type: 'array',
      label: 'Car Labels',
      required: true,
      interfaceName: 'CarLabels',
      fields: [
        {
          name: 'name',
          type: 'text',
          localized: true,
          label: 'name',
          required: true,
        },
      ],
    },
    {
      name: 'withDriver',
      type: 'checkbox',
      label: 'Can be used with driver',
      required: true,
      defaultValue: false,
    },
    {
      name: 'withoutDriver',
      type: 'checkbox',
      label: 'Can be used without driver',
      required: true,
      defaultValue: true,
    },
    {
      name: 'gallery',
      type: 'array',
      label: 'Photo Gallery',
      required: true,
      interfaceName: 'Photo Gallery',
      fields: [
        {
          name: 'image',
          type: 'upload',
          relationTo: 'media',
          label: 'Image',
          required: true,
        },
      ],
    },
    {
      name: 'carDescription',
      type: 'richText',
      label: 'Car Description',
      required: true,
    },
    {
      name: 'specifications',
      type: 'array',
      label: 'Specifications',
      required: true,
      minRows: 1,
      interfaceName: 'Specifications',
      fields: [
        {
          name: 'name',
          type: 'text',
          label: 'Name',
          required: true,
        },
        {
          name: 'value',
          type: 'text',
          label: 'Value',
          required: true,
        },
      ],
    },
    {
      name: 'image',
      type: 'upload',
      relationTo: 'media',
      label: 'Image',
      required: true,
    },
    {
      name: 'carsId',
      type: 'text',
      label: 'cars unique ID',
      admin: {
        readOnly: true,
      },
      hooks: {
        beforeChange: [
          ({ value }) => {
            return value || crypto.randomUUID()
          },
        ],
      },
    },
  ],
}
