const fastify = require("fastify");
const storeController = require('../controllers/storeController')
const customJwtAuth = require('../customAuthJwt');
const fastifyJwt = require('fastify-jwt');
const userController = require('../controllers/userController');


module.exports = function (fastify, opts, next) {
    fastify.route({
        method: 'POST',
        url: '/api/installSignup',
        schema: {
          tags: ['Install'],
          description: 'This is for creating a New Install Account',
          summary: 'This is for creating a New Install Account',
          body: {
            type: 'object',
            properties: {
              phone: { type: 'string' },
              password: { type: 'string' },
              emails: {
                type: 'array',
                maxItems: 2,
                items: {
                  type: 'object',
                  properties: {
                    email: { type: 'string', default: null },
                  },
                },
              },
              //name: { type: 'string' },
              
              team: { type: 'string', default: null },
              zone: { type: 'string', default: null },
              designation: { type: 'string', default: null },
              reportingManager: { type: 'string', default: null },
              departmentName: { type: 'string', default: null },
              manager: { type: 'string', default: null },
              address: { type: 'string', default: null },
              address1: { type: 'string', default: null },
              address2: { type: 'string', default: null },
              city: { type: 'string', default: null },
              state: { type: 'string', default: null },
              zip: { type: 'string', default: null },
              country: { type: 'string', default: null },
              notes: { type: 'string', default: null },
              latitude: { type: 'number', default: 0.0 },
              longitude: { type: 'number', default: 0.0 },
              fcmId: { type: 'string', default: null },
              alternativeNumber : { type: 'string', default: null },
              firstName :{ type: 'string', default: null },
              lastName : { type: 'string', default: null },
            },
          },
          security: [
            {
              basicAuth: [],
            },
          ],
        },
        handler: storeController.installSignUp,
      });
//   fastify.post("/api/insatllLogin", {
//     schema: {
//       description: "This is for Login Install",
//       tags: ["Install"],
//       summary: "This is for Login Install",
//       body: {
//         type: "object",
//         required: ["phone", "password"],
//         properties: {
//           phone: { type: "string" },
//           password: { type: "string" },
//         },
//       },
//     },
//     handler: storeController.installLogin,
// });

// fastify.post("/api/installotplogin", {
//     schema: {
//       description: "This is for Login Otp Boy",
//       tags: ["Install"],
//       summary: "This is for Login Otp Boy",
//       body: {
//         type: "object",
//         required: ["phone"],
//         properties: {
//           phoneVerificationCode: { type: "string" },
//           phone: { type: "string" },
//         },
//       },
//     },
//     handler: storeController.installationVerifyPhone,
// });


fastify.post('/api/stores', {
  schema: {
    description: "Create a new store account",
    tags: ["Store-Data"],
    summary: "create a new store account.",
    body: {
      type: "object",
     // required: ["storename", "phone", "password"],
      properties: {
        storename: { type: "string" },
        password: {type: "string"},
        zone: { type: 'string', default: null },
        designation: { type: 'string', default: null },
        reportingManager: { type: 'string', default: null },
        departmentName: { type: 'string', default: null },
        phone: { type: "string", unique: true, trim: true },
        contactPersonName: { type: "string" },
        contactPersonPhone: { type: "string", unique: true, trim: true },
        emails: { type: "string" },
        description: { type: "string", default: null },
        startingPrice: { type: "string", default: 0.0 },
        profile: {
          type: "object",
          properties: {
            role: { type: "array", items: { type: "string" }, default: ["store"] },
            firstName: { type: "string", default: null },
            lastName: { type: "string", default: null },
            contactNumber: { type: "string", default: null },
            alternativeContactNumber: { type: "string", default: null },
            store_address: { type: "string", default: null },
            address1: { type: 'string', default: null },
            address2: { type: 'string', default: null },
            city: { type: "string", default: null },
            state: { type: "string", default: null },
            country: { type: "string", default: null },
            zip: { type: "string", default: null },
          },
        },
        longitude: { type: "number", default: 0.0 },
        latitude: { type: "number", default: 0.0 },
        fcmId: { type: "string", default: null },
      },
    },
    security: [{ basicAuth: [] }],
  },
  handler: storeController.addStore,
});


fastify.post('/api/salesSignUp', {
  schema: {
    description: "This is for Create New Sales",
    tags: ["Sales-Data"],
    summary: "This is for Create New Sales.",
    body: {
      type: "object",
      properties: {
        username: { type: "string" },
        designation: { type: 'string', default: null },
        reportingManager: { type: 'string', default: null },
        departmentName: { type: 'string', default: null },
        zone: { type: 'string', default: null },

        phone: { type: "string" },
        password: { type: "string" },
        emails: { type: "string" },
        address: { type: "string", default: null },
        profile: {  
          type: "object",
          properties: {
            firstName: { type: "string" },
            lastName: { type: "string" },
            address: { type: "string" },
            city: { type: 'string', default: null },
            state: { type: 'string', default: null },
            zip: { type: 'string', default: null },
            country: { type: 'string', default: null },
            address1: { type: 'string', default: null },
            address2: { type: 'string', default: null },
          },
          required: ["firstName", "lastName"]
        }
      }
    },
    security: [{ basicAuth: [] }],
  },
  handler: storeController.addSales,
});

fastify.get("/api/getallsales", {
  schema: {
    tags: ["Sales-Data"],
    description: "This is for Get all Sales Person Data",
    summary: "This is for to Get all Sales Person Data",
   
    security: [
      {
        basicAuth: [],
      },
    ],
  },
  //preHandler: fastify.auth([fastify.authenticate]),
  handler: storeController.getallsales,
});

fastify.get("/api/getallstore", {
  schema: {
    tags: ["Sales-Data"],
    description: "This is for Get all Store Person Data",
    summary: "This is for to Get all Store Person Data",
   
    security: [
      {
        basicAuth: [],
      },
    ],
  },
  //preHandler: fastify.auth([fastify.authenticate]),
  handler: storeController.getallstore,
});

fastify.delete("/api/deletesale/:salesId", {
  schema: {
    description: "Delete a Sales by salesId",
    tags: ["Sales-Data"],
    summary: "Delete a user by salesId",
    params: {
      type: "object",
      properties: {
        salesId: { type: "string" },  // Customer ID
      },
      required: ["salesId"],
    },
    response: {
      200: {
        type: "object",
        properties: {
          success: { type: "boolean" },
          message: { type: "string" },
        }
      }
    }
  },
  handler: storeController.deleteUserInfo,
});

fastify.delete("/api/deleteStore/:storeId", {
  schema: {
    description: "Delete a Store by storeId",
    tags: ["Sales-Data"],
    summary: "Delete a Store by storeId",
    params: {
      type: "object",
      properties: {
        storeId: { type: "string" },  // Customer ID
      },
      required: ["storeId"],
    },
    response: {
      200: {
        type: "object",
        properties: {
          success: { type: "boolean" },
          message: { type: "string" },
        }
      }
    }
  },
  handler: storeController.deleteStoreInfo,
});

fastify.put('/api/editSalesUser/:salesId', {
  schema: {
    description: "Edit Sales user details by Sales ID",
    tags: ["Sales-Data"],
    summary: "Edit sales user details.",
    params: {
      type: "object",
      properties: {
        salesId: { type: "string" },  
      },
      required: ["salesId"],
    },
    body: {
      type: "object",
      properties: {
        username: { type: "string" },
        phone: { type: "string" },
        zone: { type: 'string', default: null },
        designation: { type: 'string', default: null },
        reportingManager: { type: 'string', default: null },
        departmentName: { type: 'string', default: null },
        password: { type: "string" },
        emails: { type: "string" },
        address: { type: "string" },
        profile: { 
          type: "object",
          properties: {
            firstName: { type: "string" },
            lastName: { type: "string" },
            address: { type: "string" },
            city: { type: 'string', default: null },
            state: { type: 'string', default: null },
            zip: { type: 'string', default: null },
            country: { type: 'string', default: null },
            address1: { type: 'string', default: null },
            address2: { type: 'string', default: null },

          },
        }
      },
      
    }
  },
  handler: storeController.editSalesUser,
});

fastify.put('/api/editStore/:storeId', {
  schema: {
    description: "Edit store details by Store ID",
    tags: ["Store-Data"],
    summary: "Edit store  details.",
    params: {
      type: "object",
      properties: {
        storeId: { type: "string" }, 
      },
      required: ["storeId"],
    },
    body: {
      type: "object",
      properties: {
        storename: { type: "string" },
        phone: { type: "string" },
        address: { type: "string" },
        address1: { type: "string" },
        address2: { type: "string" },
        emails: { type: "string" },
        zone: { type: 'string', default: null },
        designation: { type: 'string', default: null },
        reportingManager: { type: 'string', default: null },
        departmentName: { type: 'string', default: null },
        profile: {
          type: "object",
          properties: {
            firstName: { type: "string" },
            lastName: { type: "string" },
            contactNumber: { type: "string" },
            alternativeContactNumber: { type: "string" },
            store_address: { type: "string" },
            city: { type: "string" },
            state: { type: "string" },
            country: { type: "string" },
            zip: { type: "string" },
            address1: { type: 'string', default: null },
            address2: { type: 'string', default: null },
          },
        },
        alternativeNumber: { type: "string" },
        longitude: { type: "number" },
        latitude: { type: "number" },
        fcmId: { type: "string" },
        description: { type: "string" },
        startingPrice: { type: "string" },
        contactPersonPhone: { type: "string" }, 
      },
      required: ["phone"], 
    }
  },
  handler: storeController.editStore,
});




fastify.get("/api/getAllInstallers", {
  schema: {
    description: "Retrieve all users",
    tags: ["Install Management"],
    summary: "Get all users",
    // response: {
    //   200: {
    //     type: "array",
    //     items: {
    //       type: "object",
    //       properties: {
    //         installationId: { type: "string" },
    //         phone: { type: "string" },
    //         password: { type: "string" },
    //         emails: { type: "array", items: { type: "object", properties: { email: { type: "string" } } } },
    //         team: { type: "string" },
    //         manager: { type: "string" },
    //         address: { type: "string" },
    //         address1: { type: "string" },
    //         address2: { type: "string" },
    //         city: { type: "string" },
    //         state: { type: "string" },
    //         zip: { type: "string" },
    //         country: { type: "string" },
    //         notes: { type: "string" },
    //         latitude: { type: "number" },
    //         longitude: { type: "number" },
    //         fcmId: { type: "string" },
    //         alternativeNumber: { type: "string" },
    //         firstName: { type: "string" },
    //         lastName: { type: "string" },
    //       },
    //     },
    //   },
    // },
  },
  handler: storeController.getAllUsers,
});


fastify.delete("/api/deleteInstaller/:installationId", {
  schema: {
    description: "Delete a user by installationId",
    tags: ["Install Management"],
    summary: "Delete user",
    params: {
      type: "object",
      properties: {
        installationId: { type: "string" }, 
      },
      required: ["installationId"],
    },
    response: {
      200: {
        type: "object",
        properties: {
          message: { type: "string" },
        },
      },
    },
  },
  handler: storeController.deleteUserByInstallationId,
});


fastify.put("/api/updaeInstaller/:installationId", {
  schema: {
    description: "Update installer details by installationId",
    tags: ["Install Management"],
    summary: "Update installer details by installationId",
    params: {
      type: "object",
      properties: {
        installationId: { type: "string" },
      },
      required: ["installationId"],
    },
    body: {
      type: "object",
      properties: {
        phone: { type: "string" },
        password: { type: "string" },
        emails: { type: "array", items: { type: "object", properties: { email: { type: "string" } } } },
        team: { type: "string" },
        zone: { type: 'string', default: null },
        designation: { type: 'string', default: null },
        reportingManager: { type: 'string', default: null },
        departmentName: { type: 'string', default: null },
        manager: { type: "string" },
        address: { type: "string" },
        address1: { type: "string" },
        address2: { type: "string" },
        city: { type: "string" },
        state: { type: "string" },
        zip: { type: "string" },
        country: { type: "string" },
        notes: { type: "string" },
        latitude: { type: "number" },
        longitude: { type: "number" },
        fcmId: { type: "string" },
        alternativeNumber: { type: "string" },
        firstName: { type: "string" },
        lastName: { type: "string" },
      },
    },
    response: {
      200: {
        type: "object",
        properties: {
          message: { type: "string" },
        },
      },
    },
  },
  handler: storeController.updateUserByInstallationId,
});





fastify.get("/api/getusersofParticularInstaller", {
  schema: {
    tags: ["Install"],
    description: "This is to Get users of Installer",
    summary: "This is  to Get users of Installer",
    
    querystring: {
      InstallerId: {type: 'string'}
    },
    security: [
      {
        basicAuth: [],
      },
    ],
  },
 // preHandler: fastify.auth([fastify.authenticate]),
  handler: storeController.getusersofParticularInstaller,
});

// fastify.post("/api/createwaterlevelSensor/:storeId", {
//     schema: {
//       description: "This is for creating waterlevel Sensor",
//       tags: ["Store-Data"],
//       summary: "This is for creating waterlevel Sensor",
//       params: {
//         required: ["storeId"],
//         type: "object",
//         properties: {
//           storeId: {
//             type: "string",
//             description: "storeId",
//           },
//         },
//       },
      
//       body: {
//         type: "object",
        
//         properties: {
          
//           hardwareId: { type: "string" },
//           type: { type: "string" },
//           indate: { type: "string" },
//           hardwareId_company: { type: "string" }
//         },
//       },
//     },
//     handler: storeController.createwaterlevelSensor,
// })



// fastify.put("/api/editwaterlevelSensor/:storeId", {
//   schema: {
//     description: "This is for editing a water level sensor",
//     tags: ["Store-Data"],
//     summary: "This is for editing a water level sensor",
//     params: {
//       required: ["storeId"],
//       type: "object",
//       properties: {
//         storeId: {
//           type: "string",
//           description: "storeId",
//         },

//       },
//     },
//     body: {
//       type: "object",
//       properties: {
//         hardwareId: { type: "string" },
//         type: { type: "string" },
//         indate: { type: "string" },
//         hardwareId_company: { type: "string" }
//       },
//     },
//   },
//   handler: storeController.editWaterLevelSensor,
// });


fastify.delete("/api/deletewaterlevelSensor/:storeId", {
  schema: {
    description: "This is for deleting a water level sensor",
    tags: ["Store-Data"],
    summary: "This is for deleting a water level sensor",
    params: {
      required: ["storeId"],
      type: "object",
      properties: {
        storeId: {
          type: "string",
          description: "storeId",
        },

      },
    },
    body: {
      type: "object",
      properties: {
        hardwareId: { type: "string" },
        
      },
    },
  },
  handler: storeController.deleteWaterLevelSensor,
});

fastify.get("/api/getHardware/:storeId", {
  schema: {
    tags: ["Store-Data"],
    description: "This is to Get Waterlevel sensor Data",
    summary: "This is to Get Waterlevel sensor Data",
    params: {
      required: ["storeId"],
      type: "object",
      properties: {
        storeId: {
          type: "string",
          description: "storeId",
        },
      },
    },
    security: [
      {
        basicAuth: [],
      },
    ],
  },
 // preHandler: fastify.auth([fastify.authenticate]),
  handler: storeController.getHardware,
});

// fastify.post("/api/qccheckwaterlevelSensor/:hardwareId", {
//   schema: {
//     description: "This is for checking waterlevel Sensor",
//     tags: ["Store-Data"],
//     summary: "This is for checking waterlevel Sensor",
//     params: {
//       required: ["hardwareId"],
//       type: "object",
//       properties: {
//         hardwareId: {
//           type: "string",
//           description: "hardwareId",
//         },
//       },
//     },
//     body: {
//       type: "object",
      
//       properties: {
//         qccheck: { type: "string" },
//         qccheckdate: { type: "string" },
//         qcby: { type: "string" },
//         comment: { type: "string" },
//         outforrepairdate: { type: "string" },
//         sendto: { type: "string" },
//         repairfeedback: { type: "string" },
//       },
//     },
//   },
//   handler: storeController.qccheckwaterlevelSensor,
// })


fastify.put("/api/getHardwareqc/:storeId", {
  schema: {
    tags: ["Store-Data"],
    description: "This is to Get Quality Check Details Of Waterlevel Sensor",
    summary: "This is to Get Quality Details Of Waterlevel Sensor",
    params: {
      required: ["storeId"],
      type: "object",
      properties: {
        storeId: {
          type: "string",
          description: "storeId",
        },
      },
    },
    body: {
      type: "object",
      properties: {
        hardwareId: { type: "string" },
       
      },
    },
    security: [
      {
        basicAuth: [],
      },
    ],
  },
 // preHandler: fastify.auth([fastify.authenticate]),
  handler: storeController.getHardwareqc,
});


fastify.post("/api/installwaterlevelSensor/:storeId", {
  schema: {
    description: "This is for installing waterlevel Sensor",
    tags: ["Store-Data"],
    summary: "This is for installing waterlevel Sensor",
    params: {
      required: ["storeId"],
      type: "object",
      properties: {
        storeId: {
          type: "string",
          description: "storeId",
        },
      },
    },
    
    body: {
      type: "object",
      
      properties: {
        
        hardwareId: { type: "string" },
        dateofinstallation: { type: "string" },
        customerId: { type: "string" },
        installedby: { type: "string" }
      },
    },
  },
  handler: storeController.installwaterlevelSensor,
})


fastify.post("/api/addSlave/:hardwareId", {
  schema: {
    description: "This is for adding slaves to waterlevel Sensor(Slave)",
    tags: ["Store-Data"],
    summary: "This is for adding slaves to waterlevel Sensor(Slave)",
    params: {
      required: ["hardwareId"],
      type: "object",
      properties: {
        hardwareId: {
          type: "string",
          description: "Main hardware ID",
        },
      },
    },
    body: {
      type: "object",
      properties: {
        tankhardwareId: { type: "string" },
        type: { type: "string" },
        indate: { type: "string" },
        hardwareId_company: { type: "string" }
      },
    },
  },
  handler: storeController.addSlave,
});


// fastify.put("/api/editSlave/:hardwareId", {
//   schema: {
//     description: "This is for editing a slave of a water level sensor",
//     tags: ["Store-Data"],
//     summary: "This is for editing a slave of a water level sensor",
//     params: {
//       required: ["hardwareId"],
//       type: "object",
//       properties: {
//         hardwareId: {
//           type: "string",
//           description: "Main hardware ID",
//         },

//       },
//     },
//     body: {
//       type: "object",
//       properties: {
//         tankhardwareId: { type: "string" },
//         type: { type: "string" },
//         indate: { type: "string" },
//         hardwareId_company: { type: "string" },
//         qccheck: { type: "string", default: null },
//         qccheckdate: { type: "string", default: null },
//         qcby: { type: "string", default: null },
//         comment: { type: "string", default: "0" },
//         outforrepairdate: { type: "string", default: "0" },
//         sendto: { type: "string", default: null },
//         repairfeedback: { type: "string", default: "0" },
//         dateofinstallation: { type: "string", default: null },
//         installedby: { type: "string", default: "0" },
//         customerId: { type: "string", default: "0" },
//         comments: { type: "string", default: "0" },
//       },
//     },
//   },
//   handler: storeController.editSlave,
// });


fastify.delete("/api/deleteSlave/:hardwareId", {
  schema: {
    description: "This is for deleting a slave of a water level sensor",
    tags: ["Store-Data"],
    summary: "This is for deleting a slave of a water level sensor",
    params: {
      required: ["hardwareId"],
      type: "object",
      properties: {
        hardwareId: {
          type: "string",
          description: "Main hardware ID",
        },

      },
    },
    body: {
      type: "object",
      properties: {
        tankhardwareId: { type: "string" },
        
      },
    },
  },
  handler: storeController.deleteSlave,
});

fastify.post("/api/qccheckwaterlevelslaveSensor/:storeId", {
  schema: {
    description: "This is for checking slaves (Slave)",
    tags: ["Store-Data"],
    summary: "This is for checking slaves (Slave)",
    params: {
      required: ["storeId"],
      type: "object",
      properties: {
        storeId: {
          type: "string",
          description: "Store ID",
        },
       
      },
    },
    body: {
      type: "object",
      properties: {
        hardwareId: { type: "string" },
        tankhardwareId: { type: "string" },
        qccheck: { type: "string" },
        qccheckdate: { type: "string" },
        qcby: { type: "string" },
        comment: { type: "string" },
        outforrepairdate: { type: "string" },
        sendto: { type: "string" },
        repairfeedback: { type: "string" },
      },
    },
  },
  handler: storeController.qccheckwaterlevelSensorSlave,
});



fastify.put("/api/getHardwareqcslave/:storeId", {
  schema: {
    tags: ["Store-Data"],
    description: "This is to Get Quality Check Data Of Slave (Slave)",
    summary: "This is to Get Quality Check Data Of Slave (Slave)",
    params: {
      required: ["storeId"],
      type: "object",
      properties: {
        storeId: {
          type: "string",
          description: "Store ID",
        },
      },
    },
    body: {
      type: "object",
      properties: {
        hardwareId: { type: "string" },
        tankhardwareId: { type: "string" }, // Optional field for tank hardware
      },
    },
    security: [
      {
        basicAuth: [],
      },
    ],
  },
  handler: storeController.getHardwareqcslave,
});



fastify.post("/api/installwaterlevelSensorSlave/:storeId", {
  schema: {
    description: "This is for installing Slaves (Slave)",
    tags: ["Store-Data"],
    summary: "This is for installing  Slaves (Slave)",
    params: {
      required: ["storeId"],
      type: "object",
      properties: {
        storeId: {
          type: "string",
          description: "Store ID",
        },
      },
    },
    body: {
      type: "object",
      properties: {
        hardwareId: { type: "string" },
        tankhardwareId: { type: "string" }, // Add tankhardwareId in body
        dateofinstallation: { type: "string" },
        customerId: { type: "string" },
        installedby: { type: "string" }
      },
    },
  },
  handler: storeController.installwaterlevelSensorSlave,
});




fastify.post("/api/createmotorswitchSensor/:storeId", {
  schema: {
    description: "This is for creating motor switch",
    tags: ["Store-Data"],
    summary: "This is for creating motor switch",
    params: {
      required: ["storeId"],
      type: "object",
      properties: {
        storeId: {
          type: "string",
          description: "storeId",
        },
      },
    },
    
    body: {
      type: "object",
      
      properties: {
        
        motorId: { type: "string" },
        type: { type: "string" },
        indate: { type: "string" },
      
      },
    },
  },
  handler: storeController.createmotorswitchSensor,
})


fastify.get("/api/getHardwaremotorswitch/:storeId", {
  schema: {
    tags: ["Store-Data"],
    description: "This is to Get Motor Switches",
    summary: "This is to Get Motor Switches",
    params: {
      required: ["storeId"],
      type: "object",
      properties: {
        storeId: {
          type: "string",
          description: "storeId",
        },
      },
    },
    security: [
      {
        basicAuth: [],
      },
    ],
  },
 // preHandler: fastify.auth([fastify.authenticate]),
  handler: storeController.getHardwaremotorswitch,
});



fastify.post("/api/qccheckpumpswitch/:motorId", {
  schema: {
    description: "This is for checking Motor Switch",
    tags: ["Store-Data"],
    summary: "This is for checking Motor Switch",
    params: {
      required: ["motorId"],
      type: "object",
      properties: {
        motorId: {
          type: "string",
          description: "motorId",
        },
      },
    },
    body: {
      type: "object",
      
      properties: {
        qccheck: { type: "string" },
        qccheckdate: { type: "string" },
        qcby: { type: "string" },
        comment: { type: "string" },
        outforrepairdate: { type: "string" },
        sendto: { type: "string" },
        repairfeedback: { type: "string" },
      },
    },
  },
  handler: storeController.qccheckmotorswitch,
})


fastify.put("/api/getpumpswitchqc/:storeId", {
  schema: {
    tags: ["Store-Data"],
    description: "This is to Get Quality Check Details Of Motor Switch",
    summary: "This is to Get Quality Details Of Motor Switch",
    params: {
      required: ["storeId"],
      type: "object",
      properties: {
        storeId: {
          type: "string",
          description: "storeId",
        },
      },
    },
    body: {
      type: "object",
      properties: {
        motorId: { type: "string" },
       
      },
    },
    security: [
      {
        basicAuth: [],
      },
    ],
  },
 // preHandler: fastify.auth([fastify.authenticate]),
  handler: storeController.getpumpswitchqc,
});


fastify.post("/api/installmotorswitch/:storeId", {
  schema: {
    description: "This is for installing Motor Switch",
    tags: ["Store-Data"],
    summary: "This is for installing Motor Switch",
    params: {
      required: ["storeId"],
      type: "object",
      properties: {
        storeId: {
          type: "string",
          description: "storeId",
        },
      },
    },
    
    body: {
      type: "object",
      
      properties: {
        
        motorId: { type: "string" },
        dateofinstallation: { type: "string" },
        customerId: { type: "string" },
        installedby: { type: "string" }
      },
    },
  },
  handler: storeController.installmotorswitch,
})

fastify.post("/api/createwaterlevelSensorintime/:storeId", {
  schema: {
    description: "This is for creating Sensors at in time",
    tags: ["Store-Data"],
    summary: "This is for creating Sensors at in time",
    params: {
      required: ["storeId"],
      type: "object",
      properties: {
        storeId: {
          type: "string",
          description: "storeId",
        },
      },
    },
    
    body: {
      type: "object",
      
      properties: {
        batchno: { type: "string" },
        model:{type:"string"},
        type:{type:"string"},
        quantity: { type: "string" },
        indate: { type: "string" },
        hardwareId_company: { type: "string" }
      },
    },
  },
  handler: storeController.createSensor,
})

fastify.delete("/api/deleteSensorById/:_id", {
  schema: {
    description: "Delete a sensor by ID",
    tags: ["Store-Data"],
    summary: "Delete a sensor based on its _id",
    params: {
      required: ["_id"],
      type: "object",
      properties: {
        _id: {
          type: "string",
          description: "Sensor ID",
        },
      },
    },
  },
  handler: storeController.deleteSensorById,
});

fastify.post('/api/generateHardwareMasterId/:storeId', {
  schema: {
    description: 'Generate hardwareId and masterId dynamically for pending sensors',
    tags: ['Store-Data'],
    summary: 'Assign hardwareId and masterId to pending sensors dynamically',
    params: {
      required: ["storeId"],
      type: "object",
      properties: {
        storeId: {
          type: "string",
          description: "storeId",
        },
      },
    },
    body: {
      type: 'object',
      required: ['from', 'to', 'type'],
      properties: {
        from: { type: 'string', description: 'Starting hardwareId (e.g., 00000020)' },
        to: { type: 'string', description: 'Ending hardwareId (e.g., 00000030)' },
        type: { type: 'string', description: 'Type of sensor' },
        quantity: { type: 'string' },
      },
    },
  },

  handler: storeController.generateHardwareMasterId,
});


fastify.post('/api/getSensorByHardwareId/:storeId', {
  schema: {
    description: 'Fetch details of a specific sensor using hardwareId',
    tags: ['Store-Data'],
    summary: 'Retrieve sensor details by hardwareId',
    params: {
      required: ['storeId'],
      type: 'object',
      properties: {
        storeId: { type: 'string', description: 'Store ID' },
      },
    },
    body: {
      type: 'object',
      required: ['hardwareId'],
      properties: {
        hardwareId: { type: 'string', description: 'Hardware ID of the sensor' },
      },
    },
  },
  handler: storeController.getSensorByHardwareId,
});


fastify.post("/api/updateSensorById/:_id", {
  schema: {
    description: "Edit specific sensor fields",
    tags: ["Store-Data"],
    summary: "Update specific fields of a sensor before macId",
    params: {
      required: ["_id"],
      type: "object",
      properties: {
        _id: {
          type: "string",
          description: "Sensor ID",
        },
      },
    },
    body: {
      type: "object",
      properties: {
        model: { type: "string", description: "Model of the sensor" },
        type: { type: "string", description: "Type of sensor" },
        masterId: { type: "string"},
        hardwareId_company: { type: "string", description: "Company name of hardware ID" },
        hardwareId: { type: "string", nullable: true, description: "Hardware ID (if applicable)" },
       
      },
    },
  },
  handler: storeController.updateSensorById,
});


fastify.post('/api/updateSensorQC/:hardwareId', {
  schema: {
    description: 'Edit specific sensor QC fields',
    tags: ['Store-Data'],
    summary: 'Update QC fields of a sensor',
    params: {
      required: ['_ihardwareIdd'],
      type: 'object',
      properties: {
        hardwareId: { type: 'string', description: 'Sensor ID' },
      },
    },
    body: {
      type: 'object',
      properties: {
        qccheck: { type: 'string', description: 'QC check status' },
        qcby: { type: 'string', description: 'QC checked by' },
        comments: { type: 'string', description: 'QC comment' },
        quality_check_details: {
          type: 'array',
          description: 'Detailed quality check results',
          items: {
            type: 'object',
            properties: {
              damage_check: { type: 'string' },
              stickering_check: { type: 'string' },
              power_check: { type: 'string' },
              master_connecting_gsm: { type: 'string' },
              slave_connecting: { type: 'string' },
              motor_starting: {
                type: 'object',
                properties: {
                  result: { type: 'string' },
                  steps: {
                    type: 'array',
                    items: {
                      type: 'object',
                      properties: {
                        step: { type: 'number' },
                        result: { type: 'string' }
                      }
                    }
                  }
                }
              },
              connecting_to_sensor: { type: 'string' },
              connecting_to_slave: { type: 'string' },
              data_sending: { type: 'string' },
              distance_check: {
                type: 'object',
                properties: {
                  result: { type: 'string' },
                  steps: {
                    type: 'array',
                    items: {
                      type: 'object',
                      properties: {
                        step: { type: 'number' },
                        result: { type: 'string' }
                      }
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
  },
  handler: storeController.updateSensorQC,
});


fastify.get("/api/getSensorsByStatus/:storeId", {
  schema: {
    description: "Get list of sensors grouped by status for a given store",
    tags: ["Store-Data"],
    summary: "Retrieve sensors grouped by status",
    params: {
      type: "object",
      required: ["storeId"],
      properties: {
        storeId: {
          type: "string",
          description: "Store ID",
        },
      },

    },
  },
  handler: storeController.getSensorsByStatus,
});


fastify.get("/api/getbatchnumbers/:storeId/:type", {
  schema: {
    tags: ["Store-Data"],
    description: "This is to Get batch numbers",
    summary: "This is to Get batch numbers",
    params: {
      type: "object",
      properties: {
        storeId: {
          type: "string",
          description: "storeId",
        },
        type: {
          type: "string",
          description: "type",
        },
      },
      required: ["storeId", "type"],
    },
    security: [
      {
        basicAuth: [],
      },
    ],
  },
  // preHandler: fastify.auth([fastify.authenticate]),
  handler: storeController.getbatchnumbers,
});


fastify.get("/api/getbatquotationsforparticularstore/:storeId", {
  schema: {
    tags: ["Store-Data"],
    description: "This is to Get quotations for store based on store id",
    summary: "This is to Get quotations for store based on store id",
    params: {
      type: "object",
      properties: {
        storeId: {
          type: "string",
          description: "storeId",
        },
  
      },
      required: ["storeId"],
    },
    security: [
      {
        basicAuth: [],
      },
    ],
  },
  // preHandler: fastify.auth([fastify.authenticate]),
  handler: storeController.getbatquotationsforparticularstore,
});

fastify.get("/api/getiots/:storeId/:type", {
  schema: {
    tags: ["Store-Data"],
    description: "This is to Get iots of particular store",
    summary: "This is to get iots of particular store",
    params: {
      type: "object",
      properties: {
        storeId: {
          type: "string",
          description: "storeId",
        },
        type: {
          type: "string",
          description: "type",
        },
      },
      required: ["storeId", "type"],
    },
    security: [
      {
        basicAuth: [],
      },
    ],
  },
  // preHandler: fastify.auth([fastify.authenticate]),
  handler: storeController.getiots,
});


fastify.post("/api/createquotationforSensor/:surveyId", {
  schema: {
    description: "This is for create quotation for sensors",
    tags: ["Install"],
    summary: "This is for create quotation for sensors",
    params: {
      required: ["surveyId"],
      type: "object",
      properties: {
        surveyId: {
          type: "string",
          description: "surveyId",
        },
      },
    },
    body: {
      type: "object",
      properties: {
        customerId: { type: "string" },
        
        masters: { type: "string" },
        slaves: { type: "string" },
        sensors: { type: "string" },
        motor_switches: { type: "string" },
        master_connections: {
          type: "array",
          maxItems: 2500,
          items: {
              type: "object",
              properties: {
                  master_name: { type: "string", default: null },
                  slaves: { type: "string", default: null },
                  tanks: {
                      type: "array",
                      items: {
                          type: "object",
                          properties: {
                              tankName: { type: "string", default: null },
                              tankLocation: { type: "string", default: null },
                          },
                      },
                      default: [],
                  },
              },
          },
      },
      
        electricals: {
          type: "array",
          maxItems: 2500,
          items: {
              type: "object",
              properties: {
                  type: { type: "string", default: null },
                  wire: { type: "string", default: null },
                  switch: { type: "string", default: null },
                  text: { type: "string", default: null },
              },
          },
      },
      },
    },
  },
  handler: storeController.createquotationforSensor,
});

fastify.post("/api/createEstimationPrice", {
  schema: {
    description: "Calculate estimated cost for a list of items",
    tags: ["Install"],
    summary: "Fetch unit prices from IotPrice and calculate total price for all items",
    body: {
      type: "object",
      properties: {
        customerId: { type: "string", description: "Customer ID" },
        items: {
          type: "array",
          description: "List of items",
          items: {
            type: "object",
            properties: {
              name: { type: "string", description: "Item name" },
              
              type: { type: "string", description: "Item type" },
              quantity: { type: "string", description: "Quantity of the item" }
            },
            required: ["name", "type", "quantity"]
          }
        }
      },
      required: ["items"]
    }
  },
  handler: storeController.createEstimationPrice
});


fastify.post("/api/handleEstimation", {
  schema: {
    description: "Accept or reject the estimated price",
    tags: ["Install"],
    summary: "Handles user action for estimated price",
    body: {
      type: "object",
      properties: {
        customerId: { type: "string", description: "Customer ID" },
        items: {
          type: "array",
          description: "List of items with price details",
          items: {
            type: "object",
            properties: {
              name: { type: "string", description: "Item name" },
              type: { type: "string", description: "Item type" },
              quantity: { type: "string", description: "Quantity of the item" },
              unitPrice: { type: "number", description: "Unit price" },
              totalCost: { type: "number", description: "Total cost" }
            },
            required: ["name", "type", "quantity", "unitPrice", "totalCost"]
          }
        },
        estimatedTotal: { type: "number", description: "Total estimated cost" },
        action: { type: "string", enum: ["accept", "reject"], description: "User decision" }
      },
      required: ["customerId", "items", "estimatedTotal", "action"]
    }
  },
  handler: storeController.handleEstimation
});

fastify.put("/api/editOrder", {
  schema: {
    description: "Edit an existing order based on orderId",
    tags: ["Install"],
    summary: "Modify items and update the estimated total",
    body: {
      type: "object",
      properties: {
        orderId: { type: "string", description: "Order ID to be updated" },
        customerId: { type: "string", description: "Customer ID associated with the order" },
        items: {
          type: "array",
          description: "Updated list of items",
          items: {
            type: "object",
            properties: {
              name: { type: "string", description: "Item name" },
              type: { type: "string", description: "Item type" },
              quantity: { type: "string", description: "Quantity of the item" },
              unitPrice: { type: "number", description: "Unit price of the item" },
              totalPrice: { type: "number", description: "Total price of the item" }
            },
            required: ["name", "type", "quantity", "unitPrice", "totalPrice"]
          }
        },
        estimatedTotal: { type: "number", description: "Updated estimated total cost" }
      },
      required: ["orderId", "customerId", "items", "estimatedTotal"]
    }
  },
  handler: storeController.editOrder
});

fastify.get("/api/orders/:customerId", {
  schema: {
    description: "Get pending and accepted orders for a particular customer",
    tags: ["Install"],
    summary: "Fetch orders based on customerId",
    params: {
      type: "object",
      properties: {
        customerId: { type: "string", description: "Customer ID whose orders need to be fetched" }
      },
      required: ["customerId"]
    }
  },
  handler: storeController.getOrdersByCustomer
});


fastify.post("/api/editQuotationForSensor/:quatationId", {
  schema: {
    description: "This is for editing quotation for sensors",
    tags: ["Install"],
    summary: "Edit an existing sensor quotation",
    params: {
      required: ["quatationId"],
      type: "object",
      properties: {
        quatationId: {
          type: "string",
          description: "Quotation ID",
        },
      },
    },
    body: {
      type: "object",
      properties: {
        masters: { type: "string" },
        slaves: { type: "string" },
        sensors: { type: "string" },
        motor_switches: { type: "string" },
        electricals: {
          type: "array",
          maxItems: 2500,
          items: {
            type: "object",
            properties: {
              type: { type: "string", default: null },
              wire: { type: "string", default: null },
              switch: { type: "string", default: null },
              text: { type: "string", default: null },
            },
          },
        },
        master_connections: { // Added master_connections to schema
          type: "array",
          maxItems: 2500,
          items: {
            type: "object",
            properties: {
              master_name: { type: "string", default: null },
              slaves: { type: "string", default: null },
              tanks: {
                type: "array",
                items: {
                  type: "object",
                  properties: {
                    tankName: { type: "string", default: null },
                    tankLocation: { type: "string", default: null },
                  },
                },
                default: [],
              },
            },
          },
        },
      },
    },
  },
  handler: storeController.editQuotationForSensor,
});


fastify.post("/api/getquotationofinstalleranduser/:surveyId", {
  schema: {
    tags: ["Install"],
    description: "This is to Get Quotations for survey and particular user",
    summary: "This is to Get Quotations for survey and particular user",
    params: {
      required: ["surveyId"],
      type: "object",
      properties: {
        surveyId: {
          type: "string",
          description: "surveyId",
        },
      },
    },
    body: {
      type: "object",
      
      properties: {
        customerId: { type: "string" },
       
      },
    },
    security: [
      {
        basicAuth: [],
      },
    ],
  },
 // preHandler: fastify.auth([fastify.authenticate]),
  handler: storeController.getquotationofinstalleranduser,
});




fastify.get("/api/getallquotationdata", {
  schema: {
    tags: ["Install"],
    description: "This is for Get All Quotation Data",
    summary: "This is for to Get All Quotation Data",

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

fastify.post("/api/sendBackQuotationforSensor/:quotationId", {
  schema: {
    description: "This is for sending back the quotation for sensors",
    tags: ["Install"],
    summary: "Send back quotation for sensors",
    params: {
      type: "object",
      properties: {
        quotationId: {
          type: "string",
          description: "quotationId", 
        },
      },
    },
    body: {
      type: "object",
      properties: {
        masters_quantity_price: { type: "string" },
        masters_total_price: { type: "string" },
        slaves_quantity_price: { type: "string" },
        slaves_total_price: { type: "string" },
        motor_switches_quantity_price: { type: "string" },
        motor_switches_total_price: { type: "string" },
        electricals_quantity_price: { type: "string" },
        electricals_total_price: { type: "string" },
        master_type_quantity_price: { type: "string" },
        master_type_total_price: { type: "string" },
        sensor_type_quantity_price: { type: "string" },
        sensor_type_total_price: { type: "string" },
        switch_type_quantity_price: { type: "string" },
        switch_type_total_price: { type: "string" },
        quotation_total_price: { type: "string" },
      },
    },
  },
  handler: storeController.saveQuotationData,
});

fastify.get("/api/getSingleQuotationData/:quotationId", {
  schema: {
    tags: ["Install"],
    description: "This is for Get Single Quotation Data",
    summary: "This is to Get Single Quotation Data",
    params: {
      type: "object",
      properties: {
        quotationId: {
          type: "string",
          description: "quotationId", 
        },
      },
    },
    
    security: [
      {
        basicAuth: [],
      },
    ],
  },
  handler: storeController.getSinleQuotationData,
});


fastify.put("/api/updateInstallationId/:_id", {
  schema: {
    tags: ["Install"],
    description: "Update the installationId of an order",
    summary: "Update installationId",
    params: {
      type: "object",
      properties: {
        _id: {
          type: "string",
          description: "_id",
        },
      },
      required: ["_id"],
    },
    body: {
      type: "object",
      properties: {
        installationId: {
          type: "string",
          description: "Installation ID to update",
        },
      },
      required: ["installationId"],
    },
    response: {
      200: {
        description: "Installation ID updated successfully",
        type: "object",
        properties: {
          status_code: { type: "number" },
          message: { type: "string" },
          data: { type: "object" },
        },
      },
      400: {
        description: "Bad request",
        type: "object",
        properties: {
          error: { type: "string" },
        },
      },
      404: {
        description: "Order not found",
        type: "object",
        properties: {
          error: { type: "string" },
        },
      },
      500: {
        description: "Internal server error",
        type: "object",
        properties: {
          error: { type: "string" },
        },
      },
    },
    security: [
      {
        basicAuth: [],
      },
    ],
  },
  handler: storeController.updateInstallationId,
});


fastify.get("/api/getPendingOrders", {
  schema: {
    tags: ["Install"],
    description: "Fetch all orders with status 'pending'",
    summary: "Get all pending orders",
    response: {
      200: {
        type: "object",
        properties: {
          status_code: { type: "number" },
          message: { type: "string" },
          data: { type: "array", items: { type: "object" } },
        },
      },
    },
  },
  handler: storeController.getPendingOrders,
});








fastify.post("/api/cart/hardwareItem", {
  schema: {
    description: "To add items to the Hardwarecart",
    tags: ["Cart"],
    summary: "Add item to Hardwarecart",
    body: {
      type: "object",
      properties: {
        productId: { type: "string", description: "Unique identifier for the product" },
        productName: { type: "string", description: "Name of the product" },
        description: { type: "string", description: "Product description", default: null },
        GST: { type: "number", description: "GST applied to the product", minimum: 0 },
        unitPrice: { type: "number", description: "Unit price of the product", minimum: 0 },
        quantity: { type: "integer", description: "Quantity of the product", minimum: 1 },
        totalAmount: { type: "number", description: "Total amount before GST", minimum: 0 },
        serialId: { type: "string", description: "Serial identifier for the cart item", default: null }
      },
      //required: ["productId", "productName", "GST", "unitPrice", "quantity", "totalAmount"]
    },
  },
  handler: storeController.addToCartHardwareItems
});

fastify.post("/api/cart/installationService", {
  schema: {
    description: "To add items to the Installation Service",
    tags: ["Cart"],
    summary: "Add item to Installation Service",
    body: {
      type: "object",
      properties: {
        installationId: { type: "string" },

        productId: { type: "string"},
        productName: { type: "string" },
        description: { type: "string" },
        GST: { type: "number", description: "GST applied to the product", minimum: 0 },
        unitPrice: { type: "number", description: "Unit price of the product", minimum: 0 },
        quantity: { type: "integer", description: "Quantity of the product", minimum: 1 },
        totalAmount: { type: "number", description: "Total amount before GST", minimum: 0 },
        serialId: { type: "string", description: "Serial identifier for the cart item", default: null }
      },
      //required: ["productId", "productName", "GST", "unitPrice", "quantity", "totalAmount"]
    },
  },
  handler: storeController.addToCartService
});


fastify.post("/api/acceptquotation/:quotationId", {
  schema: {
    tags: ["Install"],
    description: "Accepts a quotation and moves it to the Orders database",
    summary: "Accepts a quotation",
    params: {
      type: "object",
      properties: {
        quotationId: { type: "string" },
      },
    },
    body: {
      type: "object",
      properties: {
        action: { type: "string" },
        storeId: { type: "string" },
      },
      required: ["action"],
    },
    security: [ 
      {
        basicAuth: [],
      },
    ],
  },
  // preHandler: fastify.auth([fastify.authenticate]), // Uncomment if authentication is needed
  handler: storeController.acceptQuotation,
});


fastify.get("/api/ordersofstore/:storeId", {
  schema: {
    tags: ["Install"],
    description: "Fetches orders based on storeId",
    summary: "Get orders by storeId",
    params: {
      type: "object",
      properties: {
        storeId: { type: "string" },
      },
      required: ["storeId"],
    },
    security: [
      {
        basicAuth: [],
      },
    ],
  },
  handler: storeController.getOrdersByStoreId,
});


fastify.get("/api/ordersofinstall/:installationId", {
  schema: {
    tags: ["Installation"],
    description: "Fetches orders based on installationId",
    summary: "Get orders by installationId",
    params: {
      type: "object",
      properties: {
        installationId: { type: "string" },
      },
      required: ["installationId"],
    },
    security: [
      {
        basicAuth: [],
      },
    ],
  },
  handler: storeController.getOrdersByInstallationId,
});


fastify.get("/api/getallocatedsensors/:customerId", {
  schema: {
    tags: ["Install"],
    description: "Fetches sensors based on storeId",
    summary: "Get sensors by customerId",
    params: {
      type: "object",
      properties: {
        customerId: { type: "string" },
      },
      required: ["customerId"],
    },
    security: [
      {
        basicAuth: [],
      },
    ],
  },
  handler: storeController.getallocatedsensorstouser,
});

next();
};
