import { DataSource } from 'typeorm';

import { SeedObjectMetadataIds } from 'src/database/typeorm-seeds/metadata/object-metadata';
import { FieldMetadataType } from 'src/metadata/field-metadata/field-metadata.entity';
import { SeedWorkspaceId } from 'src/database/typeorm-seeds/core/workspaces';

const fieldMetadataTableName = 'fieldMetadata';

export enum SeedPersonFieldMetadataIds {
  Id = '20202020-03cd-4cd0-9afc-92077b69f24f',
  CreatedAt = '20202020-bec0-4cf0-bf1c-8b2ed21f027a',
  UpdatedAt = '20202020-2bf4-42b8-8718-a3e852bfa6a6',

  Name = '20202020-9b56-4888-bfe3-f6f59aa999e3',

  Email = '20202020-8a96-4e4b-86fd-ea126530e0c1',
  LinkedinLink = '20202020-dcf6-445a-b543-37e55de43c25',
  XLink = '20202020-a3a7-4f63-9303-10226f6055be',
  JobTitle = '20202020-3b86-413e-ab56-0ebd1a583ff3',
  Phone = '20202020-486f-45f9-bbdf-aac18b1831c0',
  City = '20202020-78f8-4b4c-90ff-86adf77590f5',
  AvatarUrl = '20202020-bb05-45cb-aa2a-71b58d49dd23',

  Company = '20202020-64e1-4080-b6ad-db03c3809885',
  CompanyForeignKey = '20202020-64e1-4080-b6ad-db03c3809f8b',
  ContactForOpportunities = '20202020-2c2e-418e-ba2d-d28434ff02a6',
  Opportunities = '20202020-6bc4-4905-a9d3-4f8d616a19e1',
  ActivityTargets = '20202020-f285-4115-a46c-116522986b29',
  Favorites = '20202020-6268-4165-8774-9aaf45db2b25',
  Attachments = '20202020-5883-4bb3-a308-65271901a1d4',
}

