import axios from 'axios';
import { apiConfig } from '@/config/api.config';
import { ActionMenuReponseTypes, RoleList } from '@/types/GlobalTypes';
const API_URL = apiConfig.service_dashboard;

export async function doGetNavbarMenu(): Promise<ActionMenuReponseTypes> {
  try {
    const storedToken = localStorage.getItem(`${import.meta.env.VITE_APP_NAME}-auth-v${import.meta.env.VITE_APP_VERSION}`);
    const token = storedToken ? JSON.parse(storedToken) : null;

    if (!token) {
      throw new Error('No authentication token found');
    }

    const response = await axios.put(`${API_URL}/renew_token/${token.refresh_token}`);
    if (response.status !== 200 || !response.data.status) {
      throw new Error(response.data.message ?? 'Failed to fetch navbar menu');
    }

    return {
      status: true,
      message: 'Successfully retrieved navbar menu',
      data: {
        role_name: response.data.data.role_name,
        roles_list: response.data.data.roles_list,
        token: response.data.data.token
      }
    };
  } catch (error: any) {
    console.error('Error fetching navbar menu:', error.message);
    return {
      status: false,
      message: error.message,
      data: null
    };
  }
}
