/* Copyright (C) 2024-2025 LibreWeddingPlanner contributors*/

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

export const guestStatuses = ['considered', 'invited', 'confirmed', 'declined', 'tentative'] as const;
export type GuestStatus = typeof guestStatuses[number];

export class Guest implements Entity {
  id?: string;
  name?: string;
  group_name?: string;
  groupId?: string;
  color?: string;
  status?: GuestStatus;
  children?: Guest[];
  formResponses?: {
    busNeeded?: boolean;
    dietaryRestrictions?: string;
    mealPreference?: 'meat' | 'fish' | undefined;
  };

  constructor(
    id?: string,
    name?: string,
    groupName?: string,
    groupId?: string,
    color?: string,
    status?: GuestStatus,
    children?: Guest[],
    formResponses?: {
      busNeeded?: boolean;
      dietaryRestrictions?: string;
      mealPreference?: 'meat' | 'fish' | undefined;
    }
  ) {
    this.id = id;
    this.name = name;
    this.group_name = groupName;
    this.groupId = groupId;
    this.color = color;
    this.status = status;
    this.children = children;
    this.formResponses = formResponses;
  }
}

export class GuestSerializer implements Serializable<Guest> {
  fromJson(data: any): Guest {
    return new Guest(
      data.id,
      data.name,
      data.group?.name,
      data.group?.id,
      data.color,
      data.status,
      data.children,
      data.form_responses
        ? {
            busNeeded: data.form_responses.bus_needed,
            dietaryRestrictions: data.form_responses.dietary_restrictions,
            mealPreference: data.form_responses.meal_preference,
          }
        : undefined
    );
  }

  toJson(guest: Guest): string {
    return JSON.stringify({
      guest: {
        name: guest.name,
        status: guest.status,
        group_id: guest.groupId,
        form_responses: guest.formResponses
          ? {
              bus_needed: guest.formResponses.busNeeded,
              dietary_restrictions: guest.formResponses.dietaryRestrictions,
              meal_preference: guest.formResponses.mealPreference,
            }
          : undefined,
      },
    });
  }

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

