import { Injectable } from '@nestjs/common';
import { PrismaService } from '../prisma/prisma.service';

@Injectable()
export class BlocksService {
  constructor(private prisma: PrismaService) {}

  findAll() {
    return this.prisma.block.findMany({ orderBy: { createdAt: 'asc' } });
  }

  async findByPath(path: string) {
    const existing = await this.prisma.block.findUnique({ where: { path } });
    if (existing) return existing;
    return this.prisma.block.create({
      data: { path, name: path, version: 'v0.1', isInPreview: false },
    });
  }

  update(path: string, data: { version?: string; isInPreview?: boolean }) {
    return this.prisma.block.update({ where: { path }, data });
  }
}
