import api from './api';

export interface SystemSettings {
    settingsId: string;
    appName: string;
    appSubtitle: string;
    appLogoUrl?: string;
    updatedBy?: string;
}

export const systemSettingsService = {
    getSettings: async (): Promise<SystemSettings> => {
        const response = await api.get('/system-settings');
        return response.data;
    },

    updateSettings: async (settings: Partial<SystemSettings>): Promise<SystemSettings> => {
        // Axios interceptors handle organization-id headers
        const response = await api.put('/system-settings', settings);
        return response.data;
    },

    uploadLogo: async (file: File): Promise<string> => {
        const formData = new FormData();
        formData.append('logo', file);

        const response = await api.post('/system-settings/logo', formData, {
            headers: {
                'Content-Type': 'multipart/form-data'
            }
        });

        return response.data.url;
    },

    getGlobalUsers: async (): Promise<GlobalUser[]> => {
        const response = await api.get('/system-settings/users');
        return response.data;
    },

    getGlobalOrganizations: async (): Promise<GlobalOrganization[]> => {
        const response = await api.get('/system-settings/organizations');
        return response.data;
    },

    toggleOrganizationBan: async (organizationId: string, isBanned: boolean): Promise<any> => {
        const response = await api.post('/system-settings/organizations/ban', { organizationId, isBanned });
        return response.data;
    }
};

export interface GlobalUser {
    _id: string;
    id: string;
    name: string;
    email: string;
    role: string;
    isBanned: boolean;
    createdAt: string;
}

export interface GlobalOrganization {
    _id: string; // organizationId
    id?: string;
    memberCount: number;
    lastActive?: string;
    isBanned: boolean;
    name?: string; // Added
    members: {
        name: string;
        email: string;
        role: 'admin' | 'user' | 'guest';
        userId: string;
        isBanned: boolean;
    }[];
}
