const Joi = require("joi");

// ========================
// Brand Validation
// ========================
const insertBrandSchema = Joi.object({
  brand_name: Joi.string().max(100).required(),
  brand_type: Joi.string().max(50).optional().allow(""),
  brand_manufacture: Joi.string().max(100).required(),
  brand_model: Joi.string().max(100).optional().allow(""),
  is_active: Joi.boolean().required(),
  description: Joi.string().max(255).optional().allow(""),
  error_code: Joi.array()
    .items(
      Joi.object({
        error_code: Joi.string().max(100).required(),
        error_code_name: Joi.string().max(100).required(),
        error_code_description: Joi.string().optional().allow(""),
        error_code_color: Joi.string().optional().allow(""),
        path_icon: Joi.string().optional().allow(""),
        is_active: Joi.boolean().required(),
        what_action_to_take: Joi.string().optional().allow(""),
        solution: Joi.array()
          .items(
            Joi.object({
              solution_name: Joi.string().max(100).required(),
              type_solution: Joi.string()
                .valid("text", "pdf", "image", "video", "link")
                .required(),
              text_solution: Joi.when("type_solution", {
                is: "text",
                then: Joi.string().required(),
                otherwise: Joi.string().optional().allow(""),
              }),
              path_solution: Joi.when("type_solution", {
                is: "text",
                then: Joi.string().optional().allow(""),
                otherwise: Joi.string().required(),
              }),
              is_active: Joi.boolean().required(),
            })
          )
          .min(1)
          .required(),
      })
    )
    .min(1)
    .required(),
});

// Update Brand Validation
const updateBrandSchema = Joi.object({
  brand_name: Joi.string().max(100).required(),
  brand_type: Joi.string().max(50).optional().allow(""),
  brand_manufacture: Joi.string().max(100).required(),
  brand_model: Joi.string().max(100).optional().allow(""),
  is_active: Joi.boolean().required(),
  description: Joi.string().max(255).optional().allow(""),
  error_code: Joi.array()
    .items(
      Joi.object({
        error_code: Joi.string().max(100).required(),
        error_code_name: Joi.string().max(100).required(),
        error_code_description: Joi.string().optional().allow(""),
        error_code_color: Joi.string().optional().allow(""),
        path_icon: Joi.string().optional().allow(""),
        is_active: Joi.boolean().required(),
        what_action_to_take: Joi.string().optional().allow(""),
        solution: Joi.array()
          .items(
            Joi.object({
              solution_name: Joi.string().max(100).required(),
              type_solution: Joi.string()
                .valid("text", "pdf", "image", "video", "link")
                .required(),
              text_solution: Joi.when("type_solution", {
                is: "text",
                then: Joi.string().required(),
                otherwise: Joi.string().optional().allow(""),
              }),
              path_solution: Joi.when("type_solution", {
                is: "text",
                then: Joi.string().optional().allow(""),
                otherwise: Joi.string().required(),
              }),
              is_active: Joi.boolean().optional(),
            })
          )
          .min(1)
          .required(),
      })
    )
    .optional(),
}).min(1);

module.exports = {
  insertBrandSchema,
  updateBrandSchema,
};
