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

interface profileGet {
    data: {
        id: number,
        first_name: string,
        last_name: string,
        phone_number: string,
        profile_photo: string,
    }
}


export type ProfilePass = {
    password: string;
};


export const profile = createApi({
    reducerPath: "profile",
    baseQuery: baseQueryWithReauth, // Use enhanced baseQuery with error handling
    endpoints: (builder) => ({
        profilePassword: builder.mutation<ProfilePass, Partial<ProfilePass>>({
            query: (data) => ({
                url: "/profile-password",
                method: "POST",
                body: data,
            }),
        }),
        // 🔹 GET: Fetch all posts
        getProfile: builder.query<profileGet, void>({
            query: () => "/profile-view",
        }),

        verifyOTP: builder.mutation({
            query: (updatedData) => ({
                url: "/profile-otp",
                method: "POST",
                body: updatedData,
            }),
        }),

        newPasswordSet: builder.mutation({
            query: (updatedData) => ({
                url: "/profile-change-password",
                method: "POST",
                body: updatedData,
            }),
        }),

        resendOtp: builder.mutation({
            query: ({ data }) => ({
                url: `/profile-resend-otp`,
                method: "POST",
                body: data,
            }),
        }),

    }),
});

export const {
    useGetProfileQuery,
    useProfilePasswordMutation,
    useVerifyOTPMutation,
    useNewPasswordSetMutation,
    useResendOtpMutation
} = profile;