/* Copyright (C) 2024 Manuel Bustillo*/

import { Serializable } from "../api/abstract-api";
import { Entity } from "./definitions";
import { Guest } from "./guest";

export type Discomfort = {
  discomfort: number;
  breakdown: {
    tableSizePenalty: number;
    cohesionPenalty: number;
  }
}

export type Table = {
  number: number;
  guests: Guest[];
  discomfort: Discomfort;
}

export class TableSimulation implements Entity {
  id: string;
  tables: Table[];

  constructor(id: string, tables: Table[]) {
    this.id = id;
    this.tables = tables;
  }
}

export class TableSimulationSerializer implements Serializable<TableSimulation> {
  fromJson(data: any): TableSimulation {
    return new TableSimulation(data.id, data.tables.map((table: any) => {
      return {
        number: table.number,
        guests: table.guests.map((guest: any) => new Guest(guest.id, guest.name, guest.group?.name, guest.group?.id, guest.color)),
        discomfort: {
          discomfort: table.discomfort.discomfort,
          breakdown: {
            tableSizePenalty: table.discomfort.breakdown.table_size_penalty,
            cohesionPenalty: table.discomfort.breakdown.cohesion_penalty,
          }
        },
      }
    }));
  }

  toJson(simulation: TableSimulation): string {
    return JSON.stringify({ simulation: { tables: simulation.tables.map((table) => {
      return {
        number: table.number,
        guests: table.guests.map((guest) => {
          return {
            id: guest.id,
            name: guest.name,
            group_id: guest.groupId,
            color: guest.color,
            status: guest.status,
            children: guest.children,
          }
        }),
        discomfort: table.discomfort,
      }
    }) } });
  }

  apiPath(): string {
    return 'tables_arrangements';
  }
}