import {v4 as UUID} from 'uuid';
import {Status} from '@shared/models/status.model';
import {Category} from '@shared/models/category.model';

export class Pentest {
  id?: string;
  category: Category;
  refNumber: string;
  childEntries?: Pentest[];
  status: Status;
  findingsIds?: Array<string>;
  commentsIds?: Array<string>;

  constructor(category: Category,
              refNumber: string,
              status: Status,
              id?: string,
              findingsIds?: Array<string>,
              commentsIds?: Array<string>) {
    this.id = id ? id : UUID();
    this.category = category;
    this.refNumber = refNumber;
    this.status = status;
    this.findingsIds = findingsIds ? findingsIds : [];
    this.commentsIds = commentsIds ? commentsIds : [];
  }
}

export interface PentestEntry {
  refNumber: string;
  status: string;
  findings?: number;
  kind?: string;
  childEntries?: PentestEntry[];
  expanded?: boolean;
}

export function transformPentestsToEntries(pentests: Pentest[]): PentestEntry[] {
  const pentestEntries: PentestEntry[] = [];
  pentests.forEach((value: Pentest) => {
    pentestEntries.push({
      refNumber: value.refNumber,
      status: value.status,
      findings: value.findingsIds ? value.findingsIds.length : 0,
      kind: value.childEntries ? 'dir' : 'cell',
      childEntries: value.childEntries ? value.childEntries : null,
      expanded: !!value.childEntries
    } as PentestEntry);
  });
  return pentestEntries;
}
