import type { PrismaClient, Prompt } from '@prisma/client';

export interface IPromptRepository {
  findAll(projectId?: string): Promise<Prompt[]>;
  findGlobal(): Promise<Prompt[]>;
  findById(id: string): Promise<Prompt | null>;
  findByNameAndProject(name: string, projectId: string | null): Promise<Prompt | null>;
  create(data: { name: string; content: string; projectId?: string; priority?: number; linkTarget?: string }): Promise<Prompt>;
  update(id: string, data: { content?: string; priority?: number; summary?: string; chapters?: string[] }): Promise<Prompt>;
  delete(id: string): Promise<void>;
}

export class PromptRepository implements IPromptRepository {
  constructor(private readonly prisma: PrismaClient) {}

  async findAll(projectId?: string): Promise<Prompt[]> {
    const include = { project: { select: { name: true } } };
    if (projectId !== undefined) {
      // Project-scoped + global prompts
      return this.prisma.prompt.findMany({
        where: { OR: [{ projectId }, { projectId: null }] },
        include,
        orderBy: { name: 'asc' },
      });
    }
    return this.prisma.prompt.findMany({ include, orderBy: { name: 'asc' } });
  }

  async findGlobal(): Promise<Prompt[]> {
    return this.prisma.prompt.findMany({
      where: { projectId: null },
      include: { project: { select: { name: true } } },
      orderBy: { name: 'asc' },
    });
  }

  async findById(id: string): Promise<Prompt | null> {
    return this.prisma.prompt.findUnique({
      where: { id },
      include: { project: { select: { name: true } } },
    });
  }

  async findByNameAndProject(name: string, projectId: string | null): Promise<Prompt | null> {
    return this.prisma.prompt.findUnique({
      where: { name_projectId: { name, projectId: projectId ?? '' } },
    });
  }

  async create(data: { name: string; content: string; projectId?: string; priority?: number; linkTarget?: string }): Promise<Prompt> {
    return this.prisma.prompt.create({ data });
  }

  async update(id: string, data: { content?: string; priority?: number; summary?: string; chapters?: string[] }): Promise<Prompt> {
    return this.prisma.prompt.update({ where: { id }, data });
  }

  async delete(id: string): Promise<void> {
    await this.prisma.prompt.delete({ where: { id } });
  }
}
