import { z } from 'zod';
import { NodeModel } from './core';
import { fieldValueSchema } from './fields';
import { blockSchema } from './block';

export const recordAttributesSchema = z.object({
  type: z.literal('record'),
  parentId: z.string(),
  databaseId: z.string(),
  name: z.string(),
  avatar: z.string().nullable().optional(),
  fields: z.record(z.string(), fieldValueSchema),
  content: z.record(blockSchema),
});

export type RecordAttributes = z.infer<typeof recordAttributesSchema>;

export const recordModel: NodeModel = {
  type: 'record',
  schema: recordAttributesSchema,
  canCreate: async (context, attributes) => {
    if (attributes.type !== 'record') {
      return false;
    }

    return context.hasCollaboratorAccess();
  },
  canUpdate: async (context, node, attributes) => {
    if (attributes.type !== 'record' || node.type !== 'record') {
      return false;
    }

    return context.hasCollaboratorAccess();
  },
  canDelete: async (context, _) => {
    return context.hasCollaboratorAccess();
  },
};
