import {
  Schedule,
  ScheduleStatus,
  ScheduleType,
  ScheduleSpan,
} from "../../generated/prisma/client";

type ScheduleWithRelations = Schedule & {
  status?: ScheduleStatus | null;
  type?: ScheduleType | null;
  scheduleSpan?: ScheduleSpan | null;
};

export class ScheduleController {
  public readonly id: number;
  public readonly uid: string;
  public name: string;
  public description: string | null;

  private _data: ScheduleWithRelations;

  constructor(data: ScheduleWithRelations) {
    this.id = data.id;
    this.uid = data.uid;
    this.name = data.name;
    this.description = data.description;
    this._data = data;
  }

  public toJson() {
    const d = this._data;
    return {
      id: d.uid,
      cwId: d.id,
      memberId: d.memberId,
      name: d.name,
      description: d.description,
      closedFlag: d.closedFlag,
      reminderFlag: d.reminderFlag,
      allDayFlag: d.allDayFlag,
      acknowledgementFlag: d.acknowledgementFlag,
      meetingFlag: d.meetingFlag,
      recurringFlag: d.recurringFlag,
      billableFlag: d.billableFlag,
      acknowledgedById: d.acknowledgedById,
      acknowledgedAt: d.acknowledgedAt,
      startDate: d.startDate,
      endDate: d.endDate,
      hoursScheduled: d.hoursScheduled,
      duration: d.duration,
      hoursPerDay: d.hoursPerDay,
      reminderMinutes: d.reminderMinutes,
      closedById: d.closedById,
      closedAt: d.closedAt,
      createdById: d.createdById,
      updatedById: d.updatedById,
      createdAt: d.createdAt,
      updatedAt: d.updatedAt,
      status: d.status
        ? {
            id: d.status.id,
            uid: d.status.uid,
            name: d.status.name,
            color: d.status.color,
          }
        : null,
      type: d.type
        ? {
            id: d.type.id,
            uid: d.type.uid,
            name: d.type.name,
            displayColor: d.type.displayColor,
          }
        : null,
      scheduleSpan: d.scheduleSpan
        ? {
            id: d.scheduleSpan.id,
            scheduleSpanId: d.scheduleSpan.scheduleSpanId,
            spanDesc: d.scheduleSpan.spanDesc,
          }
        : null,
    };
  }
}
