import { SubscriptionPeriod } from '@/app/types/subscription';
import { useAuthStore } from './auth-store';

const API_URL = process.env.NEXT_PUBLIC_API_URL || 'http://localhost:5000';

export interface ApiResponse<T = any> {
    success: boolean;
    data?: T;
    error?: string;
    status: number;
    msg?: string; // Backend often returns 'msg'
    meta?: any;
}

const getAuthToken = (): string | null => {
    if (typeof window === 'undefined') {
        return null;
    }
    try {
        return useAuthStore.getState().token;
    } catch {
        return null;
    }
};

const buildHeaders = (options?: { token?: string; hasBody?: boolean }): HeadersInit => {
    const headers: Record<string, string> = {};
    if (options?.hasBody) {
        headers['Content-Type'] = 'application/json';
    }
    const token = options?.token ?? getAuthToken();
    if (token) {
        headers['x-auth-token'] = token;
    }
    return headers;
};

export const api = {
    team: {
        get: async (token: string): Promise<ApiResponse> => {
            try {
                const res = await fetch(`${API_URL}/api/doctor-manager/team`, {
                    headers: { 'x-auth-token': token }
                });
                const data = await res.json();
                return { success: res.ok, status: res.status, data: data.data, msg: data.msg };
            } catch (err: any) {
                return { success: false, status: 500, error: err.message };
            }
        },
        add: async (token: string, doctorData: any): Promise<ApiResponse> => {
            try {
                const res = await fetch(`${API_URL}/api/doctor-manager/add`, {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json',
                        'x-auth-token': token
                    },
                    body: JSON.stringify(doctorData)
                });
                const data = await res.json();
                // We need to return the raw data and status because the UI checks for 402
                // Extract error message from various possible locations
                const errorMsg = data.error || data.msg || data.message;
                return {
                    success: res.ok,
                    status: res.status,
                    data,
                    msg: errorMsg,
                    error: errorMsg
                };
            } catch (err: any) {
                return { success: false, status: 500, error: err.message, msg: 'Failed to fetch' };
            }
        },
        remove: async (token: string, doctorUserId: string): Promise<ApiResponse> => {
            try {
                const res = await fetch(`${API_URL}/api/doctor-manager/team/${doctorUserId}`, {
                    method: 'DELETE',
                    headers: { 'x-auth-token': token }
                });
                const data = await res.json();
                return { success: res.ok, status: res.status, data, msg: data.message, error: data.error };
            } catch (err: any) {
                return { success: false, status: 500, error: err.message };
            }
        },
        upgradeLimit: async (token: string, newLimit: number): Promise<ApiResponse> => {
            try {
                const res = await fetch(`${API_URL}/api/doctor-manager/upgrade-limit`, {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json',
                        'x-auth-token': token
                    },
                    body: JSON.stringify({ newLimit })
                });
                const data = await res.json();
                return { success: res.ok, status: res.status, data, msg: data.msg };
            } catch (err: any) {
                return { success: false, status: 500, error: err.message };
            }
        }
    },
    subscription: {
        getPlans: async (): Promise<ApiResponse> => {
            try {
                const res = await fetch(`${API_URL}/api/plans`);
                const data = await res.json();
                return { success: res.ok, status: res.status, data: data.data, msg: data.msg };
            } catch (err: any) {
                return { success: false, status: 500, error: err.message };
            }
        },
        getMySubscription: async (token: string): Promise<ApiResponse> => {
            try {
                const res = await fetch(`${API_URL}/api/subscription/me`, {
                    headers: { 'x-auth-token': token }
                });
                const data = await res.json();
                return { success: res.ok, status: res.status, data: data.data, msg: data.message };
            } catch (err: any) {
                return { success: false, status: 500, error: err.message };
            }
        },
        upgrade: async (token: string, userId: string, planId: string, billingCycle: SubscriptionPeriod): Promise<ApiResponse> => {
            try {
                const res = await fetch(`${API_URL}/api/subscription/upgrade`, {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json',
                        'x-auth-token': token
                    },
                    body: JSON.stringify({ userId, planId, billingCycle })
                });
                const data = await res.json();
                return { success: res.ok, status: res.status, data, msg: data.message };
            } catch (err: any) {
                return { success: false, status: 500, error: err.message };
            }
        },
        cancel: async (token: string, subscriptionId?: string): Promise<ApiResponse> => {
            try {
                const res = await fetch(`${API_URL}/api/subscription/cancel`, {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json',
                        'x-auth-token': token
                    },
                    body: JSON.stringify({ subscriptionId })
                });
                const data = await res.json();
                return { success: res.ok, status: res.status, data: data.data, msg: data.message, error: data.error };
            } catch (err: any) {
                return { success: false, status: 500, error: err.message };
            }
        }
    },
    admin: {
        getRegistrations: async (token: string): Promise<ApiResponse> => {
            try {
                const res = await fetch(`${API_URL}/api/admin/registrations`, {
                    headers: { 'x-auth-token': token }
                });
                const data = await res.json();
                return { success: res.ok, status: res.status, data: data.data, error: data.error };
            } catch (err: any) {
                return { success: false, status: 500, error: err.message };
            }
        },
        getPlans: async (token: string): Promise<ApiResponse> => {
            try {
                const res = await fetch(`${API_URL}/api/admin/plans`, {
                    headers: { 'x-auth-token': token }
                });
                const data = await res.json();
                return { success: res.ok, status: res.status, data: data.data, error: data.error };
            } catch (err: any) {
                return { success: false, status: 500, error: err.message };
            }
        },
        seedFreeForever: async (token: string): Promise<ApiResponse> => {
            try {
                const res = await fetch(`${API_URL}/api/admin/plans/seed-free-forever`, {
                    method: 'POST',
                    headers: { 'x-auth-token': token }
                });
                const data = await res.json();
                return { success: res.ok, status: res.status, data: data.data, error: data.error };
            } catch (err: any) {
                return { success: false, status: 500, error: err.message };
            }
        },
        getSubscription: async (token: string, userId: string): Promise<ApiResponse> => {
            try {
                const res = await fetch(`${API_URL}/api/admin/subscription/${userId}`, {
                    headers: { 'x-auth-token': token }
                });
                const data = await res.json();
                return { success: res.ok, status: res.status, data: data.data, error: data.error };
            } catch (err: any) {
                return { success: false, status: 500, error: err.message };
            }
        },
        updateSubscription: async (token: string, userId: string, planId: string): Promise<ApiResponse> => {
            try {
                const res = await fetch(`${API_URL}/api/admin/subscription/${userId}`, {
                    method: 'PUT',
                    headers: { 'Content-Type': 'application/json', 'x-auth-token': token },
                    body: JSON.stringify({ planId })
                });
                const data = await res.json();
                return { success: res.ok, status: res.status, data: data.data, error: data.error };
            } catch (err: any) {
                return { success: false, status: 500, error: err.message };
            }
        }
    },
    content: {
        getManual: async (): Promise<ApiResponse> => {
            try {
                const res = await fetch(`${API_URL}/api/content/manual`);
                const data = await res.json();
                return { success: res.ok, status: res.status, data: data.data, error: data.error };
            } catch (err: unknown) {
                const message = err instanceof Error ? err.message : 'Failed to fetch';
                return { success: false, status: 500, error: message };
            }
        },
        getAdminManual: async (token: string): Promise<ApiResponse> => {
            try {
                const res = await fetch(`${API_URL}/api/content/manual/admin`, {
                    headers: { 'x-auth-token': token }
                });
                const data = await res.json();
                return { success: res.ok, status: res.status, data: data.data, error: data.error };
            } catch (err: unknown) {
                const message = err instanceof Error ? err.message : 'Failed to fetch';
                return { success: false, status: 500, error: message };
            }
        },
        updateAdminManual: async (token: string, body: {
            title: string;
            subtitle: string;
            quickStart: string[];
            menuMap: { name: string; purpose: string }[];
            sections: { id: string; icon: string; title: string; description: string; steps: string[] }[];
            faq: { q: string; a: string }[];
        }): Promise<ApiResponse> => {
            try {
                const res = await fetch(`${API_URL}/api/content/manual/admin`, {
                    method: 'PUT',
                    headers: { 'Content-Type': 'application/json', 'x-auth-token': token },
                    body: JSON.stringify(body)
                });
                const data = await res.json();
                return { success: res.ok, status: res.status, data: data.data, error: data.error };
            } catch (err: unknown) {
                const message = err instanceof Error ? err.message : 'Failed to fetch';
                return { success: false, status: 500, error: message };
            }
        },
        getBlogs: async (limit?: number): Promise<ApiResponse> => {
            try {
                const query = limit ? `?limit=${limit}` : '';
                const res = await fetch(`${API_URL}/api/content/blogs${query}`);
                const data = await res.json();
                return { success: res.ok, status: res.status, data: data.data, error: data.error };
            } catch (err: unknown) {
                const message = err instanceof Error ? err.message : 'Failed to fetch';
                return { success: false, status: 500, error: message };
            }
        },
        getAdminBlogs: async (token: string): Promise<ApiResponse> => {
            try {
                const res = await fetch(`${API_URL}/api/content/blogs/admin`, {
                    headers: { 'x-auth-token': token }
                });
                const data = await res.json();
                return { success: res.ok, status: res.status, data: data.data, error: data.error };
            } catch (err: unknown) {
                const message = err instanceof Error ? err.message : 'Failed to fetch';
                return { success: false, status: 500, error: message };
            }
        },
        createBlog: async (token: string, body: { title: string; excerpt: string; content: string; published?: boolean }): Promise<ApiResponse> => {
            try {
                const res = await fetch(`${API_URL}/api/content/blogs`, {
                    method: 'POST',
                    headers: { 'Content-Type': 'application/json', 'x-auth-token': token },
                    body: JSON.stringify(body)
                });
                const data = await res.json();
                return { success: res.ok, status: res.status, data: data.data, error: data.error };
            } catch (err: unknown) {
                const message = err instanceof Error ? err.message : 'Failed to fetch';
                return { success: false, status: 500, error: message };
            }
        },
        updateBlog: async (
            token: string,
            blogId: string,
            body: { title?: string; excerpt?: string; content?: string; published?: boolean }
        ): Promise<ApiResponse> => {
            try {
                const res = await fetch(`${API_URL}/api/content/blogs/${blogId}`, {
                    method: 'PUT',
                    headers: { 'Content-Type': 'application/json', 'x-auth-token': token },
                    body: JSON.stringify(body)
                });
                const data = await res.json();
                return { success: res.ok, status: res.status, data: data.data, error: data.error };
            } catch (err: unknown) {
                const message = err instanceof Error ? err.message : 'Failed to fetch';
                return { success: false, status: 500, error: message };
            }
        },
        setBlogVisibility: async (token: string, blogId: string, published: boolean): Promise<ApiResponse> => {
            try {
                const res = await fetch(`${API_URL}/api/content/blogs/${blogId}/visibility`, {
                    method: 'PUT',
                    headers: { 'Content-Type': 'application/json', 'x-auth-token': token },
                    body: JSON.stringify({ published })
                });
                const data = await res.json();
                return { success: res.ok, status: res.status, data: data.data, error: data.error };
            } catch (err: unknown) {
                const message = err instanceof Error ? err.message : 'Failed to fetch';
                return { success: false, status: 500, error: message };
            }
        },
        deleteBlog: async (token: string, blogId: string): Promise<ApiResponse> => {
            try {
                const res = await fetch(`${API_URL}/api/content/blogs/${blogId}`, {
                    method: 'DELETE',
                    headers: { 'x-auth-token': token }
                });
                const data = await res.json();
                return { success: res.ok, status: res.status, data: data.data, error: data.error };
            } catch (err: unknown) {
                const message = err instanceof Error ? err.message : 'Failed to fetch';
                return { success: false, status: 500, error: message };
            }
        },
        getTestimonials: async (limit?: number): Promise<ApiResponse> => {
            try {
                const query = limit ? `?limit=${limit}` : '';
                const res = await fetch(`${API_URL}/api/content/testimonials${query}`);
                const data = await res.json();
                return { success: res.ok, status: res.status, data: data.data, error: data.error };
            } catch (err: unknown) {
                const message = err instanceof Error ? err.message : 'Failed to fetch';
                return { success: false, status: 500, error: message };
            }
        },
        getMyTestimonials: async (token: string): Promise<ApiResponse> => {
            try {
                const res = await fetch(`${API_URL}/api/content/testimonials/mine`, {
                    headers: { 'x-auth-token': token }
                });
                const data = await res.json();
                return { success: res.ok, status: res.status, data: data.data, error: data.error };
            } catch (err: unknown) {
                const message = err instanceof Error ? err.message : 'Failed to fetch';
                return { success: false, status: 500, error: message };
            }
        },
        createTestimonial: async (token: string, body: { quote: string; rating: number }): Promise<ApiResponse> => {
            try {
                const res = await fetch(`${API_URL}/api/content/testimonials`, {
                    method: 'POST',
                    headers: { 'Content-Type': 'application/json', 'x-auth-token': token },
                    body: JSON.stringify(body)
                });
                const data = await res.json();
                return { success: res.ok, status: res.status, data: data.data, error: data.error };
            } catch (err: unknown) {
                const message = err instanceof Error ? err.message : 'Failed to fetch';
                return { success: false, status: 500, error: message };
            }
        },
        getAdminTestimonials: async (token: string): Promise<ApiResponse> => {
            try {
                const res = await fetch(`${API_URL}/api/content/testimonials/admin`, {
                    headers: { 'x-auth-token': token }
                });
                const data = await res.json();
                return { success: res.ok, status: res.status, data: data.data, error: data.error };
            } catch (err: unknown) {
                const message = err instanceof Error ? err.message : 'Failed to fetch';
                return { success: false, status: 500, error: message };
            }
        },
        setTestimonialVisibility: async (token: string, testimonialId: string, approved: boolean): Promise<ApiResponse> => {
            try {
                const res = await fetch(`${API_URL}/api/content/testimonials/${testimonialId}/visibility`, {
                    method: 'PUT',
                    headers: { 'Content-Type': 'application/json', 'x-auth-token': token },
                    body: JSON.stringify({ approved })
                });
                const data = await res.json();
                return { success: res.ok, status: res.status, data: data.data, error: data.error };
            } catch (err: unknown) {
                const message = err instanceof Error ? err.message : 'Failed to fetch';
                return { success: false, status: 500, error: message };
            }
        },
        deleteTestimonial: async (token: string, testimonialId: string): Promise<ApiResponse> => {
            try {
                const res = await fetch(`${API_URL}/api/content/testimonials/${testimonialId}`, {
                    method: 'DELETE',
                    headers: { 'x-auth-token': token }
                });
                const data = await res.json();
                return { success: res.ok, status: res.status, data: data.data, error: data.error };
            } catch (err: unknown) {
                const message = err instanceof Error ? err.message : 'Failed to fetch';
                return { success: false, status: 500, error: message };
            }
        },
        submitSupportFeedback: async (body: { name: string; email: string; subject: string; rating: number; message: string }): Promise<ApiResponse> => {
            try {
                const res = await fetch(`${API_URL}/api/content/support-feedback`, {
                    method: 'POST',
                    headers: { 'Content-Type': 'application/json' },
                    body: JSON.stringify(body)
                });
                const data = await res.json();
                return { success: res.ok, status: res.status, data: data.data, error: data.error };
            } catch (err: unknown) {
                const message = err instanceof Error ? err.message : 'Failed to fetch';
                return { success: false, status: 500, error: message };
            }
        },
        getAdminSupportFeedback: async (token: string): Promise<ApiResponse> => {
            try {
                const res = await fetch(`${API_URL}/api/content/support-feedback/admin`, {
                    headers: { 'x-auth-token': token }
                });
                const data = await res.json();
                return { success: res.ok, status: res.status, data: data.data, error: data.error };
            } catch (err: unknown) {
                const message = err instanceof Error ? err.message : 'Failed to fetch';
                return { success: false, status: 500, error: message };
            }
        }
    },
    plans: {
        create: async (token: string, body: { title: string; price: number; period: string; features?: string[] }): Promise<ApiResponse> => {
            try {
                const res = await fetch(`${API_URL}/api/plans`, {
                    method: 'POST',
                    headers: { 'Content-Type': 'application/json', 'x-auth-token': token },
                    body: JSON.stringify(body)
                });
                const data = await res.json();
                return { success: res.ok, status: res.status, data: data.data, error: data.error };
            } catch (err: any) {
                return { success: false, status: 500, error: err.message };
            }
        },
        update: async (token: string, planId: string, body: { title?: string; price?: number; period?: string; features?: string[] }): Promise<ApiResponse> => {
            try {
                const res = await fetch(`${API_URL}/api/plans/${planId}`, {
                    method: 'PUT',
                    headers: {
                        'Content-Type': 'application/json',
                        'x-auth-token': token
                    },
                    body: JSON.stringify(body)
                });
                const data = await res.json();
                return { success: res.ok, status: res.status, data: data.data, msg: data.message, error: data.error };
            } catch (err: any) {
                return { success: false, status: 500, error: err.message };
            }
        },
        delete: async (token: string, planId: string): Promise<ApiResponse> => {
            try {
                const res = await fetch(`${API_URL}/api/plans/${planId}`, {
                    method: 'DELETE',
                    headers: { 'x-auth-token': token }
                });
                const data = await res.json();
                return { success: res.ok, status: res.status, data: data.data, error: data.error };
            } catch (err: any) {
                return { success: false, status: 500, error: err.message };
            }
        },
        reset: async (token: string): Promise<ApiResponse> => {
            try {
                const res = await fetch(`${API_URL}/api/plans/reset`, {
                    method: 'POST',
                    headers: { 'x-auth-token': token }
                });
                const data = await res.json();
                return { success: res.ok, status: res.status, data: data.data, error: data.error };
            } catch (err: any) {
                return { success: false, status: 500, error: err.message };
            }
        }
    },
    dashboard: {
        getStats: async (token: string): Promise<ApiResponse> => {
            try {
                const res = await fetch(`${API_URL}/api/dashboard/stats`, {
                    headers: { 'x-auth-token': token }
                });
                const data = await res.json();
                return { success: res.ok, status: res.status, data: data.data, msg: data.message };
            } catch (err: any) {
                return { success: false, status: 500, error: err.message };
            }
        }
    },
    patients: {
        get: async (params: { page?: number, limit?: number, search?: string } = {}): Promise<ApiResponse> => {
            try {
                const queryParams = new URLSearchParams();
                if (params.page) queryParams.append('page', params.page.toString());
                if (params.limit) queryParams.append('limit', params.limit.toString());
                if (params.search) queryParams.append('search', params.search);

                const res = await fetch(`${API_URL}/api/patients?${queryParams}`, {
                    headers: buildHeaders()
                });
                const data = await res.json();
                return { success: res.ok, status: res.status, data: data.data, msg: data.message, meta: data.meta };
            } catch (err: any) {
                return { success: false, status: 500, error: err.message };
            }
        },
        getById: async (id: string): Promise<ApiResponse> => {
            try {
                const res = await fetch(`${API_URL}/api/patients/${id}`, {
                    headers: buildHeaders()
                });
                const data = await res.json();
                return { success: res.ok, status: res.status, data: data.data, msg: data.message };
            } catch (err: any) {
                return { success: false, status: 500, error: err.message };
            }
        },
        delete: async (id: string): Promise<ApiResponse> => {
            try {
                const res = await fetch(`${API_URL}/api/patients/${id}`, {
                    method: 'DELETE',
                    headers: buildHeaders()
                });
                const data = await res.json();
                return { success: res.ok, status: res.status, data: data.data, msg: data.message, meta: data.meta };
            } catch (err: any) {
                return { success: false, status: 500, error: err.message };
            }
        }
    },
    appointments: {
        get: async (
            token: string,
            params: { upcoming?: boolean, limit?: number, page?: number, status?: string } = {}
        ): Promise<ApiResponse> => {
            try {
                const queryParams = new URLSearchParams();
                if (params.upcoming) queryParams.append('upcoming', 'true');
                if (params.limit) queryParams.append('limit', params.limit.toString());
                if (params.page) queryParams.append('page', params.page.toString());
                if (params.status && params.status !== 'all') queryParams.append('status', params.status);

                const res = await fetch(`${API_URL}/api/appointments?${queryParams}`, {
                    headers: { 'x-auth-token': token }
                });
                const data = await res.json();
                return { success: res.ok, status: res.status, data: data.data, msg: data.message, meta: data.meta };
            } catch (err: any) {
                return { success: false, status: 500, error: err.message };
            }
        }
    },
    doctors: {
        get: async (token: string, params: { page?: number, limit?: number } = {}): Promise<ApiResponse> => {
            try {
                const queryParams = new URLSearchParams();
                if (params.page) queryParams.append('page', params.page.toString());
                if (params.limit) queryParams.append('limit', params.limit.toString());

                const res = await fetch(`${API_URL}/api/doctors?${queryParams}`, {
                    headers: { 'x-auth-token': token }
                });
                const data = await res.json();
                return { success: res.ok, status: res.status, data: data.data, msg: data.message };
            } catch (err: any) {
                return { success: false, status: 500, error: err.message };
            }
        }
    },
    prescriptions: {
        getByPatientId: async (patientId: string): Promise<ApiResponse> => {
            try {
                const res = await fetch(`${API_URL}/api/prescriptions/patient/${patientId}`, {
                    headers: buildHeaders()
                });
                const data = await res.json();
                return { success: res.ok, status: res.status, data: data.data };
            } catch (err: any) {
                return { success: false, status: 500, error: err.message };
            }
        },
        getById: async (id: string): Promise<ApiResponse> => {
            try {
                const res = await fetch(`${API_URL}/api/prescriptions/${id}`, {
                    headers: buildHeaders()
                });
                const data = await res.json();
                return { success: res.ok, status: res.status, data: data.data };
            } catch (err: any) {
                return { success: false, status: 500, error: err.message };
            }
        },
        create: async (body: any): Promise<ApiResponse> => {
            try {
                const res = await fetch(`${API_URL}/api/prescriptions`, {
                    method: 'POST',
                    headers: buildHeaders({ hasBody: true }),
                    body: JSON.stringify(body)
                });
                const data = await res.json();
                return { success: res.ok, status: res.status, data: data.data };
            } catch (err: any) {
                return { success: false, status: 500, error: err.message };
            }
        },
        update: async (id: string, body: any): Promise<ApiResponse> => {
            try {
                const res = await fetch(`${API_URL}/api/prescriptions/${id}`, {
                    method: 'PUT',
                    headers: buildHeaders({ hasBody: true }),
                    body: JSON.stringify(body)
                });
                const data = await res.json();
                return { success: res.ok, status: res.status, data: data.data };
            } catch (err: any) {
                return { success: false, status: 500, error: err.message };
            }
        }
    },
    payment: {
        checkout: async (token: string, data: { userId: string, planId: string, currency?: string, [key: string]: any }): Promise<ApiResponse> => {
            try {
                const res = await fetch(`${API_URL}/api/payments/checkout`, {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json',
                        'x-auth-token': token
                    },
                    body: JSON.stringify(data)
                });
                const responseData = await res.json();
                return { success: res.ok, status: res.status, data: responseData.data, msg: responseData.message || responseData.error };
            } catch (err: any) {
                return { success: false, status: 500, error: err.message };
            }
        }
    }
};

