/*
 * 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 { smartUnion } from "../../types/smartUnion.js";
import { SDKValidationError } from "../errors/sdkvalidationerror.js";
import {
  Address,
  Address$inboundSchema,
  Address$Outbound,
  Address$outboundSchema,
} from "./address.js";
import {
  CustomerStateBenefitGrant,
  CustomerStateBenefitGrant$inboundSchema,
  CustomerStateBenefitGrant$Outbound,
  CustomerStateBenefitGrant$outboundSchema,
} from "./customerstatebenefitgrant.js";
import {
  CustomerStateMeter,
  CustomerStateMeter$inboundSchema,
  CustomerStateMeter$Outbound,
  CustomerStateMeter$outboundSchema,
} from "./customerstatemeter.js";
import {
  CustomerStateSubscription,
  CustomerStateSubscription$inboundSchema,
  CustomerStateSubscription$Outbound,
  CustomerStateSubscription$outboundSchema,
} from "./customerstatesubscription.js";
import {
  MetadataOutputType,
  MetadataOutputType$inboundSchema,
  MetadataOutputType$Outbound,
  MetadataOutputType$outboundSchema,
} from "./metadataoutputtype.js";
import {
  TaxIDFormat,
  TaxIDFormat$inboundSchema,
  TaxIDFormat$outboundSchema,
} from "./taxidformat.js";

export type CustomerStateIndividualTaxId = string | TaxIDFormat;

/**
 * A customer along with additional state information:
 *
 * @remarks
 *
 * * Active subscriptions
 * * Granted benefits
 * * Active meters
 */
export type CustomerStateIndividual = {
  /**
   * The ID of the customer.
   */
  id: string;
  /**
   * Creation timestamp of the object.
   */
  createdAt: Date;
  /**
   * Last modification timestamp of the object.
   */
  modifiedAt: Date | null;
  metadata: { [k: string]: MetadataOutputType };
  /**
   * The ID of the customer in your system. This must be unique within the organization. Once set, it can't be updated.
   */
  externalId?: string | null | undefined;
  /**
   * The email address of the customer. This must be unique within the organization.
   */
  email: string;
  /**
   * Whether the customer email address is verified. The address is automatically verified when the customer accesses the customer portal using their email address.
   */
  emailVerified: boolean;
  /**
   * The type of customer.
   */
  type: "individual";
  /**
   * The name of the customer.
   */
  name: string | null;
  /**
   * The name that should appear on the customer's invoices. Falls back to the customer name when not explicitly set.
   */
  billingName: string | null;
  billingAddress: Address | null;
  taxId: Array<string | TaxIDFormat | null> | null;
  locale?: string | null | undefined;
  /**
   * The ID of the organization owning the customer.
   */
  organizationId: string;
  /**
   * The ID of the customer's default payment method, if any. Use the payment methods endpoint to retrieve its details.
   */
  defaultPaymentMethodId?: string | null | undefined;
  /**
   * Timestamp for when the customer was soft deleted.
   */
  deletedAt: Date | null;
  avatarUrl: string | null;
  /**
   * The customer's active subscriptions.
   */
  activeSubscriptions: Array<CustomerStateSubscription>;
  /**
   * The customer's active benefit grants.
   */
  grantedBenefits: Array<CustomerStateBenefitGrant>;
  /**
   * The customer's active meters.
   */
  activeMeters: Array<CustomerStateMeter>;
};

/** @internal */
export const CustomerStateIndividualTaxId$inboundSchema: z.ZodMiniType<
  CustomerStateIndividualTaxId,
  unknown
> = smartUnion([z.string(), TaxIDFormat$inboundSchema]);
/** @internal */
export type CustomerStateIndividualTaxId$Outbound = string | string;

/** @internal */
export const CustomerStateIndividualTaxId$outboundSchema: z.ZodMiniType<
  CustomerStateIndividualTaxId$Outbound,
  CustomerStateIndividualTaxId
> = smartUnion([z.string(), TaxIDFormat$outboundSchema]);

export function customerStateIndividualTaxIdToJSON(
  customerStateIndividualTaxId: CustomerStateIndividualTaxId,
): string {
  return JSON.stringify(
    CustomerStateIndividualTaxId$outboundSchema.parse(
      customerStateIndividualTaxId,
    ),
  );
}
export function customerStateIndividualTaxIdFromJSON(
  jsonString: string,
): SafeParseResult<CustomerStateIndividualTaxId, SDKValidationError> {
  return safeParse(
    jsonString,
    (x) => CustomerStateIndividualTaxId$inboundSchema.parse(JSON.parse(x)),
    `Failed to parse 'CustomerStateIndividualTaxId' from JSON`,
  );
}

