import { defineConfig } from "tinacms";
import { contentBlockSchema } from "../components/blocks/content";
import { featureBlockSchema } from "../components/blocks/features";
import { heroBlockSchema } from "../components/blocks/hero";
import { testimonialBlockSchema } from "../components/blocks/testimonial";
import { carouselBlockSchema } from "../components/blocks/carousel";
import { headerSchema } from "../components/layout/header";
import { footerSchema } from "../components/layout/footer";
import { richTextTemplates } from "../components/rich-text/rich-text-definitions";
import { titleBlockSchema } from "../components/blocks/title";
import { facebookPageTimelineBlockSchema } from "../components/blocks/facebook-page-timeline";

const config = defineConfig({
    clientId: process.env.NEXT_PUBLIC_TINA_CLIENT_ID!,
    branch:
    process.env.NEXT_PUBLIC_TINA_BRANCH! || // custom branch env override
    process.env.NEXT_PUBLIC_VERCEL_GIT_COMMIT_REF! || // Vercel branch env
    process.env.HEAD!, // Netlify branch env
    token: process.env.TINA_TOKEN!,
    media: {
        // If you wanted cloudinary do this
        // loadCustomStore: async () => {
        //   const pack = await import("next-tinacms-cloudinary");
        //   return pack.TinaCloudCloudinaryMediaStore;
        // },
        // this is the config for the tina cloud media store
        tina: {
            publicFolder: "public",
            mediaRoot: "uploads"
        }
    },
    build: {
        publicFolder: "public", // The public asset folder for your framework
        outputFolder: "admin" // within the public folder
    },
    schema: {
        collections: [
            {
                label: "Blog Posts",
                name: "post",
                path: "content/posts",
                format: "mdx",
                ui: {
                    router: ({ document }) => {
                        return `/posts/${ document._sys.filename }`;
                    }
                },
                fields: [
                    {
                        type: "string",
                        label: "Title",
                        name: "title",
                        isTitle: true,
                        required: true
                    },
                    {
                        type: "image",
                        name: "heroImg",
                        label: "Hero Image"
                    },
                    {
                        type: "rich-text",
                        label: "Excerpt",
                        name: "excerpt"
                    },
                    {
                        type: "reference",
                        label: "Author",
                        name: "author",
                        collections: ["author"]
                    },
                    {
                        type: "datetime",
                        label: "Posted Date",
                        name: "date",
                        ui: {
                            dateFormat: "MMMM DD YYYY",
                            timeFormat: "hh:mm A"
                        }
                    },
                    {
                        type: "rich-text",
                        label: "Body",
                        name: "_body",
                        templates: richTextTemplates,
                        isBody: true
                    }
                ]
            },
            {
                label: "Global",
                name: "global",
                path: "content/global",
                format: "json",
                ui: {
                    global: true
                },
                fields: [
                    headerSchema,
                    footerSchema
                ]
            },
            {
                label: "Authors",
                name: "author",
                path: "content/authors",
                format: "md",
                fields: [
                    {
                        type: "string",
                        label: "Name",
                        name: "name",
                        isTitle: true,
                        required: true
                    },
                    {
                        type: "image",
                        label: "Avatar",
                        name: "avatar"
                    }
                ]
            },
            {
                label: "Pages",
                name: "page",
                path: "content/pages",
                format: "mdx",
                ui: {
                    router: ({ document, collection }) => {
                        if (document._sys.filename === "home") {
                            return "/";
                        }

                        return `/${ document._sys.filename }`;
                    }
                },
                fields: [
                    {
                        type: "string",
                        label: "Title",
                        name: "title",
                        description:
              "The title of the page. This is used to display the title in the CMS",
                        isTitle: true,
                        required: true
                    },
                    {
                        type: "object",
                        list: true,
                        name: "blocks",
                        label: "Sections",
                        ui: {
                            visualSelector: true
                        },
                        templates: [
                            heroBlockSchema,
                            featureBlockSchema,
                            contentBlockSchema,
                            testimonialBlockSchema,
                            carouselBlockSchema,
                            titleBlockSchema,
                            facebookPageTimelineBlockSchema
                        ]
                    }
                ]
            }
        ]
    }
});

export default config;
