import type { TConsoleFeatureServices } from "@/internal/console-features/console-features.services";
import type { TConsoleRoleServices } from "@/internal/console-roles/console-roles.services";
import { logError } from "@/lib/logger";
import { error, success } from "@/lib/response";
import type { Context } from "hono";
import type z from "zod";
import type {
  CreateConsoleFeatureSchema,
  CreateRoleSchema,
  SearchRoleSchema,
  UpdateRoleSchema,
} from "./console-user-roles.schema";

type TConsoleUserRolesControllerDeps = {
  ConsoleFeatureServices: TConsoleFeatureServices;
  ConsoleRoleServices: TConsoleRoleServices;
};

export const ConsoleUserRoleController = (
  ctx: Context,
  {
    ConsoleFeatureServices,
    ConsoleRoleServices,
  }: TConsoleUserRolesControllerDeps,
) => {
  const CreateFeature = async () => {
    try {
      const raw: z.infer<typeof CreateConsoleFeatureSchema> =
        await ctx.req.json();
      const newFeature = await ConsoleFeatureServices.CreateFeature(raw);
      return success(ctx, newFeature);
    } catch (err) {
      logError(err);
      return error(ctx, undefined, undefined, err);
    }
  };

  const GetAllFeatures = async () => {
    try {
      const features = await ConsoleFeatureServices.FindFeatures();
      return success(ctx, features);
    } catch (err) {
      logError(err);
      return error(ctx, undefined, undefined, err);
    }
  };

  const CreateRole = async () => {
    try {
      const raw: z.infer<typeof CreateRoleSchema> = await ctx.req.json();
      const newRole = await ConsoleRoleServices.CreateRole(raw);
      return success(ctx, newRole);
    } catch (err) {
      logError(err);
      return error(ctx, undefined, undefined, err);
    }
  };

  const UpdateRole = async () => {
    try {
      const { id } = ctx.req.param();
      const raw: z.infer<typeof UpdateRoleSchema> = await ctx.req.json();
      const updated = await ConsoleRoleServices.UpdateRole(id, raw);
      return success(ctx, updated);
    } catch (err) {
      logError(err);
      return error(ctx, undefined, undefined, err);
    }
  };

  const FindRoleById = async () => {
    try {
      const { id } = ctx.req.param();
      const updated = await ConsoleRoleServices.FindRoleById(id);
      return success(ctx, updated);
    } catch (err) {
      logError(err);
      return error(ctx, undefined, undefined, err);
    }
  };

  const SearchRoles = async () => {
    try {
      const raw: z.infer<typeof SearchRoleSchema> = await ctx.req.query();
      const roles = await ConsoleRoleServices.FindRoles({
        page: raw?.page ? Number.parseInt(raw.page) : undefined,
        pageSize: raw?.pageSize ? Number.parseInt(raw?.pageSize) : undefined,
        searchTerm: raw?.searchTerm,
      });
      return success(ctx, roles);
    } catch (err) {
      logError(err);
      return error(ctx, undefined, undefined, err);
    }
  };

  return {
    CreateFeature,
    CreateRole,
    UpdateRole,
    FindRoleById,
    SearchRoles,
    GetAllFeatures,
  };
};
