import { z } from "zod";
import { ZIntegrationBase, ZIntegrationBaseSurveyData } from "./shared-types";

export const ZIntegrationNotionCredential = z.object({
  access_token: z.string(),
  bot_id: z.string(),
  token_type: z.string(),
  duplicated_template_id: z.string().nullable(),
  owner: z.object({
    type: z.string(),
    workspace: z.boolean().nullish(),
    user: z
      .object({
        id: z.string(),
        name: z.string().nullish(),
        type: z.string().nullish(),
        object: z.string(),
        person: z
          .object({
            email: z.email(),
          })
          .nullish(),
        avatar_url: z.string().nullish(),
      })
      .nullable(),
  }),
  workspace_icon: z.string().nullable(),
  workspace_id: z.string(),
  workspace_name: z.string().nullable(),
});

export type TIntegrationNotionCredential = z.infer<typeof ZIntegrationNotionCredential>;

export const ZIntegrationNotionConfigData = z
  .object({
    // element -> notion database column mapping
    mapping: z.array(
      z.object({
        element: z.object({
          id: z.string(),
          name: z.string(),
          type: z.string(),
        }),
        column: z.object({
          id: z.string(),
          name: z.string(),
          type: z.string(),
        }),
      })
    ),
    databaseId: z.string(),
    databaseName: z.string(),
  })
  .extend(
    ZIntegrationBaseSurveyData.omit({
      elementIds: true,
      elements: true,
    }).shape
  );

export type TIntegrationNotionConfigData = z.infer<typeof ZIntegrationNotionConfigData>;

export const ZIntegrationNotionConfig = z.object({
  key: ZIntegrationNotionCredential,
  data: z.array(ZIntegrationNotionConfigData),
});

export type TIntegrationNotionConfig = z.infer<typeof ZIntegrationNotionConfig>;

export const ZIntegrationNotion = ZIntegrationBase.extend({
  type: z.literal("notion"),
  config: ZIntegrationNotionConfig,
});

export type TIntegrationNotion = z.infer<typeof ZIntegrationNotion>;

export const ZIntegrationNotionInput = z.object({
  type: z.literal("notion"),
  config: ZIntegrationNotionConfig,
});

export type TIntegrationNotionInput = z.infer<typeof ZIntegrationNotionInput>;

export const ZIntegrationNotionDatabase = z.object({
  id: z.string(),
  name: z.string(),
  properties: z.object({}),
});

export type TIntegrationNotionDatabase = z.infer<typeof ZIntegrationNotionDatabase>;
