
const BASE_URL = process.env.EXPO_PUBLIC_API_URL; // TODO change this


function authHeader(token?: string) {
    return token ? { Authorization: `Bearer ${token}` } : {};
}

async function request<T>(
    method: string,
    path: string,
    token?: string,
    body?: object,
    params?: Record<string, any>,
): Promise<T> {
    const url = new URL(`${BASE_URL}${path}`);
    if (params) {
        Object.entries(params).forEach(([k, v]) => {
            if (v !== undefined && v !== null) url.searchParams.set(k, String(v));
        });
    }
    const res = await fetch(url.toString(), {
        method,
        // @ts-ignore
        headers: { "Content-Type": "application/json", ...authHeader(token) },
        ...(body ? { body: JSON.stringify(body) } : {}),
    });
    if (!res.ok) throw new Error(await res.text().catch(() => `HTTP ${res.status}`));
    return res.json();
}

export function get<T>(path: string, token?: string, params?: Record<string, any>) {
    return request<T>("GET", path, token, undefined, params);
}

export function post<T>(path: string, body: object, token?: string) {
    return request<T>("POST", path, token, body);
}

export function put<T>(
    path: string,
    body?: object,
    token?: string
) {
    return request<T>("PUT", path, token, body);
}

export function del<T>(path: string, token?: string) {
    return request<T>("DELETE", path, token);
}

export type Subject = {
    id: string;
    name: string;
};

export type JwtResponse = {
    token: string;
    email: string;
    userId: string;
    message: string;
};

export type UserDTO = {
    id: string;
    email: string;
    subjectSet: Subject[];

};


export type UserUpdateDTO = {
    email: string;
    newEmail?: string;
    password?: string;
    subjectSet: { id: string }[]; // pass existing subscriptions so they aren't wiped
};



export type Entry = {
    id: string;
    title: string;
    timePublished: string;
    groupName: string;
    infoEntry: string;
    paragraph: string;
    filepath?: string;
    subject: Subject;
};

export type SpringPage<T> = {
    content: T[];
    totalPages: number;
    last: boolean;
    number: number;
};


export type LoginPayload = { email: string; password: string };
export type RegisterPayload = { email: string; password: string; name?: string };

export const authApi = {
    login: (p: LoginPayload) => post<JwtResponse>("/login", p),
    register: (p: RegisterPayload) => post<UserDTO>("/register", p),
    getUser: (userId: string, token: string) => get<UserDTO>(`/users/${userId}`, token),
    updateUser: (body: UserUpdateDTO, token: string) => request<UserDTO>("PUT", "/users", token, body),
};



export const subjectsApi = {
    getAll: (token?: string) => get<Subject[]>("/subjects", token),
};

export const subscriptionsApi = {
    subscribe: (userId: string, subjectId: string, token: string) =>
        put<UserDTO>(
            `/users/${userId}/subjects/${subjectId}`,
            {},        // body placeholder
            token
        ),

    unsubscribe: (userId: string, subjectId: string, token: string) =>
        del<UserDTO>(
            `/users/${userId}/subjects/${subjectId}`,
            token
        ),
};
export const entriesApi = {
    getEntries: (params: { subjectId?: string; groupName?: string; page?: number }) =>
        get<SpringPage<Entry>>("/entries", undefined, params),
    getEntry: (id: string) => get<Entry>(`/entries/${id}`),
};
