import { z } from 'zod';
import { NotificationChannelSchema } from '@isa/common/data-access';
import {
  CustomerSchema,
  PayerSchema as CrmPayerSchema,
  ShippingAddressSchema as CrmShippingAddressSchema,
} from '@isa/crm/data-access';
import { BuyerSchema } from './buyer.schema';
import { PayerSchema } from './payer.schema';
import { ShippingAddressSchema } from './shipping-address.schema';

/**
 * Schema for checkout completion parameters.
 *
 * @remarks
 * This schema validates all data required to complete a checkout without accessing state management.
 * All customer and order information must be provided as parameters.
 */
export const CompleteOrderParamsSchema = z.object({
  /**
   * ID of the shopping cart to process
   * The checkout will be created/refreshed from this shopping cart
   */
  shoppingCartId: z
    .number()
    .int()
    .positive()
    .describe('Shopping cart identifier'),

  /**
   * Buyer information (required)
   * Will be set on the checkout before order creation
   */
  buyer: BuyerSchema.describe('Buyer information'),

  /**
   * Notification channels for customer communications (required)
   */
  notificationChannels: NotificationChannelSchema.describe(
    'Notification channels',
  ).optional(),

  /**
   * Customer features for business logic determination
   * Used to analyze customer type and determine order handling
   */
  customerFeatures: z.record(
    z.string().describe('Customer features'),
    z.string(),
  ),

  /**
   * Payer information (optional)
   * Required only for B2B, delivery, or download orders
   */
  payer: PayerSchema.describe('Payer information').optional(),

  /**
   * Shipping address (optional)
   * Required only for delivery orders
   * Will be merged into destination objects
   */
  shippingAddress: ShippingAddressSchema.describe(
    'Shipping address information',
  ).optional(),

  /**
   * Special comment to apply to all shopping cart items (optional)
   */
  specialComment: z.string().describe('Special comment').optional(),
});

/**
 * TypeScript type inferred from the Zod schema
 */
export type CompleteOrderParams = z.infer<typeof CompleteOrderParamsSchema>;

export const CompleteCrmOrderParamsSchema = z.object({
  shoppingCartId: z
    .number()
    .int()
    .positive()
    .describe('Shopping cart identifier'),
  crmCustomer: CustomerSchema.describe('Crm customer'),
  crmPayer: CrmPayerSchema.describe('Crm payer').optional(),
  crmShippingAddress: CrmShippingAddressSchema.describe(
    'Crm shipping address',
  ).optional(),
  notificationChannels: NotificationChannelSchema.describe(
    'Notification channels',
  ).optional(),
  specialComment: z.string().describe('Special comment').optional(),
});

export type CompleteCrmOrderParams = z.infer<
  typeof CompleteCrmOrderParamsSchema
>;