type HttpParams = Record<string, string | number | boolean | undefined>;

export interface HttpResponse<T = any> {
    data: T;
    status: number;
    ok: boolean;
}



const buildUrl = (path: string, params?: HttpParams): string => {
    const normalizedPath = path.startsWith('/api')
        ? path
        : `/api${path.startsWith('/') ? path : `/${path}`}`;
    const url = new URL(`${API_URL}${normalizedPath}`);
    if (params) {
        Object.entries(params).forEach(([key, value]) => {
            if (value !== undefined) {
                url.searchParams.append(key, String(value));
            }
        });
    }
    return url.toString();
};

const request = async <T>(
    path: string,
    options: {
        method: string;
        params?: HttpParams;
        body?: unknown;
        token?: string;
    }
): Promise<HttpResponse<T>> => {
    const url = buildUrl(path, options.params);
    const res = await fetch(url, {
        method: options.method,
        headers: buildHeaders({ token: options.token, hasBody: options.body !== undefined }),
        body: options.body !== undefined ? JSON.stringify(options.body) : undefined
    });
    let data: T;
    try {
        data = (await res.json()) as T;
    } catch {
        data = {} as T;
    }
    return { data, status: res.status, ok: res.ok };
};

export const http = {
    get: async <T>(path: string, options?: { params?: HttpParams; token?: string }): Promise<HttpResponse<T>> => {
        return request<T>(path, { method: 'GET', params: options?.params, token: options?.token });
    },
    post: async <T>(path: string, body?: unknown, options?: { params?: HttpParams; token?: string }): Promise<HttpResponse<T>> => {
        return request<T>(path, { method: 'POST', params: options?.params, body, token: options?.token });
    },
    put: async <T>(path: string, body?: unknown, options?: { params?: HttpParams; token?: string }): Promise<HttpResponse<T>> => {
        return request<T>(path, { method: 'PUT', params: options?.params, body, token: options?.token });
    },
    delete: async <T>(path: string, options?: { params?: HttpParams; token?: string }): Promise<HttpResponse<T>> => {
        return request<T>(path, { method: 'DELETE', params: options?.params, token: options?.token });
    }
};
