import { createApi } from "@reduxjs/toolkit/query/react";
import { baseQueryWithReauth } from "./apiSlice";

export interface UserData {
    id: number;
    principal_type_xid: number
    first_name: string;
    last_name: string;
    phone_number: string;
    gender: string;
    date_of_birth: string;
    is_active: boolean;
    principal_type: {
        id: number;
        principal_type_title: string;
    },
    principle_language_linkss: {
        id: number;
        language_xid: number;
        iam_principal_xid: number;
        language: {
            id: number,
            language_name: string;
        }
    }
}

interface ApiResponse {
    status: string;
    status_code: number;
    message: string;
    data: {
        current_page: number,
        last_page: number,
        total: number,
        from: number,
        per_page: number,
        to: number,
        data: UserData[];
    };
}

export interface CountryEdit {
    status: string;
    status_code: number;
    message: string;
    data: UserData[];
}


export type PostCountry = {
    en_name: string;
    country_code: string;
    phonecode: string;
    capital: string;
    currency: string;
    currency_name: string;
    currency_symbol: string;
};


export const registerUser = createApi({
    reducerPath: "registerUser",
    baseQuery: baseQueryWithReauth, // Use enhanced baseQuery with error handling
    endpoints: (builder) => ({
        createUser: builder.mutation<PostCountry, Partial<PostCountry>>({
            query: (data) => ({
                url: "/manage-user-store",
                method: "POST",
                body: data,
            }),
        }),
        // 🔹 GET: Fetch all posts
        getManageUser: builder.query<ApiResponse, number>({
            query: (page = 1) => `/manage-user-list?page=${page}`,
        }),

        getDeactivateUser: builder.query<ApiResponse, number>({
            query: (page = 1) => `/manage-user-deactivate-list?page=${page}`,
        }),

        updateUser: builder.mutation({
            query: (updatedData) => ({
                url: "/manage-user-update",
                method: "POST",
                body: updatedData,
            }),
        }),

        userToggle: builder.mutation({
            query: ({ id, is_active }) => ({
                url: `/manage-user-status`,
                method: "POST",
                body: { id, is_active },
            }),
        }),

        userDeactivateToggle: builder.mutation({
            query: ({ id, is_active }) => ({
                url: `/manage-user-deactivate-status`,
                method: "POST",
                body: { id, is_active },
            }),
        }),

        deleteUser: builder.mutation({
            query: ({ id }) => ({
                url: `/manage-user-delete`,
                method: "POST",
                body: { id },
            }),
        }),

    }),
});

export const {
    useGetManageUserQuery,
    useCreateUserMutation,
    useUpdateUserMutation,
    useUserToggleMutation,
    useGetDeactivateUserQuery,
    useUserDeactivateToggleMutation,
    useDeleteUserMutation
} = registerUser;