import Joi from 'joi';
import { ApiCodeRegex, ApiCodeValidationMessage } from '@shipengine/connect-runtime';

export enum RequiredToShipEnum {
  Weight = 'Weight',
  Dimensions = 'Dimensions',
}

/** @description Package details */
export interface PackageType {
  /** @description The unique identifier for this package type. It should not change after being published */
  Id: string;
  /** @description The name of this package type ex: 'Eco friendly box' */
  Name: string;
  /** @description The identifier for this package type to be used in API calls. Must be snake cased and unique */
  ApiCode?: string;
  /** @description This is the code that will be sent to requests, this should be the same code the carrier's api would expect */
  CarrierPackageTypeCode: string;
  /** @description This is a human readable description about what the packaging is */
  Description?: string;
  /** @description This is an abbreviation of the name if necessary */
  Abbreviation?: string;
  /** @description Package attributes include International, Domestic, Consolidator */
  PackageAttributes: PackageAttribute[];
  /** @description What information is required to ship this package type Weight and or Dimensions */
  RequiredToShip?: RequiredToShipEnum[];
}

export enum PackageAttribute {
  International = 'International',
  Domestic = 'Domestic',
  Consolidator = 'Consolidator',
}

export const PackageTypeSchema = Joi.object({
  Id: Joi.string().uuid().required(),
  Name: Joi.string().required().max(50),
  ApiCode: Joi.string().optional().pattern(ApiCodeRegex, ApiCodeValidationMessage),
  CarrierPackageTypeCode: Joi.string().required().max(50),
  Description: Joi.string().optional().max(500),
  Abbreviation: Joi.string().optional().max(20),
  PackageAttributes: Joi.array()
    .required()
    .items(Joi.string().valid(...Object.values(PackageAttribute))),
  RequiredToShip: Joi.array()
    .optional()
    .items(Joi.string().valid(...Object.values(RequiredToShipEnum))),
});
