import { createRoute, OpenAPIHono, z } from "@hono/zod-openapi";

import type { User } from "../../../../types/users.js";
import { verify } from "hono/jwt";
import { getSettings } from "../../controller/settings/getSettings.js";
import { apiHit } from "../../../../globalUtils/apiHits.js";

const app = new OpenAPIHono();

app.openapi(
    createRoute({
        tags: ["server:settings"],
        summary: "Returns all settings based on your permissions",
        method: "get",
        path: "/settings",
        responses: {
            200: {
                content: {
                    "application/json": {
                        schema: z.object({
                            success: z.boolean().openapi({ example: true }),
                            message: z.string().openapi({ example: "Starter" }),
                        }),
                    },
                },
                description: "Response message",
            },
            400: {
                content: {
                    "application/json": {
                        schema: z.object({
                            message: z
                                .string()
                                .optional()
                                .openapi({ example: "Internal Server error" }),
                        }),
                    },
                },
                description: "Internal Server Error",
            },
            401: {
                content: {
                    "application/json": {
                        schema: z.object({
                            message: z
                                .string()
                                .optional()
                                .openapi({ example: "Unauthenticated" }),
                        }),
                    },
                },
                description: "Unauthorized",
            },
            500: {
                content: {
                    "application/json": {
                        schema: z.object({
                            message: z
                                .string()
                                .optional()
                                .openapi({ example: "Internal Server error" }),
                        }),
                    },
                },
                description: "Internal Server Error",
            },
        },
    }),
    async (c) => {
        // make sure we have a vaid user being accessed thats really logged in
        apiHit(c, { endpoint: `/settings` });
        // now pass all the data over to update the user info
        try {
            const data = await getSettings();
            return c.json(
                { success: true, message: "All Current Settings", data },
                200
            );
        } catch (error) {
            return c.json(
                { message: "There was an error getting the settings.", error },
                400
            );
        }
    }
);
export default app;
