{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "type": "object",
  "properties": {
    "method_id": {
      "type": "string",
      "minLength": 1,
      "description": "The shipping method ID",
      "errorMessage": {
        "type": "Shipping method ID must be a string",
        "minLength": "Shipping method ID cannot be empty"
      }
    },
    "cost": {
      "type": ["string", "number"],
      "pattern": "^\\d+(\\.\\d{1,2})?$",
      "description": "Flat rate shipping cost with up to 2 decimal places",
      "errorMessage": {
        "type": "Cost must be a number or numeric string",
        "pattern": "Cost must be a valid number with up to 2 decimal places (e.g., 10.00)"
      }
    },
    "is_enabled": {
      "type": ["integer", "string", "boolean"],
      "enum": [0, 1, "0", "1", true, false],
      "description": "Whether the shipping method is enabled",
      "errorMessage": {
        "type": "Status must be a boolean, 0, 1, '0', or '1'",
        "enum": "Status must be one of: 0, 1, '0', '1', true, or false"
      }
    },
    "calculation_type": {
      "type": "string",
      "enum": ["flat_rate", "price_based_rate", "weight_based_rate", "api"],
      "description": "The type of calculation to use for shipping cost",
      "errorMessage": {
        "type": "Calculation type must be a string",
        "enum": "Calculation type must be one of: flat_rate, price_based_rate, weight_based_rate, or api"
      }
    },
    "calculate_api": {
      "type": "string",
      "minLength": 1,
      "description": "API endpoint URL for calculating shipping cost",
      "errorMessage": {
        "type": "Calculate API must be a string",
        "minLength": "Calculate API cannot be empty"
      }
    },
    "condition_type": {
      "type": "string",
      "enum": ["weight", "price", "none"],
      "description": "The type of condition to apply for this shipping method",
      "errorMessage": {
        "type": "Condition type must be a string",
        "enum": "Condition type must be one of: weight, price, or none"
      }
    },
    "min": {
      "type": ["string", "number"],
      "pattern": "^\\d+(\\.\\d{1,2})?$",
      "description": "Minimum order price or weight for condition",
      "errorMessage": {
        "type": "Minimum value must be a number or numeric string",
        "pattern": "Minimum value must be a valid number with up to 2 decimal places"
      }
    },
    "max": {
      "type": ["string", "number"],
      "pattern": "^\\d+(\\.\\d{1,2})?$",
      "description": "Maximum order price or weight for condition",
      "errorMessage": {
        "type": "Maximum value must be a number or numeric string",
        "pattern": "Maximum value must be a valid number with up to 2 decimal places"
      }
    },
    "weight_based_cost": {
      "type": "array",
      "minItems": 1,
      "description": "Array of weight tiers for weight-based shipping calculation",
      "items": {
        "type": "object",
        "properties": {
          "min_weight": {
            "type": ["string", "number"],
            "pattern": "^\\d+(\\.\\d{1,2})?$",
            "description": "Minimum order weight for this tier",
            "errorMessage": {
              "type": "Minimum weight must be a number or numeric string",
              "pattern": "Minimum weight must be a valid number with up to 2 decimal places"
            }
          },
          "cost": {
            "type": ["string", "number"],
            "pattern": "^\\d+(\\.\\d{1,2})?$",
            "description": "Shipping cost for this weight tier",
            "errorMessage": {
              "type": "Cost must be a number or numeric string",
              "pattern": "Cost must be a valid number with up to 2 decimal places"
            }
          }
        },
        "additionalProperties": false,
        "required": ["min_weight", "cost"],
        "errorMessage": {
          "required": {
            "min_weight": "Minimum weight is required for each weight tier",
            "cost": "Cost is required for each weight tier"
          }
        }
      },
      "errorMessage": {
        "type": "Weight based cost must be an array",
        "minItems": "At least one weight tier is required for weight-based calculation"
      }
    },
    "price_based_cost": {
      "type": "array",
      "minItems": 1,
      "description": "Array of price tiers for price-based shipping calculation",
      "items": {
        "type": "object",
        "properties": {
          "min_price": {
            "type": ["string", "number"],
            "pattern": "^\\d+(\\.\\d{1,2})?$",
            "description": "Minimum order price for this tier",
            "errorMessage": {
              "type": "Minimum price must be a number or numeric string",
              "pattern": "Minimum price must be a valid number with up to 2 decimal places"
            }
          },
          "cost": {
            "type": ["string", "number"],
            "pattern": "^\\d+(\\.\\d{1,2})?$",
            "description": "Shipping cost for this price tier",
            "errorMessage": {
              "type": "Cost must be a number or numeric string",
              "pattern": "Cost must be a valid number with up to 2 decimal places"
            }
          }
        },
        "additionalProperties": false,
        "required": ["min_price", "cost"],
        "errorMessage": {
          "required": {
            "min_price": "Minimum price is required for each price tier",
            "cost": "Cost is required for each price tier"
          }
        }
      },
      "errorMessage": {
        "type": "Price based cost must be an array",
        "minItems": "At least one price tier is required for price-based calculation"
      }
    }
  },
  "additionalProperties": true,
  "required": ["method_id", "condition_type"],
  "errorMessage": {
    "required": {
      "method_id": "Shipping method ID is required",
      "condition_type": "Condition type is required"
    }
  },
  "allOf": [
    {
      "if": {
        "properties": {
          "calculation_type": { "const": "flat_rate" }
        }
      },
      "then": {
        "required": ["calculation_type", "cost"],
        "errorMessage": {
          "required": {
            "calculation_type": "Calculation type is required",
            "cost": "Cost is required when calculation type is flat_rate"
          }
        }
      }
    },
    {
      "if": {
        "properties": {
          "calculation_type": { "const": "price_based_rate" }
        }
      },
      "then": {
        "required": ["calculation_type", "price_based_cost"],
        "errorMessage": {
          "required": {
            "calculation_type": "Calculation type is required",
            "price_based_cost": "Price based cost tiers are required when calculation type is price_based_rate"
          }
        }
      }
    },
    {
      "if": {
        "properties": {
          "calculation_type": { "const": "weight_based_rate" }
        }
      },
      "then": {
        "required": ["calculation_type", "weight_based_cost"],
        "errorMessage": {
          "required": {
            "calculation_type": "Calculation type is required",
            "weight_based_cost": "Weight based cost tiers are required when calculation type is weight_based_rate"
          }
        }
      }
    },
    {
      "if": {
        "properties": {
          "calculation_type": { "const": "api" }
        }
      },
      "then": {
        "required": ["calculation_type", "calculate_api"],
        "errorMessage": {
          "required": {
            "calculation_type": "Calculation type is required",
            "calculate_api": "Calculate API URL is required when calculation type is api"
          }
        }
      }
    },
    {
      "if": {
        "properties": {
          "condition_type": { "enum": ["weight", "price"] }
        }
      },
      "then": {
        "required": ["min", "max"],
        "errorMessage": {
          "required": {
            "min": "Minimum value is required when using weight or price conditions",
            "max": "Maximum value is required when using weight or price conditions"
          }
        }
      }
    }
  ]
}