/** @internal */
export const CustomerStateIndividual$inboundSchema: z.ZodMiniType<
  CustomerStateIndividual,
  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))),
    ),
    metadata: z.record(z.string(), MetadataOutputType$inboundSchema),
    external_id: z.optional(z.nullable(z.string())),
    email: z.string(),
    email_verified: z.boolean(),
    type: z.literal("individual"),
    name: z.nullable(z.string()),
    billing_name: z.nullable(z.string()),
    billing_address: z.nullable(Address$inboundSchema),
    tax_id: z.nullable(
      z.array(z.nullable(smartUnion([z.string(), TaxIDFormat$inboundSchema]))),
    ),
    locale: z.optional(z.nullable(z.string())),
    organization_id: z.string(),
    default_payment_method_id: z.optional(z.nullable(z.string())),
    deleted_at: z.nullable(
      z.pipe(z.iso.datetime({ offset: true }), z.transform(v => new Date(v))),
    ),
    avatar_url: z.nullable(z.string()),
    active_subscriptions: z.array(CustomerStateSubscription$inboundSchema),
    granted_benefits: z.array(CustomerStateBenefitGrant$inboundSchema),
    active_meters: z.array(CustomerStateMeter$inboundSchema),
  }),
  z.transform((v) => {
    return remap$(v, {
      "created_at": "createdAt",
      "modified_at": "modifiedAt",
      "external_id": "externalId",
      "email_verified": "emailVerified",
      "billing_name": "billingName",
      "billing_address": "billingAddress",
      "tax_id": "taxId",
      "organization_id": "organizationId",
      "default_payment_method_id": "defaultPaymentMethodId",
      "deleted_at": "deletedAt",
      "avatar_url": "avatarUrl",
      "active_subscriptions": "activeSubscriptions",
      "granted_benefits": "grantedBenefits",
      "active_meters": "activeMeters",
    });
  }),
);
/** @internal */
export type CustomerStateIndividual$Outbound = {
  id: string;
  created_at: string;
  modified_at: string | null;
  metadata: { [k: string]: MetadataOutputType$Outbound };
  external_id?: string | null | undefined;
  email: string;
  email_verified: boolean;
  type: "individual";
  name: string | null;
  billing_name: string | null;
  billing_address: Address$Outbound | null;
  tax_id: Array<string | string | null> | null;
  locale?: string | null | undefined;
  organization_id: string;
  default_payment_method_id?: string | null | undefined;
  deleted_at: string | null;
  avatar_url: string | null;
  active_subscriptions: Array<CustomerStateSubscription$Outbound>;
  granted_benefits: Array<CustomerStateBenefitGrant$Outbound>;
  active_meters: Array<CustomerStateMeter$Outbound>;
};

/** @internal */
export const CustomerStateIndividual$outboundSchema: z.ZodMiniType<
  CustomerStateIndividual$Outbound,
  CustomerStateIndividual
> = 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()))),
    metadata: z.record(z.string(), MetadataOutputType$outboundSchema),
    externalId: z.optional(z.nullable(z.string())),
    email: z.string(),
    emailVerified: z.boolean(),
    type: z.literal("individual"),
    name: z.nullable(z.string()),
    billingName: z.nullable(z.string()),
    billingAddress: z.nullable(Address$outboundSchema),
    taxId: z.nullable(
      z.array(z.nullable(smartUnion([z.string(), TaxIDFormat$outboundSchema]))),
    ),
    locale: z.optional(z.nullable(z.string())),
    organizationId: z.string(),
    defaultPaymentMethodId: z.optional(z.nullable(z.string())),
    deletedAt: z.nullable(z.pipe(z.date(), z.transform(v => v.toISOString()))),
    avatarUrl: z.nullable(z.string()),
    activeSubscriptions: z.array(CustomerStateSubscription$outboundSchema),
    grantedBenefits: z.array(CustomerStateBenefitGrant$outboundSchema),
    activeMeters: z.array(CustomerStateMeter$outboundSchema),
  }),
  z.transform((v) => {
    return remap$(v, {
      createdAt: "created_at",
      modifiedAt: "modified_at",
      externalId: "external_id",
      emailVerified: "email_verified",
      billingName: "billing_name",
      billingAddress: "billing_address",
      taxId: "tax_id",
      organizationId: "organization_id",
      defaultPaymentMethodId: "default_payment_method_id",
      deletedAt: "deleted_at",
      avatarUrl: "avatar_url",
      activeSubscriptions: "active_subscriptions",
      grantedBenefits: "granted_benefits",
      activeMeters: "active_meters",
    });
  }),
);

export function customerStateIndividualToJSON(
  customerStateIndividual: CustomerStateIndividual,
): string {
  return JSON.stringify(
    CustomerStateIndividual$outboundSchema.parse(customerStateIndividual),
  );
}
export function customerStateIndividualFromJSON(
  jsonString: string,
): SafeParseResult<CustomerStateIndividual, SDKValidationError> {
  return safeParse(
    jsonString,
    (x) => CustomerStateIndividual$inboundSchema.parse(JSON.parse(x)),
    `Failed to parse 'CustomerStateIndividual' from JSON`,
  );
}
