import {
  TipPaymentPlansResponseSchema,
  type TipPaymentPlansResponseData,
  type TipPaymentPlansResponseInput,
} from "@/data/schemas/payment/response/tip_payment_plans_response";

import { TipPaymentPlan } from "../tip_payment_plan";

export class TipPaymentPlansResponse {
  declare readonly plans: TipPaymentPlan[];

  private constructor(input: TipPaymentPlansResponseInput) {
    const data = TipPaymentPlansResponseSchema.parse(input);
    Object.assign(this, {
      plans: data.plans.map((plan) => TipPaymentPlan.from(plan)),
    });
    Object.freeze(this);
  }

  static from(input: TipPaymentPlansResponseInput): TipPaymentPlansResponse {
    return new TipPaymentPlansResponse(input);
  }

  static fromJson(json: unknown): TipPaymentPlansResponse {
    return TipPaymentPlansResponse.from(
      json as TipPaymentPlansResponseInput,
    );
  }

  toJson(): TipPaymentPlansResponseData {
    return {
      plans: this.plans.map((plan) => plan.toJson()),
    };
  }
}
