import type { CollectionConfig } from "payload";

export const Projects: CollectionConfig = {
  slug: "projects",
  access: {
    read: ({ req: { user } }) =>
      user ? true : { status: { equals: "published" } },
  },
  admin: {
    useAsTitle: "title",
    defaultColumns: ["title", "status", "featured", "order", "publishedAt"],
  },
  versions: { drafts: true },
  fields: [
    { name: "title", type: "text", required: true },
    { name: "slug", type: "text", required: true, unique: true, index: true },
    {
      name: "role",
      type: "select",
      options: [
        { label: "Founder / CEO", value: "founder" },
        { label: "Consultant", value: "consultant" },
        { label: "Engineer", value: "engineer" },
        { label: "Infrastructure", value: "infra" },
      ],
    },
    { name: "category", type: "relationship", relationTo: "categories" },
    { name: "summary", type: "textarea" },
    { name: "body", type: "richText" },
    { name: "stack", type: "array", fields: [{ name: "name", type: "text" }] },
    { name: "heroImage", type: "upload", relationTo: "media" },
    {
      name: "gallery",
      type: "array",
      fields: [
        { name: "image", type: "upload", relationTo: "media", required: true },
        { name: "caption", type: "text" },
      ],
    },
    { name: "externalUrl", type: "text" },
    { name: "featured", type: "checkbox", defaultValue: false },
    { name: "order", type: "number", defaultValue: 0 },
    {
      name: "status",
      type: "select",
      defaultValue: "draft",
      required: true,
      options: [
        { label: "Draft", value: "draft" },
        { label: "Published", value: "published" },
      ],
    },
    { name: "publishedAt", type: "date" },
    {
      name: "seo",
      type: "group",
      fields: [
        { name: "title", type: "text" },
        { name: "description", type: "textarea" },
        { name: "image", type: "upload", relationTo: "media" },
      ],
    },
  ],
};
