//
// Typescript API generated from gofiber backend
// Copyright (C) 2022 - 2025 Fabio Prada
//
// This file was generated by github.com/millevolte/ts-rpc
//
// Apr 05, 2026 17:08:11 UTC
//

export interface ApiRestResponse {
  data?: unknown;
  error: string | null;
}

function isApiRestResponse(data: unknown): data is ApiRestResponse {
  return (
    typeof data === "object" &&
    data !== null &&
    Object.prototype.hasOwnProperty.call(data, "data") &&
    Object.prototype.hasOwnProperty.call(data, "error")
  );
}

function normalizeError(error: unknown): Error {
  if (error instanceof DOMException && error.name === "AbortError") {
    return new Error("api.error.timeouterror");
  }

  if (error instanceof TypeError && error.message === "Failed to fetch") {
    return new Error("api.error.connectionerror");
  }

  if (error instanceof Error) {
    return error;
  }

  return new Error(String(error));
}

export default class Api {
  apiUrl: string;
  localStorage: Storage | null;

  constructor(apiurl: string) {
    this.apiUrl = apiurl;
    this.localStorage = window.localStorage;
  }

  async request(
    method: string,
    url: string,
    data: unknown,
    timeout = 7000,
    upload = false,
  ): Promise<ApiRestResponse> {
    const headers: { [key: string]: string } = {
      "Cache-Control": "no-cache",
    };

    if (!upload) {
      headers["Content-Type"] = "application/json";
    }

    if (this.localStorage) {
      const auth = this.localStorage.getItem("Auth-Token");
      if (auth) {
        headers["Auth-Token"] = auth;
      }
    }

    const controller = new AbortController();
    const timeoutId = setTimeout(() => controller.abort(), timeout);
    const requestOptions: RequestInit = {
      method,
      cache: "no-store",
      mode: "cors",
      credentials: "include",
      headers,
      signal: controller.signal,
    };

    if (upload) {
      requestOptions.body = data as FormData;
    } else if (data !== null && data !== undefined) {
      requestOptions.body = JSON.stringify(data);
    }

    try {
      const response = await fetch(url, requestOptions);

      if (!response.ok) {
        throw new Error("api.error." + response.statusText);
      }

      if (this.localStorage) {
        const jwt = response.headers.get("Auth-Token");
        if (jwt) {
          this.localStorage.setItem("Auth-Token", jwt);
        }
      }

      const responseData = (await response.json()) as unknown;
      if (!isApiRestResponse(responseData)) {
        throw new Error("api.error.wrongdatatype");
      }

      if (responseData.error) {
        throw new Error(responseData.error);
      }

      return responseData;
    } catch (error: unknown) {
      throw normalizeError(error);
    } finally {
      clearTimeout(timeoutId);
    }
  }

  processResult(result: ApiRestResponse): {
    data: unknown;
    error: string | null;
  } {
    if (typeof result.data !== "object") {
      return { data: result.data, error: null };
    }

    if (!result.data) {
      result.data = {};
    }

    return { data: result.data, error: null };
  }

  processError(error: unknown): {
    data: unknown;
    error: string | null;
  } {
    const normalizedError = normalizeError(error);

    if (normalizedError.message === "api.error.timeouterror") {
      Object.defineProperty(normalizedError, "__api_error__", {
        value: normalizedError.message,
        writable: false,
      });

      return { data: null, error: normalizedError.message };
    }

    if (normalizedError.message === "api.error.connectionerror") {
      Object.defineProperty(normalizedError, "__api_error__", {
        value: normalizedError.message,
        writable: false,
      });

      return { data: null, error: normalizedError.message };
    }

    return {
      data: null,
      error: normalizedError.message,
    };
  }

  async POST(
    url: string,
    data: unknown,
    timeout?: number,
  ): Promise<{
    data: unknown;
    error: string | null;
  }> {
    try {
      const upload = url.includes("/upload/");
      const result = await this.request(
        "POST",
        this.apiUrl + url,
        data,
        timeout,
        upload,
      );

      return this.processResult(result);
    } catch (error: unknown) {
      return this.processError(error);
    }
  }

  async PUT(
    url: string,
    data: unknown,
    timeout?: number,
  ): Promise<{
    data: unknown;
    error: string | null;
  }> {
    try {
      const upload = url.includes("/upload/");
      const result = await this.request(
        "PUT",
        this.apiUrl + url,
        data,
        timeout,
        upload,
      );

      return this.processResult(result);
    } catch (error: unknown) {
      return this.processError(error);
    }
  }

  async GET(
    url: string,
    timeout?: number,
  ): Promise<{
    data: unknown;
    error: string | null;
  }> {
    try {
      const result = await this.request(
        "GET",
        this.apiUrl + url,
        null,
        timeout,
      );
      return this.processResult(result);
    } catch (error: unknown) {
      return this.processError(error);
    }
  }

  async DELETE(
    url: string,
    timeout?: number,
  ): Promise<{
    data: unknown;
    error: string | null;
  }> {
    try {
      const result = await this.request(
        "DELETE",
        this.apiUrl + url,
        null,
        timeout,
      );
      return this.processResult(result);
    } catch (error: unknown) {
      return this.processError(error);
    }
  }

