const fastify = require("fastify");
const adminController = require('../controllers/admincontroller')


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


fastify.route({
  method: "POST",
  url: "/api/adminSignup",
  schema: {
    tags: ["Admin"],
    description: "This is for creating a new Admin/sales/store Account",
    summary: "This is for creating a new Admin/sales/store Account",
    body: {
      type: "object",
      required: ["phone", "username", "password", "role"],  // Add role to required fields
      properties: {
        phone: { type: "string" },
        password: { type: "string" },
        username: { type: "string" },
        role: { type: "string", enum: ["admin", "sales", "store"] },  // Define allowed roles
      },
    },
    security: [
      {
        basicAuth: [],
      },
    ],
  },
  handler: adminController.adminSignUp,
});


  fastify.post("/api/adminLogin", {
    schema: {
      description: "This is for Login Admin",
      tags: ["Admin"],
      summary: "This is for Login Admin",
      body: {
        type: "object",
        required: ["phone", "password"],
        properties: {
          phone : { type: "string" },
          password: { type: "string" },
        },
      },
    },
    handler: adminController.adminLogin,
});

fastify.post("/api/salesStoreLogin", {
  schema: {
    description: "Login for sales/store users",
    tags: ["Sales/Store Login"],
    summary: "Login for sales/store users",
    body: {
      type: "object",
      required: ["phone", "password", "role"],
      properties: {
        phone : { type: "string" },
        password: { type: "string" },
        role: { type: "string", enum: ["sales", "store"] }
      },
    },
  },
  handler: adminController.salesStoreLogin,
});


fastify.get("/api/getUsersByRole/:role", {
  schema: {
    description: "Get list of users by role (sales/store)",
    tags: ["Sales/Store Users"],
    summary: "Get list of users by role",
    params: {
      type: "object",
      properties: {
        role: { type: "string", enum: ["sales", "store"] },
      },
      required: ["role"],
    },
    response: {
      200: {
        type: "array",
        items: {
          type: "object",
          properties: {
            phone: { type: "string" },
            username: { type: "string" },
            role: { type: "string" },
            date: { type: "string", format: "date-time" }
          }
        }
      }
    }
  },
  handler: adminController.getUsersByRole,
});



fastify.put("/api/editUser/:customerId", {
  schema: {
    description: "Edit user details by customer ID",
    tags: ["Sales/Store Users"],
    summary: "Edit user details",
    params: {
      type: "object",
      properties: {
        customerId: { type: "string" },  // Customer ID
      },
      required: ["customerId"],
    },
    body: {
      type: "object",
      properties: {
        phone: { type: "string" },
        username: { type: "string" },
        role: { type: "string", enum: ["sales", "store"] },
        date: { type: "string", format: "date-time" }
      },
      required: ["phone", "username", "role", "date"]
    },
    response: {
      200: {
        type: "object",
        properties: {
          success: { type: "boolean" },
          message: { type: "string" },
        }
      }
    }
  },
  handler: adminController.editUserByCustomerId,
});



fastify.delete("/api/deleteUser/:customerId", {
  schema: {
    description: "Delete a user by customer ID",
    tags: ["Sales/Store Users"],
    summary: "Delete a user by customer ID",
    params: {
      type: "object",
      properties: {
        customerId: { type: "string" },  // Customer ID
      },
      required: ["customerId"],
    },
    response: {
      200: {
        type: "object",
        properties: {
          success: { type: "boolean" },
          message: { type: "string" },
        }
      }
    }
  },
  handler: adminController.deleteUserInfo,
});


fastify.route({
  method: "GET",
  url: "/api/users/:customerId",  // Use path parameters for customerId
  schema: {
    tags: ["Admin"],
    description: "Retrieve user information by customerId",
    summary: "Get user by customerId",
    params: {
      type: "object",
      required: ["customerId"],
      properties: {
        customerId: { type: "string" },
      },
    },
    security: [
      {
        basicAuth: [],
      },
    ],
  },
  handler: adminController.getUserByCustomerId, // Link the handler function
});


// fastify.post("/api/createUser", {
//   schema: {
//     description: "This is for Create sale/store",
//     tags: ["createUser for sale/sore"],
//     summary: "This is for Create sale/store",
//     body: {
//       type: "object",
//       required: ["phone", "password", "role"],
//       properties: {
//         phone : { type: "string" },
//         password: { type: "string" },
//         role: { type: "string", enum: ["sales", "store"] }
//       },
//     },
//   },
//   handler: adminController.createUser,
// });




fastify.post("/api/integratingHardwareidToTank", {
  schema: {
    description: "This is for integrating hardwareId with tank",
    tags: ["Admin"],
    summary: "This is for integrating hardwareId with tank",
    
    body: {
      type: "object",
      
      properties: {
        hardwareId_company:{ type: "string" },
        hardwareId_type:{ type: "string" },
        customerId: { type: "string" },
        tankName: { type: "string" },
        tankLocation: { type: "string" },
        hardwareId: { type: "string" },
      },
    },
  },
  handler: adminController.integratingHardwareidToTank,
});



next();
};