export const seedPersonFieldMetadata = async (
  workspaceDataSource: DataSource,
  schemaName: string,
) => {
  await workspaceDataSource
    .createQueryBuilder()
    .insert()
    .into(`${schemaName}.${fieldMetadataTableName}`, [
      'id',
      'objectMetadataId',
      'isCustom',
      'workspaceId',
      'isActive',
      'type',
      'name',
      'label',
      'targetColumnMap',
      'description',
      'icon',
      'isNullable',
      'isSystem',
      'defaultValue',
    ])
    .orIgnore()
    .values([
      // Default fields
      {
        id: SeedPersonFieldMetadataIds.Id,
        objectMetadataId: SeedObjectMetadataIds.Person,
        isCustom: false,
        workspaceId: SeedWorkspaceId,
        isActive: true,
        type: FieldMetadataType.UUID,
        name: 'id',
        label: 'Id',
        targetColumnMap: {
          value: 'id',
        },
        description: undefined,
        icon: undefined,
        isNullable: false,
        isSystem: true,
        defaultValue: { type: 'uuid' },
      },
      {
        id: SeedPersonFieldMetadataIds.CreatedAt,
        objectMetadataId: SeedObjectMetadataIds.Person,
        isCustom: false,
        workspaceId: SeedWorkspaceId,
        isActive: true,
        type: FieldMetadataType.DATE_TIME,
        name: 'createdAt',
        label: 'Creation date',
        targetColumnMap: {
          value: 'createdAt',
        },
        description: undefined,
        icon: 'IconCalendar',
        isNullable: false,
        isSystem: false,
        defaultValue: { type: 'now' },
      },
      {
        id: SeedPersonFieldMetadataIds.UpdatedAt,
        objectMetadataId: SeedObjectMetadataIds.Person,
        isCustom: false,
        workspaceId: SeedWorkspaceId,
        isActive: true,
        type: FieldMetadataType.DATE_TIME,
        name: 'updatedAt',
        label: 'Update date',
        targetColumnMap: {
          value: 'updatedAt',
        },
        description: undefined,
        icon: 'IconCalendar',
        isNullable: false,
        isSystem: true,
        defaultValue: { type: 'now' },
      },
      // Main Identifier
      {
        id: SeedPersonFieldMetadataIds.Name,
        objectMetadataId: SeedObjectMetadataIds.Person,
        isCustom: false,
        workspaceId: SeedWorkspaceId,
        isActive: true,
        type: FieldMetadataType.FULL_NAME,
        name: 'name',
        label: 'Name',
        targetColumnMap: {
          firstName: 'nameFirstName',
          lastName: 'nameLastName',
        },
        description: 'Contact’s name',
        icon: 'IconUser',
        isNullable: true,
        isSystem: false,
        defaultValue: { firstName: '', lastName: '' },
      },

      // Scalar Fields
      {
        id: SeedPersonFieldMetadataIds.Email,
        objectMetadataId: SeedObjectMetadataIds.Person,
        isCustom: false,
        workspaceId: SeedWorkspaceId,
        isActive: true,
        type: FieldMetadataType.EMAIL,
        name: 'email',
        label: 'Email',
        targetColumnMap: {
          value: 'email',
        },
        description: 'Contact’s Email',
        icon: 'IconMail',
        isNullable: true,
        isSystem: false,
        defaultValue: undefined,
      },
      {
        id: SeedPersonFieldMetadataIds.LinkedinLink,
        objectMetadataId: SeedObjectMetadataIds.Person,
        isCustom: false,
        workspaceId: SeedWorkspaceId,
        isActive: true,
        type: FieldMetadataType.LINK,
        name: 'linkedinLink',
        label: 'Linkedin',
        targetColumnMap: {
          label: 'linkedinLinkLabel',
          url: 'linkedinLinkUrl',
        },
        description: 'Contact’s Linkedin account',
        icon: 'IconBrandLinkedin',
        isNullable: true,
        isSystem: false,
        defaultValue: undefined,
      },
      {
        id: SeedPersonFieldMetadataIds.XLink,
        objectMetadataId: SeedObjectMetadataIds.Person,
        isCustom: false,
        workspaceId: SeedWorkspaceId,
        isActive: true,
        type: FieldMetadataType.LINK,
        name: 'xLink',
        label: 'X',
        targetColumnMap: {
          label: 'xLinkLabel',
          url: 'xLinkUrl',
        },
        description: 'Contact’s X/Twitter account',
        icon: 'IconBrandX',
        isNullable: true,
        isSystem: false,
        defaultValue: undefined,
      },
      {
        id: SeedPersonFieldMetadataIds.JobTitle,
        objectMetadataId: SeedObjectMetadataIds.Person,
        isCustom: false,
        workspaceId: SeedWorkspaceId,
        isActive: true,
        type: FieldMetadataType.TEXT,
        name: 'jobTitle',
        label: 'Job Title',
        targetColumnMap: {
          value: 'jobTitle',
        },
        description: 'Contact’s job title',
        icon: 'IconBriefcase',
        isNullable: true,
        isSystem: false,
        defaultValue: { value: '' },
      },
      {
        id: SeedPersonFieldMetadataIds.Phone,
        objectMetadataId: SeedObjectMetadataIds.Person,
        isCustom: false,
        workspaceId: SeedWorkspaceId,
        isActive: true,
        type: FieldMetadataType.TEXT,
        name: 'phone',
        label: 'Phone',
        targetColumnMap: {
          value: 'phone',
        },
        description: 'Contact’s phone number',
        icon: 'IconPhone',
        isNullable: true,
        isSystem: false,
        defaultValue: { value: '' },
      },
      {
        id: SeedPersonFieldMetadataIds.City,
        objectMetadataId: SeedObjectMetadataIds.Person,
        isCustom: false,
        workspaceId: SeedWorkspaceId,
        isActive: true,
        type: FieldMetadataType.TEXT,
        name: 'city',
        label: 'City',
        targetColumnMap: {
          value: 'city',
        },
        description: 'Contact’s city',
        icon: 'IconMap',
        isNullable: true,
        isSystem: false,
        defaultValue: { value: '' },
      },
      {
        id: SeedPersonFieldMetadataIds.AvatarUrl,
        objectMetadataId: SeedObjectMetadataIds.Person,
        isCustom: false,
        workspaceId: SeedWorkspaceId,
        isActive: true,
        type: FieldMetadataType.TEXT,
        name: 'avatarUrl',
        label: 'Avatar',
        targetColumnMap: {
          value: 'avatarUrl',
        },
        description: 'Contact’s avatar',
        icon: 'IconFileUpload',
        isNullable: true,
        isSystem: true,
        defaultValue: { value: '' },
      },

      // Relationships
      {
        id: SeedPersonFieldMetadataIds.Company,
        objectMetadataId: SeedObjectMetadataIds.Person,
        isCustom: false,
        workspaceId: SeedWorkspaceId,
        isActive: true,
        type: FieldMetadataType.RELATION,
        name: 'company',
        label: 'Company',
        targetColumnMap: {},
        description: 'Contact’s company',
        icon: 'IconBuildingSkyscraper',
        isNullable: true,
        isSystem: false,
        defaultValue: undefined,
      },
      {
        id: SeedPersonFieldMetadataIds.CompanyForeignKey,
        objectMetadataId: SeedObjectMetadataIds.Person,
        isCustom: false,
        workspaceId: SeedWorkspaceId,
        isActive: true,
        type: FieldMetadataType.UUID,
        name: 'companyId',
        label: 'Company ID (foreign key)',
        targetColumnMap: {},
        description: 'Foreign key for company',
        icon: undefined,
        isNullable: true,
        isSystem: true,
        defaultValue: undefined,
      },
      {
        id: SeedPersonFieldMetadataIds.ContactForOpportunities,
        objectMetadataId: SeedObjectMetadataIds.Person,
        isCustom: false,
        workspaceId: SeedWorkspaceId,
        isActive: true,
        type: FieldMetadataType.RELATION,
        name: 'pointOfContactForOpportunities',
        label: 'POC for Opportunities',
        targetColumnMap: {},
        description: 'Point of Contact for Opportuniites',
        icon: 'IconTargetArrow',
        isNullable: true,
        isSystem: false,
        defaultValue: undefined,
      },
      {
        id: SeedPersonFieldMetadataIds.ActivityTargets,
        objectMetadataId: SeedObjectMetadataIds.Person,
        isCustom: false,
        workspaceId: SeedWorkspaceId,
        isActive: true,
        type: FieldMetadataType.RELATION,
        name: 'activityTargets',
        label: 'Activities',
        targetColumnMap: {},
        description: 'Activities tied to the contact',
        icon: 'IconCheckbox',
        isNullable: true,
        isSystem: false,
        defaultValue: undefined,
      },
      {
        id: SeedPersonFieldMetadataIds.Opportunities,
        objectMetadataId: SeedObjectMetadataIds.Person,
        isCustom: false,
        workspaceId: SeedWorkspaceId,
        isActive: true,
        type: FieldMetadataType.RELATION,
        name: 'opportunities',
        label: 'Opportunities',
        targetColumnMap: {},
        description: 'Opportunities linked to the contact.',
        icon: 'IconTargetArrow',
        isNullable: true,
        isSystem: false,
        defaultValue: undefined,
      },
      {
        id: SeedPersonFieldMetadataIds.Favorites,
        objectMetadataId: SeedObjectMetadataIds.Person,
        isCustom: false,
        workspaceId: SeedWorkspaceId,
        isActive: true,
        type: FieldMetadataType.RELATION,
        name: 'favorites',
        label: 'Favorites',
        targetColumnMap: {},
        description: 'Favorites linked to the contact',
        icon: 'IconHeart',
        isNullable: true,
        isSystem: false,
        defaultValue: undefined,
      },
      {
        id: SeedPersonFieldMetadataIds.Attachments,
        objectMetadataId: SeedObjectMetadataIds.Person,
        isCustom: false,
        workspaceId: SeedWorkspaceId,
        isActive: true,
        type: FieldMetadataType.RELATION,
        name: 'attachments',
        label: 'Attachments',
        targetColumnMap: {},
        description: 'Attachments linked to the contact.',
        icon: 'IconFileImport',
        isNullable: true,
        isSystem: false,
        defaultValue: undefined,
      },
    ])
    .execute();
};
