import { type PrismaClient, type Secret } from '@prisma/client';
import type { ISecretRepository } from './interfaces.js';
import type { CreateSecretInput, UpdateSecretInput } from '../validation/secret.schema.js';

export class SecretRepository implements ISecretRepository {
  constructor(private readonly prisma: PrismaClient) {}

  async findAll(): Promise<Secret[]> {
    return this.prisma.secret.findMany({ orderBy: { name: 'asc' } });
  }

  async findById(id: string): Promise<Secret | null> {
    return this.prisma.secret.findUnique({ where: { id } });
  }

  async findByName(name: string): Promise<Secret | null> {
    return this.prisma.secret.findUnique({ where: { name } });
  }

  async create(data: CreateSecretInput): Promise<Secret> {
    return this.prisma.secret.create({
      data: {
        name: data.name,
        data: data.data,
      },
    });
  }

  async update(id: string, data: UpdateSecretInput): Promise<Secret> {
    return this.prisma.secret.update({
      where: { id },
      data: { data: data.data },
    });
  }

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