/* Includes ------------------------------------------------------------------*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
/* Define --------------------------------------------------------------------*/
#define MAX_ROW 2
#define MAX_COLUMN 15
#define MAX_ENTERPRISE 30
/* Enum ----------------------------------------------------------------------*/
typedef enum Status
{
      Empty,
      Registered
} Status;
typedef enum CommandType
{
      REGISTER,
      ALTER,
      SHOW,
      DELETE,
      QUIT,
      INVALID
} CommandType;
typedef struct
{
      // TODO: Implement this function
      int booth_index;
      char name[100];
      char abbre[100];
      int itemValue;
      int itemWeight;
} Enterprise;
void getAbbreviation(char *name, char *abbre)
{
      // TODO: Implement this function
      int i = 0, j = 0;
    int a = 0; // signal to know if we're inside a word
    //loop through name
    while (name[i] != '\0') {
        // Check if the character is an alphabetic letter (A-Z or a-z)
        if ((name[i] >= 'A' && name[i] <= 'Z') || (name[i] >= 'a' && name[i] <=
'z')) {
            if (!a) {
                 // If it's the start of a word, take the first letter
                 abbre[j++] = name[i];
                 a = 1; // set a=1 indicates that we in a word, which disable this
'if' function
            }
        } else {
            // If it's a space or else, we're outside of a word
            a = 0;
        }
        i++; //add 1 to i to go to the next element
    }
    abbre[j] = '\0'; //end the string by adding null}
}
void registerEnterprise(int map[2][15], Enterprise enterpriseArray[30],char* name,
int booth, int itemValue, int itemWeight,
int* out_booth, char* out_abbre) {
    getAbbreviation(name,out_abbre);
    int default_booth = booth;
    int num_abbre,i=0;
    num_abbre=strlen(out_abbre);
    //printf("%d\n\n",num_abbre);
    // Determine booth if not specified
    if (booth == -1) {
        default_booth = (num_abbre * 30) % 26;
        //printf("%d\n",default_booth);
        // Find the nearest available booth
        int found = 0;
        int upper_limit = MAX_COLUMN;
        for (i=0;i<=1;i++){
        while (!found) {
            if (default_booth <= upper_limit && map[i][default_booth] == 0) {
                found = 1;
                //printf("%d\n",default_booth);
                break;
            } else if (default_booth < upper_limit) {
                default_booth++;
                //printf("%d\n",default_booth);
            } else if (default_booth>=upper_limit){
                while (default_booth >= 0 && map[i][default_booth] != 0) {
                    default_booth--;
                    //printf("%d\n",default_booth);
                }
                found = (default_booth >= 0);
            }
        }
        }
    }
    // Step 3: Register booth
    if (map[i][default_booth] == 0) {
        map[i][default_booth] = 1; // Mark as registered
        *out_booth = default_booth;//+ 200;
        // Update enterpriseArray with new entry
        for (int i = 0; i < MAX_ENTERPRISE; i++) {
             if (enterpriseArray[i].booth_index == -1) { // Find empty slot
                 strcpy(enterpriseArray[i].abbre, out_abbre);
                 enterpriseArray[i].itemValue = itemValue;
                 enterpriseArray[i].itemWeight = itemWeight;
                 enterpriseArray[i].booth_index = default_booth;
                 break;
             }
        }
    } else {
        *out_booth = -1;//default_booth; //+ 100; // Unsuccessful registration
    }
    //printf("%d\n",i);
    //printf("%d\n",*out_booth);
}
// Initialize the booth map with default status Empty
void initMap(int map[MAX_ROW][MAX_COLUMN])
{
      for (int i = 0; i < MAX_ROW; i++)
      {
            for (int j = 0; j < MAX_COLUMN; j++)
            {
                  map[i][j] = Empty;
            }
      }
}
// Initialize the enterprise array with default values
void initEnterpriseArray(Enterprise enterpriseArray[MAX_ENTERPRISE])
{
      for (int i = 0; i < MAX_ENTERPRISE; i++)
      {
            enterpriseArray[i].booth_index = -1;
            strcpy(enterpriseArray[i].name, "");
            strcpy(enterpriseArray[i].abbre, "");
            enterpriseArray[i].itemValue = 0;
            enterpriseArray[i].itemWeight = 0;
      }
}
int main()
{
      // Initialize the map and enterprise array
      int map[MAX_ROW][MAX_COLUMN];
      Enterprise enterpriseArray[MAX_ENTERPRISE];
      initMap(map);
      initEnterpriseArray(enterpriseArray);
     int booth;
     char abbrev[10];
     int totalEmpty = 0;
      // Test Case 1: Register enterprises with automatic booth assignment
      registerEnterprise(map, enterpriseArray, "Tech Co",-1,10,10, &booth, abbrev);
      printf("Registered 'Tech Co' at booth %d with abbreviation %s\n", booth,
abbrev);
      printf("%d\n",booth);
      //printf("%d\n",map[0][10]);
      // Test Case 5: Register an enterprise to an already occupied booth
      registerEnterprise(map, enterpriseArray, "CompeteX", 10,10,2, &booth,
abbrev);
      if (booth == -1)
      {
            printf("\nBooth 10 is already occupied. Registration failed for
'CompeteX'.\n");
      }
      printf("%d\n",booth);
      printf("%d\n",map[0][10]);
}