import type {Prisma, Tag} from '@/generated/prisma/client'
import {prismaClient} from './prismaClient'
import type {TagWithContacts} from '@/models/tags'
import {tagWithContactsInclude} from '@/models/tags'

export type CreateTagParams = Prisma.TagCreateInput

export async function createTag(tagInput: CreateTagParams): Promise<Tag> {
  return prismaClient.tag.create({data: tagInput})
}

export async function getTags(): Promise<Tag[]> {
  return prismaClient.tag.findMany()
}

export async function getTag(id: string): Promise<TagWithContacts> {
  return prismaClient.tag.findUniqueOrThrow({
    where: {id},
    include: tagWithContactsInclude,
  })
}

export async function deleteTag(id: string): Promise<void> {
  await prismaClient.tag.delete({where: {id}})
}

export type UpdateTagParams = Prisma.TagUpdateInput & {id: string}

export async function updateTag(tag: UpdateTagParams): Promise<Tag> {
  return prismaClient.tag.update({where: {id: tag.id}, data: tag})
}

export async function linkTagsToContact(tagId: string, contactIds: string[]): Promise<void> {
  throw new Error('Not implemented')
}

export async function disconnectTagsFromContact(tagId: string, contactIds: string[]): Promise<void> {
  throw new Error('Not implemented')
}
