import { apiClient } from './client';
import { User, Product, ApiResponse } from '@/types/user';

export const usersApi = {
    // Get user status by email or mobile - package name is now a parameter
    getUserStatus: async (identifier: string, packageName: string, token: string): Promise<User> => {
        // Check if identifier is email or mobile
        const isEmail = identifier.includes('@');
        const endpoint = isEmail
            ? `/admin/users/${identifier}/status?package_name=${packageName}`
            : `/admin/users/${identifier}/status?package_name=${packageName}`;

        return apiClient.get<User>(endpoint, token);
    },

    // Subscribe user to a product
    subscribeUser: async (identifier: string, productId: number, token: string): Promise<ApiResponse<any>> => {
        const data = {
            product_id: productId
        };

        return apiClient.put<ApiResponse<any>>(
            `/admin/users/${identifier}/status`,
            data,
            token
        );
    },

    // Unsubscribe user from a product
    unsubscribeUser: async (identifier: string, productId: number, token: string): Promise<ApiResponse<any>> => {
        const data = {
            product_id: productId
        };

        return apiClient.delete<ApiResponse<any>>(
            `/admin/users/${identifier}/status`,
            data,
            token
        );
    },

    // Get user transactions
    getUserTransactions: async (userId: number, packageName: string, token: string): Promise<any> => {
        return apiClient.get(`/admin/users/${userId}/transactions?package_name=${packageName}`, token);
    },

    // Get products for a specific package
    getProductsByPackage: async (packageName: string, token: string): Promise<Product[]> => {
        return apiClient.get<Product[]>(`/package-names/${packageName}/products`, token);
    }
};