//include std lib if they havent been included
#ifndef STD_LIB_H
#define STD_LIB_H
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include <math.h>
#include <time.h>
#include <ctype.h>
#endif

#ifndef Utils
#define Utils
#include "sorting.h"
#include "utils.h"
#endif // !Utils
//list of functions
#include "inv_control.h"
#include "tran_control.h"
#include "user_control.h"
#include "role_control.h"
struct user_row *prompt_user();
int admin_menu_user_choices(char* name,char* role);

void admin_menu(){
    Cls();
    welcome_message(stdout);
    //the selection menu
    struct user_row *user = prompt_user();
    if(user == NULL){//invalid user
        return;
    }
    char * Items[] = {
        "Inventory control",
        "Transaction control",
        "User control",
        "Role control",
    };
    char welcome[256];
    sprintf(welcome,"welcome %s(%s)\n",user->name,user->role);
    bool exit = false;
    do{

        switch (choices_selecter(Items,4,welcome))
        {
            case 1:;//action with inventory
                inv_control();
                break;
            case 2://action with transction
                tran_control();
                break;
            case 3://action with user
                user_control();
                break;
            case 4://role management
                role_control();
                break;
            case 5://Exit
                exit = true;
                break;
            default://invalid input ,should not happen,as it is already catch in above function
                printf("Invalid choice\n");
                break;
        }
    }while(!exit);
    
    return;
}

//func for main for admin
struct user_row *prompt_user(){
    long user_id = 0;
    printf("Please tap your card(student card, staff card,etc)(or input the id)(0 to cancel)\n>");
    scanf("%ld", &user_id);
    if(user_id == 0){
        printf("cancelled\n");
        printf("press any key to continue\n");
        fflush_stdin();
        getchar();
        return NULL;
    }
    struct user_row *user = get_user(user_id);
    Cls();
    if(user == NULL){
        printf("User not found\n");
        printf("press any key to continue\n");
        fflush_stdin();
        getchar();
        return NULL;
    }else if(!is_admin(user->role)){
        printf("You aren't an admin\n");
        printf("press any key to continue\n");
        fflush_stdin();
        getchar();
        return NULL;
    }else{
            printf("Welcome %s %s\n", user->name , user->role);
        printf("press any key to continue\n");
        fflush_stdin();
        getchar();
    }
    return user;
}
