const fastify = require("fastify");
const createConnectionController = require("../controllers/createConnectionController");

module.exports = function (fastify, opts, next) {

  

    fastify.route({
      method: "POST",
      url: "/api/createConnections/:customerId",
      schema: {
          tags: ["Connections"],
          description: "This is for cretae New Connection",
          summary: "This is for cretae New Connection.",
          params: {
              required: ["customerId"],
              type: "object",
              properties: {
                  customerId: {
                      type: "string",
                      description: "customerId",
                  },
              },
          },
          body: {
              type: "object",
              properties: {
                  tankname: { type: "string" },
                  inputConnections: {
                      type: "array",
                      maxItems: 2500,
                      items: {
                          type: "object",
                          properties: {
                              inputConnections: { type: "string", default: null },
                              input_type: { type: "string", default: null },
                              inputismotor: { type: "boolean"},
                              motor_id:{ type: "string" ,default: null},
                              motor_status: { type: "string", default: "1" },
                              motor_stop_status: { type: "string", default: "1" },
                              capacity:{ type: "string" ,default: null},
                              water_level:{ type: "string" ,default: null},
                          },
                      },
                  },
                  outputConnections: {
                      type: "array",
                      maxItems: 2500,
                      items: {
                          type: "object",
                          properties: {
                              outputConnections: { type: "string", default: null },
                              output_type: { type: "string", default: null },
                              outputismotor: { type: "boolean" },
                              motor_id:{ type: "string" ,default: null},
                              motor_status: { type: "string", default: "1" },
                              motor_stop_status: { type: "string", default: "1" },
                              capacity:{ type: "string" ,default: null},
                              water_level:{ type: "string" ,default: null},

                          },
                      },
                  },
              },
          },
          security: [
              {
                  basicAuth: [],
              },
          ],
      },
      //preHandler: fastify.auth([fastify.authenticate]),
      handler: createConnectionController.createConnections,
  });
  

      fastify.route({
        method: "PUT",
        url: "/api/updateConnections/:customerId",
        schema: {
          tags: ["Connections"],
          summary: "This is for update connections",
          params: {
            required: ["customerId"],
            type: "object",
            properties: {
              customerId: {
                type: "string",
                description: "customerId",
              },
            },
          },
         
          body: {
            type: "object",
            // required: ['phone'],
            properties: {
                tankname: { type: "string" },
                inputConnections: {
                    type: "array",
                    maxItems: 2500,
                    items: {
                        type: "object",
                        properties: {
                          inputConnections: { type: "string", default: null },
                          input_type: { type: "string", default: null },
                          inputismotor: { type: "boolean"},
                          motor_id:{ type: "string" ,default: null},
                          motor_status: { type: "string", default: "1" },
                          motor_stop_status: { type: "string", default: "1" },
                          capacity: { type: "string", default: null },
                          water_level: { type: "string", default: null },
                        },
                    },
                },
                outputConnections: {
                    type: "array",
                    maxItems: 2500,
                    items: {
                        type: "object",
                        properties: {
                          outputConnections: { type: "string", default: null },
                          output_type: { type: "string", default: null },
                          outputismotor: { type: "boolean" },
                          motor_id:{ type: "string" ,default: null},
                          motor_status: { type: "string", default: "1" },
                          motor_stop_status: { type: "string", default: "1" },
                          capacity: { type: "string", default: null },
                          water_level: { type: "string", default: null },
                        },
                    },
                },
            },
          },
          security: [
            {
              basicAuth: [],
            },
          ],
        },
        // preHandler: [
        //   fastify.auth([fastify.operatorAuthenticate]),
        //   validationHandler.validatePhoneFormat,
        // ],
        preHandler: fastify.auth([fastify.authenticate]),
        handler: createConnectionController.updateconnectionInfo,
      });



      fastify.get("/api/getConnections", {
        schema: {
          tags: ["Connections"],
          description: "This is for Get Connections Data",
          summary: "This is for to Get Connections Data",
          querystring: {
            customerId: {type: 'string'}
          },
          security: [
            {
              basicAuth: [],
            },
          ],
        },
        preHandler: fastify.auth([fastify.authenticate]),
        handler: createConnectionController.getConnections,
      });
    
      fastify.route({
        method: "PUT",
        url: "/api/deleteTankConnections/:customerId",
        schema: {
          tags: ["Connections"],
          summary: "This is for delete tank connections",
          params: {
            required: ["customerId"],
            type: "object",
            properties: {
              customerId: {
                type: "string",
                description: "customerId",
              },
            },
          },
          querystring: {
            tankName: {type: 'string'}
          },
          security: [
            {
              basicAuth: [],
            },
          ],
        },
        preHandler: fastify.auth([fastify.authenticate]),
        handler: createConnectionController.deleteTankConnections,
      });
      





    next();
}
