/*
 * Code generated by Speakeasy (https://speakeasy.com). DO NOT EDIT.
 */

import * as z from "zod/v4-mini";
import { remap as remap$ } from "../../lib/primitives.js";
import { safeParse } from "../../lib/schemas.js";
import { Result as SafeParseResult } from "../../types/fp.js";
import { SDKValidationError } from "../errors/sdkvalidationerror.js";
import {
  MemberRole,
  MemberRole$inboundSchema,
  MemberRole$outboundSchema,
} from "./memberrole.js";

/**
 * A member of a customer.
 */
export type Member = {
  /**
   * The ID of the member.
   */
  id: string;
  /**
   * Creation timestamp of the object.
   */
  createdAt: Date;
  /**
   * Last modification timestamp of the object.
   */
  modifiedAt: Date | null;
  /**
   * The ID of the customer this member belongs to.
   */
  customerId: string;
  /**
   * The email address of the member.
   */
  email: string;
  /**
   * The name of the member.
   */
  name: string | null;
  /**
   * The ID of the member in your system. This must be unique within the customer.
   */
  externalId: string | null;
  role: MemberRole;
};

/** @internal */
export const Member$inboundSchema: z.ZodMiniType<Member, unknown> = z.pipe(
  z.object({
    id: z.string(),
    created_at: z.pipe(
      z.iso.datetime({ offset: true }),
      z.transform(v => new Date(v)),
    ),
    modified_at: z.nullable(
      z.pipe(z.iso.datetime({ offset: true }), z.transform(v => new Date(v))),
    ),
    customer_id: z.string(),
    email: z.string(),
    name: z.nullable(z.string()),
    external_id: z.nullable(z.string()),
    role: MemberRole$inboundSchema,
  }),
  z.transform((v) => {
    return remap$(v, {
      "created_at": "createdAt",
      "modified_at": "modifiedAt",
      "customer_id": "customerId",
      "external_id": "externalId",
    });
  }),
);
/** @internal */
export type Member$Outbound = {
  id: string;
  created_at: string;
  modified_at: string | null;
  customer_id: string;
  email: string;
  name: string | null;
  external_id: string | null;
  role: string;
};

/** @internal */
export const Member$outboundSchema: z.ZodMiniType<Member$Outbound, Member> = z
  .pipe(
    z.object({
      id: z.string(),
      createdAt: z.pipe(z.date(), z.transform(v => v.toISOString())),
      modifiedAt: z.nullable(
        z.pipe(z.date(), z.transform(v => v.toISOString())),
      ),
      customerId: z.string(),
      email: z.string(),
      name: z.nullable(z.string()),
      externalId: z.nullable(z.string()),
      role: MemberRole$outboundSchema,
    }),
    z.transform((v) => {
      return remap$(v, {
        createdAt: "created_at",
        modifiedAt: "modified_at",
        customerId: "customer_id",
        externalId: "external_id",
      });
    }),
  );

export function memberToJSON(member: Member): string {
  return JSON.stringify(Member$outboundSchema.parse(member));
}
export function memberFromJSON(
  jsonString: string,
): SafeParseResult<Member, SDKValidationError> {
  return safeParse(
    jsonString,
    (x) => Member$inboundSchema.parse(JSON.parse(x)),
    `Failed to parse 'Member' from JSON`,
  );
}
