import { createRoute, OpenAPIHono, z } from "@hono/zod-openapi";
import { setUserAccess } from "../../controllers/userRoles/setUserRoles.js";
import { apiHit } from "../../../../globalUtils/apiHits.js";
import { apiReturn } from "../../../../globalUtils/apiReturn.js";
import { authMiddleware } from "../../middleware/authMiddleware.js";
import hasCorrectRole from "../../middleware/roleCheck.js";
import { responses } from "../../../../globalUtils/routeDefs/responses.js";
import { updateUserADM } from "../../controllers/userAdmin/updateUserAdm.js";

const app = new OpenAPIHono();

const responseSchema = z.object({
    success: z.boolean().openapi({ example: true }),
    message: z.string().optional().openapi({ example: "user access" }),
    data: z.array(z.object({})).optional().openapi({ example: [] }),
});

const UserAccess = z.object({
    user_id: z.string().openapi({ example: "users UUID" }),
    username: z
        .string()
        .regex(/^[a-zA-Z0-9_]{3,30}$/)
        .optional()
        .openapi({ example: "smith034" }),
    email: z
        .string()
        .email()
        .optional()
        .openapi({ example: "smith@example.com" }),
    password: z
        .string()

        .optional()
        .openapi({ example: "Password1!" }),
});

app.openapi(
    createRoute({
        tags: ["Auth:admin"],
        summary: "updates a specific user",
        method: "patch",
        path: "/updateuser",
        middleware: [
            authMiddleware,
            hasCorrectRole(["admin", "systemAdmin"], "admin"),
        ],
        //description: "When logged in you will be able to grant new permissions",
        request: {
            body: {
                content: {
                    "application/json": { schema: UserAccess },
                },
            },
        },
        responses: responses(),
    }),
    async (c) => {
        //apiHit(c, { endpoint: "api/auth/setUserRoles" });
        const userData = await c.req.json();
        try {
            const userUPD: any = await updateUserADM(userData);
            //return apiReturn(c, true, access?.message, access?.data, 200);
            return c.json(
                {
                    success: userUPD.success,
                    message: userUPD.message,
                    data: userUPD.data,
                },
                200
            );
        } catch (error) {
            console.log(error);
            //return apiReturn(c, false, "Error in setting the user access", error, 400);
            return c.json(
                {
                    success: false,
                    message: "Error in setting the user access",
                    data: error,
                },
                400
            );
        }
    }
);
export default app;