  async UPLOAD(
    url: string,
    data: unknown,
    timeout?: number,
  ): Promise<{
    data: unknown;
    error: string | null;
  }> {
    try {
      const result = await this.request(
        "POST",
        this.apiUrl + url,
        data,
        timeout,
        true,
      );

      return this.processResult(result);
    } catch (error: unknown) {
      return this.processError(error);
    }
  }
}

const api = new Api("http://localhost:3000");

// Global Declarations
export type Nullable<T> = T | null;

export type Record<K extends string | number | symbol, T> = { [P in K]: T };

//
// package model
//

export interface RefreshRequest {
  refresh_token: string;
}

export interface TokenPair {
  access_token: string;
  refresh_token: string;
}

export interface ForgotPasswordRequest {
  email: string;
}

export interface LoginRequest {
  username: string;
  password: string;
}

export interface ResetPasswordRequest {
  token: string;
  password: string;
}

//
// package controllers
//

export interface BlockUserRequest {
  action: string;
}

export interface ListUsersRequest {
  page: number;
  pageSize: number;
}

export interface SimpleResponse {
  message: string;
}

export interface UpdateUserRequest {
  name: string;
  email: string;
  password: string;
  roles: models.UserRoles;
  status: models.UserStatus;
  types: models.UserTypes;
  avatar: Nullable<string>;
  details: Nullable<models.UserDetailsShort>;
  preferences: Nullable<models.UserPreferencesShort>;
}

//
// package routes
//

// Typescript: TSEndpoint= path=/metrics;  name=metrics; method=GET; response=string
// internal/http/routes/system_routes.go Line: 37
export const metrics = async (): Promise<{
  data: string;
  error: Nullable<string>;
}> => {
  return (await api.GET("/metrics")) as {
    data: string;
    error: Nullable<string>;
  };
};

// Typescript: TSEndpoint= path=/maildebug; name=mailDebug; method=GET; response=routes.[]MailDebugItem
// internal/http/routes/system_routes.go Line: 48
export const mailDebug = async (): Promise<{
  data: MailDebugItem[];
  error: Nullable<string>;
}> => {
  return (await api.GET("/maildebug")) as {
    data: MailDebugItem[];
    error: Nullable<string>;
  };
};

// Typescript: TSEndpoint= path=/users/:uuid; name=getUser; method=GET; response=models.UserProfile
// internal/http/routes/user_routes.go Line: 13
export const getUser = async (
  uuid: string,
): Promise<{ data: UserProfile; error: Nullable<string> }> => {
  return (await api.GET(`/users/${uuid}`)) as {
    data: UserProfile;
    error: Nullable<string>;
  };
};

// Typescript: TSEndpoint= path=/users/:uuid; name=updateUser; method=PUT; request=controllers.UpdateUserRequest; response=models.UserProfile
// internal/http/routes/user_routes.go Line: 19

export const updateUser = async (
  data: UpdateUserRequest,
): Promise<{ data: UserProfile; error: Nullable<string> }> => {
  return (await api.PUT("/users/:uuid", data)) as {
    data: UserProfile;
    error: Nullable<string>;
  };
};

// Typescript: TSEndpoint= path=/admin/users; name=listUsers; method=POST; request=controllers.ListUsersRequest; response=models.[]UserShort
// internal/http/routes/admin_routes.go Line: 12

export const listUsers = async (
  data: ListUsersRequest,
): Promise<{ data: UserShort[]; error: Nullable<string> }> => {
  return (await api.POST("/admin/users", data)) as {
    data: UserShort[];
    error: Nullable<string>;
  };
};

// Typescript: TSEndpoint= path=/admin/users/:uuid/block; name=blockUser; method=PUT; request=controllers.BlockUserRequest; response=models.UserShort
// internal/http/routes/admin_routes.go Line: 15

export const blockUser = async (
  data: BlockUserRequest,
): Promise<{ data: UserShort; error: Nullable<string> }> => {
  return (await api.PUT("/admin/users/:uuid/block", data)) as {
    data: UserShort;
    error: Nullable<string>;
  };
};

// Typescript: TSEndpoint= path=/users; name=createUser; method=POST; request=models.UserCreateInput; response=models.UserProfile
// internal/http/routes/user_routes.go Line: 16

export const createUser = async (
  data: UserCreateInput,
): Promise<{ data: UserProfile; error: Nullable<string> }> => {
  return (await api.POST("/users", data)) as {
    data: UserProfile;
    error: Nullable<string>;
  };
};

// Typescript: TSEndpoint= path=/users/:uuid; name=deleteUser; method=DELETE; response=controllers.SimpleResponse
// internal/http/routes/user_routes.go Line: 22

export const deleteUser = async (
  uuid: string,
): Promise<{ data: SimpleResponse; error: Nullable<string> }> => {
  return (await api.DELETE(`/users/${uuid}`)) as {
    data: SimpleResponse;
    error: Nullable<string>;
  };
};

