import { serverFetchJson } from "@/app/utils/fetchUtil";
import { BASE_API_URL } from "@/config/api";
import { cache } from "react";
import "server-only";

export interface UserResult {
    action: any;
    id: number;
    name: string;
    locale: string;
    username: string;
    fullName: string;
    firstname: string;
    lastname: string;
    title: string;
    department: string;
    email: string;
    phone1: string;
    phone2: string;
    remarks: string;
    groupId: number;
    auths: any
  }

// export interface DetailedUser extends UserResult {
//     username: string;
//     password: string
// }

export interface UserDetail {
    data: UserResult;
    authIds: number[];
    groupIds: number[];
    auths: any[]
  }

  export type passwordRule = {
    min: number;
    max: number;
    number: boolean;
    upperEng: boolean;
    lowerEng: boolean;
    specialChar: boolean;
  }

  export const preloadUser = () => {
    fetchUser();
  };

  export const preloadUserDetail = (id: number) => {
    fetchUserDetail(id);
  };

  export const fetchUser = cache(async () => {
    return serverFetchJson<UserResult[]>(`${BASE_API_URL}/user`, {
      next: { tags: ["user"] },
    });
  });

  export const fetchUserDetail = cache(async (id: number) => {
    return serverFetchJson<UserResult[]>(`${BASE_API_URL}/user/${id}`, {
      next: { tags: ["user"] },
    });
  });

  export const fetchPwRules = cache(async () => {
    return serverFetchJson<passwordRule>(`${BASE_API_URL}/user/password-rule`, {
      next: { tags: ["pwRule"] },
    });
  });