const installationController = require("../controllers/installationController")

module.exports = function (fastify, opts, next) {
  
    fastify.post("/api/createTeamMember", {
        schema: {
          description: "Create a new team member under an installation",
          tags: ["Installation"],
          summary: "Create Team Member",
          body: {
            type: "object",
            required: ["departmentId", "firstName", "phone", "password"],
            properties: {
              departmentId: { type: "string", description: "Installation ID to associate the team member with" },
              firstName: { type: "string", description: "Full name of the team member" },
              phone: { type: "string", description: "Phone number of the team member" },
              password: { type: "string", description: "Password for the team member" },
              alternativePhone: { type: "string",  },
              email: { type: "string", },
              status: { type: "string", },

            },
          },
        
        },
        handler: installationController.createTeamMember,
      });

      fastify.get("/api/getTeamMembers/:departmentId", {
        schema: {
          description: "Get all team members under a specific department",
          tags: ["Installation"],
          summary: "Get Team Members by Department ID",
          params: {
            type: "object",
            properties: {
              departmentId: { 
                type: "string", 
                description: "Department ID to fetch team members from" 
              }
            },
            required: ["departmentId"]
          },
        },
        handler: installationController.getTeamMembers
      });

      fastify.get("/api/getQuations/:installationId", {
        schema: {
          description: "Get all quatations under a specific installation",
          tags: ["Installation"],
          summary: "Get all quatations under a specific installation",
          params: {
            type: "object",
            properties: {
              installationId: { 
                type: "string", 
                description: "Installation ID to fetch team members from" 
              }
            },
            required: ["installationId"]
          },

        },
        handler: installationController.getQuotationsByInstallationId
      });
      fastify.get("/api/getQuations/:installationId/:teamMemberId", {
        schema: {
          description: "Get all quatations under a specific installation and team member",
          tags: ["Installation"],
          summary: "Get all quatations under a specific installation and team member",
          params: {
            type: "object",
            properties: {
              installationId: { 
                type: "string", 
                description: "Installation ID to fetch team members from" 
              },
              teamMemberId: { 
                type: "string", 
                description: "teamMember ID to fetch team members from" 
              }
            },
           // required: ["installationId"]
          },

        },
        handler: installationController.getQuotationsByInstallationAndTeamMember
      });
      fastify.post("/api/assignTeammember/:installationId", {
        schema: {
            description: "Assign a team member to an installation's quotation",
            tags: ["Installation"],
            summary: "Assign a team member based on installationId",
            params: {
                type: "object",
                properties: {
                    installationId: { 
                        type: "string", 
                        description: "Installation ID to fetch team members from" 
                    }
                },
                required: ["installationId"]
            },
            body: {
                type: "object",
                properties: {
                    teamMemberId: { 
                        type: "string", 
                        description: "The team member ID to assign" 
                    },
                    quotationId: { 
                        type: "string", 
                        description: "The team member ID to assign" 
                    }
                },
               // required: ["teamMemberId"]
            },
        },
        handler: installationController.assignTeamMemberToQuotation
    });
    

      fastify.get("/api/getAllInstallers/:departmentName", {
        schema: {
          description: "Get All Installtion list",
          tags: ["Installation"],
          summary: "Get All Installtion list",
          params: {
            type: "object",
            properties: {
              departmentName: { 
                type: "string", 
                description: "departmentName to fetch Installation list" 
              }
            },
            required: ["departmentName"]
          },
        },
        handler: installationController.getAllInstallers
      });

      fastify.put("/api/editTeamMember/:installationId/:teamMemberId", {
        schema: {
          description: "Update an existing team member's details",
          tags: ["Installation"],
          summary: "Edit Team Member",
          params: {
            type: "object",
            properties: {
              installationId: { type: "string", description: "Installation ID" },
              teamMemberId: { type: "string", description: "Team Member ID" }
            },
            required: ["installationId", "teamMemberId"]
          },
          body: {
            type: "object",
            properties: {
              name: { type: "string" },
              phone: { type: "string" },
              email: { type: "string" },
              alternativePhone: { type: "string" },
              status: { type: "string" }
            }
          },
          response: {
            200: {
              type: "object",
              properties: {
                simplydata: {
                  type: "object",
                  properties: {
                    error: { type: "boolean" },
                    message: { type: "string" }
                  }
                }
              }
            }
          }
        },
        handler: installationController.editTeamMember
      });
      
      fastify.delete("/api/deleteTeamMember/:installationId/:teamMemberId", {
        schema: {
          description: "Delete a team member from an installation",
          tags: ["Installation"],
          summary: "Delete Team Member",
          params: {
            type: "object",
            properties: {
              installationId: { type: "string", description: "Installation ID" },
              teamMemberId: { type: "string", description: "Team Member ID" }
            },
            required: ["installationId", "teamMemberId"]
          },
          response: {
            200: {
              type: "object",
              properties: {
                simplydata: {
                  type: "object",
                  properties: {
                    error: { type: "boolean" },
                    message: { type: "string" }
                  }
                }
              }
            }
          }
        },
        handler: installationController.deleteTeamMember
      });
      
      fastify.get("/api/getDepartmentByFirstName/:firstName", {
        schema: {
          description: "Get department details by first name",
          tags: ["Installation"],
          summary: "Fetch department's firstName and phone",
          params: {
            type: "object",
            properties: {
              firstName: { type: "string", description: "Department's first name" },
            },
            required: ["firstName"],
          },
        },
        handler: installationController.getDepartmentByFirstName,
      });
      
      fastify.get("/api/getGsmCheck/:hardwareId", {
        schema: {
          description: "Get GSM check details",
          tags: ["Installation"],
          summary: "Get GSM check details",
          params: {
            type: "object",
            properties: {
              hardwareId: { type: "string" },
            },
            required: ["hardwareId"],
          },
        },
        handler: installationController.getByHardwareId,
      });
      
      fastify.get("/api/getLoraCheck/:hardwareId/:tankhardwareId", {
        schema: {
          description: "Get LORA check details",
          tags: ["Installation"],
          summary: "Get LORA check details",
          params: {
            type: "object",
            properties: {
              hardwareId: { type: "string" },
              tankhardwareId: { type : "string"},
            },
            required: ["hardwareId","tankhardwareId"],
          },
        },
        handler: installationController.getByHardwareAndTankId,
      });
    
      fastify.get("/api/getAllocatedSensorsByTank/:customerId/:tankName", {
        schema: {
          description: "Get allocated sensors by installationId, customerId, and tankName",
          tags: ["Installation"],
          summary: "Fetch allocated sensors for a given tank",
          params: {
            type: "object",
            properties: {
            //  installationId: { type: "string" },
              customerId: { type: "string" },
              tankName: { type: "string" },
            },
            required: [ "customerId", "tankName"],
          },
        },
        handler: installationController.getAllocatedSensorsByTank,
      });
      
      fastify.post("/api/createMasterSlaveData/:installationId", {
        schema: {
          description: "Create a new Master-Slave data entry under an installation",
          tags: ["Installation"],
          summary: "Save Master-Slave Data",
          params: {
            type: "object",
            required: ["installationId"],
            properties: {
              installationId: { type: "string", description: "Installation ID to associate the master-slave data with" }
            }
          },
          body: {
            type: "object",
            required: ["hardwareId", "masterId"],
            properties: {
              type: { type: "string", description: "Type of the device (master/slave)" },
              hardwareId: { type: "string", description: "Hardware ID of the device" },
              batchno: { type: "string", description: "Batch number (optional)" },
              masterId: { type: "string", description: "Master ID associated with the device" },
              tankName: { type: "string", description: "Name of the tank" },
              tankLocation: { type: "string", description: "Location of the tank" },
              materialRecived: { type: "string", description: "Material received status" },
              electicityWork: { type: "string", description: "Electricity work status" },
              plumbingWork: { type: "string", description: "Plumbing work status" },
              loraCheck: { type: "string", description: "LoRa check status" }
            }
          }
        },
        handler: installationController.createMasterSlaveData
      });
      
    next();

}