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

export class Pentest {
  id?: string;
  projectId?: string;
  category: Category;
  refNumber: string;
  childEntries?: Pentest[];
  status: PentestStatus;
  findingIds?: Array<string>;
  commentIds?: Array<string>;

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

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

export function transformPentestToRequestBody(pentest: Pentest): Pentest {
  const transformedPentest = {
    ...pentest,
    projectId: pentest.projectId,
    category: typeof pentest.category === 'number' ? Category[pentest.category] : pentest.category,
    refNumber: pentest.refNumber,
    status: pentest.status,
    findingIds: pentest.findingIds ? pentest.findingIds : [],
    commentIds: pentest.commentIds ? pentest.commentIds : [],
    /* Remove Table Entry Object Properties */
    childEntries: undefined,
    kind: undefined,
    findings: undefined,
    expanded: undefined,
  } as unknown as Pentest;
  return transformedPentest;
}

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