/*
 * 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 {
  LicenseKeyCustomer,
  LicenseKeyCustomer$inboundSchema,
} from "./licensekeycustomer.js";
import {
  LicenseKeyStatus,
  LicenseKeyStatus$inboundSchema,
} from "./licensekeystatus.js";

export type LicenseKeyRead = {
  /**
   * The ID of the object.
   */
  id: string;
  /**
   * Creation timestamp of the object.
   */
  createdAt: Date;
  /**
   * Last modification timestamp of the object.
   */
  modifiedAt: Date | null;
  organizationId: string;
  customerId: string;
  customer: LicenseKeyCustomer;
  /**
   * The benefit ID.
   */
  benefitId: string;
  key: string;
  displayKey: string;
  status: LicenseKeyStatus;
  limitActivations: number | null;
  usage: number;
  limitUsage: number | null;
  validations: number;
  lastValidatedAt: Date | null;
  expiresAt: Date | null;
};

/** @internal */
export const LicenseKeyRead$inboundSchema: z.ZodMiniType<
  LicenseKeyRead,
  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))),
    ),
    organization_id: z.string(),
    customer_id: z.string(),
    customer: LicenseKeyCustomer$inboundSchema,
    benefit_id: z.string(),
    key: z.string(),
    display_key: z.string(),
    status: LicenseKeyStatus$inboundSchema,
    limit_activations: z.nullable(z.int()),
    usage: z.int(),
    limit_usage: z.nullable(z.int()),
    validations: z.int(),
    last_validated_at: z.nullable(
      z.pipe(z.iso.datetime({ offset: true }), z.transform(v => new Date(v))),
    ),
    expires_at: z.nullable(
      z.pipe(z.iso.datetime({ offset: true }), z.transform(v => new Date(v))),
    ),
  }),
  z.transform((v) => {
    return remap$(v, {
      "created_at": "createdAt",
      "modified_at": "modifiedAt",
      "organization_id": "organizationId",
      "customer_id": "customerId",
      "benefit_id": "benefitId",
      "display_key": "displayKey",
      "limit_activations": "limitActivations",
      "limit_usage": "limitUsage",
      "last_validated_at": "lastValidatedAt",
      "expires_at": "expiresAt",
    });
  }),
);

export function licenseKeyReadFromJSON(
  jsonString: string,
): SafeParseResult<LicenseKeyRead, SDKValidationError> {
  return safeParse(
    jsonString,
    (x) => LicenseKeyRead$inboundSchema.parse(JSON.parse(x)),
    `Failed to parse 'LicenseKeyRead' from JSON`,
  );
}
