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

export type CategoryNode = {
  id: number;
  name: string;
  parentId: number | null;
  children: CategoryNode[];
};

export type FlatCategory = {
  id: number;
  name: string;
  path: string;
};

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

  async findAll() {
    return this.prisma.category.findMany({ orderBy: { name: 'asc' } });
  }

  async findTree(): Promise<CategoryNode[]> {
    const all = await this.prisma.category.findMany({ orderBy: { name: 'asc' } });
    const map = new Map<number, CategoryNode>();
    all.forEach((c) => map.set(c.id, { ...c, children: [] }));
    const roots: CategoryNode[] = [];
    map.forEach((node) => {
      if (node.parentId === null) {
        roots.push(node);
      } else {
        map.get(node.parentId)?.children.push(node);
      }
    });
    return roots;
  }

  async findFlattened(): Promise<FlatCategory[]> {
    const all = await this.prisma.category.findMany({ orderBy: { name: 'asc' } });
    const nameMap = new Map<number, string>();
    all.forEach((c) => nameMap.set(c.id, c.name));
    return all.map((c) => ({
      id: c.id,
      name: c.name,
      path: c.parentId ? `${nameMap.get(c.parentId) ?? ''} > ${c.name}` : c.name,
    }));
  }
}
