import formatSlug from "@/utils/payload/formatSlug";
import setAuthor from "@/utils/payload/setAuthor";
import type { CollectionConfig } from "payload";

export const Properties: CollectionConfig = {
  slug: "properties",
  labels: { plural: "Properties", singular: "Property" },
  versions: {
    drafts: {
      validate: true,
    },
  },
  fields: [
    {
      name: "property_type",
      label: "Type",
      type: "select",
      options: [
        { label: "Rent", value: "rent" },
        { label: "Sell", value: "sell" },
      ],
      required: true,
    },
    {
      name: "name",
      type: "text",
      required: true,
    },
    {
      name: "slug",
      type: "text",
      admin: {
        position: "sidebar",
      },
      hooks: {
        beforeValidate: [formatSlug("name")],
      },
    },
    {
      name: "images",
      type: "upload",
      relationTo: "media",
      hasMany: true,
      minRows: 1,
      required: true,
    },
    {
      name: "aboutGroup",
      label: "About",
      type: "group",
      fields: [
        {
          name: "description",
          type: "richText",
          required: true,
        },
        {
          name: "area",
          label: "Area (Sqft)",
          type: "number",
          required: true,
        },
        {
          name: "bathrooms_count",
          label: "Total Bathrooms",
          type: "number",
        },
        {
          name: "bedrooms_count",
          label: "Total Bedrooms",
          type: "number",
        },
      ],
    },
    {
      name: "addressGroup",
      label: "Address",
      type: "group",
      fields: [
        {
          name: "state_code",
          label: "State",
          type: "text",
          admin: {
            components: {
              Field: {
                path: "/components/payload-custom/InputState",
              },
            },
          },
        },
        {
          name: "city_code",
          label: "City",
          type: "text",
          admin: {
            components: {
              Field: {
                path: "/components/payload-custom/InputCity",
              },
            },
          },
        },
        {
          name: "zip_code",
          label: "Zip",
          type: "text",
          required: true,
        },
        {
          name: "address",
          label: "Address",
          type: "text",
          required: true,
        },
      ],
    },
    {
      name: "features",
      type: "relationship",
      relationTo: "propertyFeatures",
      required: true,
      hasMany: true,
      minRows: 1,
    },
    {
      name: "base_price",
      label: "Price",
      type: "number",
      required: true,
    },
    {
      name: "additional_price",
      label: "Additional Price",
      type: "array",
      fields: [
        {
          name: "name",
          type: "text",
          required: true,
        },
        {
          name: "price",
          type: "number",
          required: true,
        },
      ],
    },
    {
      name: "embed_map_url",
      label: "Embed Google Map URL",
      type: "text",
    },
    {
      name: "createdBy",
      type: "relationship",
      relationTo: "users",
      hooks: {
        beforeChange: [setAuthor],
      },
      admin: {
        hidden: true,
      },
    },
    {
      name: "updatedBy",
      type: "relationship",
      relationTo: "users",
      hooks: {
        beforeChange: [setAuthor],
      },
      admin: {
        hidden: true,
      },
    },
  ],
  admin: {
    hideAPIURL: true,
    group: "Properties",
    useAsTitle: "name",
  },
  access: {
    read: () => true,
  },
};
