import { getSysAdminNonce } from "../helpers/GetSysAdminNonce";
import { getApiHost } from "../helpers/GetApiHost";
import axios from "axios";
import { getPayload } from "../helpers/GetPayload";
import {
    SysAdminPaginateUsersParams,
    PaginateUsersResults,
    SysAdminUpdateProfileParams,
    SysAdminSetVerifiedParams,
    SysAdminDeleteUserParams,
    SysAdminOrganizationsStatusParams,
    SysAdminUserOrganizationsResult,
} from "../models/Users.Model";

export class UsersService {
    public async paginateUsers(filters: SysAdminPaginateUsersParams): Promise<PaginateUsersResults> {
        const nonce = await getSysAdminNonce();
        const postData = { ...filters, ...{ payload: getPayload(nonce) } };

        const response = await axios.post(`${getApiHost()}/sysadmin/users/paginate`, postData, {
            headers: { "Content-Type": "application/json" },
            timeout: 5000,
        });

        return response.data;
    }

    public async updateProfile(data: SysAdminUpdateProfileParams): Promise<any> {
        const nonce = await getSysAdminNonce();
        const postData = { ...data, ...{ payload: getPayload(nonce) } };

        const response = await axios.post(`${getApiHost()}/sysadmin/users/update`, postData, {
            headers: { "Content-Type": "application/json" },
            timeout: 5000,
        });

        return response.data;
    }

    public async setVerifiedStatus(data: SysAdminSetVerifiedParams): Promise<any> {
        const nonce = await getSysAdminNonce();
        const postData = { ...data, ...{ payload: getPayload(nonce) } };

        const response = await axios.post(`${getApiHost()}/sysadmin/users/set-verified`, postData, {
            headers: { "Content-Type": "application/json" },
            timeout: 5000,
        });

        return response.data;
    }

    public async deleteUser(data: SysAdminDeleteUserParams): Promise<any> {
        const nonce = await getSysAdminNonce();
        const postData = { ...data, ...{ payload: getPayload(nonce) } };

        const response = await axios.post(`${getApiHost()}/sysadmin/users/delete`, postData, {
            headers: { "Content-Type": "application/json" },
            timeout: 10000, // Slightly longer timeout for cascading deletes
        });

        return response.data;
    }

    public async organizationsStatus(data: SysAdminOrganizationsStatusParams): Promise<SysAdminUserOrganizationsResult> {
        const nonce = await getSysAdminNonce();
        const postData = { ...data, ...{ payload: getPayload(nonce) } };

        const response = await axios.post(`${getApiHost()}/sysadmin/users/organizations-status`, postData, {
            headers: { "Content-Type": "application/json" },
            timeout: 5000,
        });

        return response.data;
    }
}
