// Prisma-based repository/member logic
import { prisma } from "./prisma-client"

// Get all repositories
export async function getAllRepositories() {
  return await prisma.repositories.findMany()
}

// Get a single repository by ID
export async function getRepositoryById(repoId: string) {
  return await prisma.repositories.findUnique({
    where: { id: repoId },
  })
}

export type GetRepositoriesForAccountPayload =
  | { userId: string; username: string } // personal account
  | { orgId: string } // organization account

export async function getRepositoriesForAccount(payload: GetRepositoriesForAccountPayload) {
  const where =
    "orgId" in payload
      ? { organization_id: payload.orgId }
      : {
          AND: [
            {
              OR: [
                {
                  repository_members: {
                    some: {
                      user_id: payload.userId,
                    },
                  },
                },
                {
                  optimization_events: {
                    some: {
                      current_username: payload.username,
                    },
                  },
                },
              ],
            },
          ],
        }
  return await prisma.repositories.findMany({
    where,
  })
}

export async function upsertRepository(data: {
  github_repo_id: string
  installation_id: number
  name: string
  full_name: string
  is_private: boolean
  organization_id?: string
}) {
  return await prisma.repositories.upsert({
    where: { github_repo_id: data.github_repo_id },
    update: {
      name: data.name,
      full_name: data.full_name,
      is_private: data.is_private,
      installation_id: data.installation_id,
      organization_id: data.organization_id,
    },
    create: {
      github_repo_id: data.github_repo_id,
      installation_id: data.installation_id,
      name: data.name,
      full_name: data.full_name,
      is_private: data.is_private,
      organization_id: data.organization_id,
      is_active: true,
    },
  })
}
// Add a new repository
export async function createRepository(data: {
  github_repo_id: string
  installation_id: number
  name: string
  full_name: string
  is_private: boolean
}) {
  return await prisma.repositories.create({
    data: {
      ...data,
    },
  })
}

// Get all members of a repository
export async function getRepositoryMembers(repoId: string) {
  return await prisma.repository_members.findMany({
    where: {
      repository_id: repoId,
    },
    include: {
      user: true,
    },
  })
}

// Create a new repository member
export async function createRepositoryMember(data: {
  repository_id: string
  user_id: string
  role: string
  added_by?: string
}) {
  return await prisma.repository_members.create({
    data: {
      ...data,
    },
  })
}

export async function upsertRepositoryMember(data: {
  repository_id: string
  user_id: string
  role: string
  added_by?: string
}) {
  return await prisma.repository_members.upsert({
    where: { repository_id_user_id: { repository_id: data.repository_id, user_id: data.user_id } },
    update: {
      role: data.role,
      added_by: data.added_by,
    },
    create: {
      repository_id: data.repository_id,
      user_id: data.user_id,
      role: data.role,
      added_by: data.added_by,
    },
  })
}

// Get a repository member by ID
export async function getRepositoryMemberById(memberId: string) {
  return await prisma.repository_members.findUnique({
    where: { id: memberId },
    include: {
      user: true,
    },
  })
}

// Update a repository member's role
export async function updateRepositoryMemberRole(memberId: string, role: string) {
  return await prisma.repository_members.update({
    where: { id: memberId },
    data: { role },
  })
}

// Delete a repository member
export async function deleteRepositoryMember(memberId: string) {
  return await prisma.repository_members.delete({
    where: { id: memberId },
  })
}
