import { ApiProperty } from '@nestjs/swagger';
import type { Category as PrismaCategory } from '@prisma/client';

export class PublicCategoryNodeDto {
  @ApiProperty()
  id!: string;

  @ApiProperty()
  categoryName!: string;

  @ApiProperty({ nullable: true })
  categoryIcon!: string | null;

  @ApiProperty({ nullable: true })
  parentCategoryId!: string | null;

  @ApiProperty({ type: [PublicCategoryNodeDto] })
  children!: PublicCategoryNodeDto[];

  static from(category: PrismaCategory, children: PublicCategoryNodeDto[] = []): PublicCategoryNodeDto {
    return {
      id: category.id.toString(),
      categoryName: category.categoryName,
      categoryIcon: category.categoryIcon,
      parentCategoryId: category.parentCategoryId
        ? category.parentCategoryId.toString()
        : null,
      children,
    };
  }
}