// Typescript: TSEndpoint= path=/health; name=health; method=GET; response=string
// internal/http/routes/system_routes.go Line: 34
export const health = async (): Promise<{
  data: string;
  error: Nullable<string>;
}> => {
  return (await api.GET("/health")) as {
    data: string;
    error: Nullable<string>;
  };
};

export interface FormRequest {
  req: string;
  count: number;
}

export interface FormResponse {
  test: string;
}

export interface MailDebugItem {
  name: string;
  content: string;
}

//
// package endpoint
//

// Typescript: TSEndpoint= path=/auth/password/forgot; name=forgotPassword; method=POST; request=model.ForgotPasswordRequest; response=controllers.SimpleResponse
// internal/auth/endpoint/routes.go Line: 34

export const forgotPassword = async (
  data: ForgotPasswordRequest,
): Promise<{ data: SimpleResponse; error: Nullable<string> }> => {
  return (await api.POST("/auth/password/forgot", data)) as {
    data: SimpleResponse;
    error: Nullable<string>;
  };
};

// Typescript: TSEndpoint= path=/auth/password/reset; name=resetPassword; method=POST; request=model.ResetPasswordRequest; response=controllers.SimpleResponse
// internal/auth/endpoint/routes.go Line: 37

export const resetPassword = async (
  data: ResetPasswordRequest,
): Promise<{ data: SimpleResponse; error: Nullable<string> }> => {
  return (await api.POST("/auth/password/reset", data)) as {
    data: SimpleResponse;
    error: Nullable<string>;
  };
};

// Typescript: TSEndpoint= path=/auth/password/valid; name=validToken; method=POST; request=string; response=controllers.SimpleResponse
// internal/auth/endpoint/routes.go Line: 40

export const validToken = async (
  data: string,
): Promise<{ data: SimpleResponse; error: Nullable<string> }> => {
  return (await api.POST("/auth/password/valid", data)) as {
    data: SimpleResponse;
    error: Nullable<string>;
  };
};

// Typescript: TSEndpoint= path=/auth/login; name=login; method=POST; request=model.LoginRequest; response=model.TokenPair
// internal/auth/endpoint/routes.go Line: 22

export const login = async (
  data: LoginRequest,
): Promise<{ data: TokenPair; error: Nullable<string> }> => {
  return (await api.POST("/auth/login", data)) as {
    data: TokenPair;
    error: Nullable<string>;
  };
};

// Typescript: TSEndpoint= path=/auth/refresh; name=refresh; method=POST; request=model.RefreshRequest; response=model.TokenPair
// internal/auth/endpoint/routes.go Line: 25

export const refresh = async (
  data: RefreshRequest,
): Promise<{ data: TokenPair; error: Nullable<string> }> => {
  return (await api.POST("/auth/refresh", data)) as {
    data: TokenPair;
    error: Nullable<string>;
  };
};

// Typescript: TSEndpoint= path=/auth/me; name=me; method=GET; response=models.UserShort
// internal/auth/endpoint/routes.go Line: 28
export const me = async (): Promise<{
  data: UserShort;
  error: Nullable<string>;
}> => {
  return (await api.GET("/auth/me")) as {
    data: UserShort;
    error: Nullable<string>;
  };
};

// Typescript: TSEndpoint= path=/auth/register; name=register; method=POST; request=models.UserCreateInput; response=models.UserShort
// internal/auth/endpoint/routes.go Line: 31

export const register = async (
  data: UserCreateInput,
): Promise<{ data: UserShort; error: Nullable<string> }> => {
  return (await api.POST("/auth/register", data)) as {
    data: UserShort;
    error: Nullable<string>;
  };
};

//
// package models
//

export interface UserCreateInput {
  name: string;
  email: string;
  password: string;
  roles: UserRoles;
  status: UserStatus;
  types: UserTypes;
  avatar: Nullable<string>;
  details: Nullable<UserDetailsShort>;
  preferences: Nullable<UserPreferencesShort>;
}

export interface UserDetailsShort {
  title: string;
  firstName: string;
  lastName: string;
  address: string;
  city: string;
  zipCode: string;
  country: string;
  phone: string;
}

export interface UserPreferencesShort {
  useIdle: boolean;
  idleTimeout: number;
  useIdlePassword: boolean;
  idlePin: string;
  useDirectLogin: boolean;
  useQuadcodeLogin: boolean;
  sendNoticesMail: boolean;
  language: string;
}

export interface UserShort {
  email: string;
  name: string;
  roles: UserRoles;
  status: UserStatus;
  uuid: string;
  details: Nullable<UserDetailsShort>;
  preferences: Nullable<UserPreferencesShort>;
  avatar: Nullable<string>;
}

export type UserRoles = string[];

export type UserStatus = (typeof EnumUserStatus)[keyof typeof EnumUserStatus];

export type UserTypes = string[];

export type UsersShort = UserShort[];

export const EnumUserStatus = {
  UserStatusPending: "pending",
  UserStatusActive: "active",
  UserStatusDisabled: "disabled",
} as const;
