//********************************************************
//
// Assignment 9 - Linked Lists
//
// Name: John Nguyen
//
// Class: C Programming, Fall 2024
//
// Date: November 17, 2024
//
// Description: Program calculates state tax, federal tax,
// gross pay, net pay, and other statistics for employees
// using dynamically allocated linked lists.
//
//********************************************************

#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>

#define STD_HOURS 40.0
#define OT_RATE 1.5
#define MA_TAX_RATE 0.05
#define NH_TAX_RATE 0.0
#define VT_TAX_RATE 0.06
#define CA_TAX_RATE 0.07
#define DEFAULT_TAX_RATE 0.08
#define FED_TAX_RATE 0.25
#define FIRST_NAME_SIZE 10
#define LAST_NAME_SIZE 10

struct name {
    char firstName[FIRST_NAME_SIZE];
    char lastName[LAST_NAME_SIZE];
};

struct employee {
    struct name empName;
    char taxState[3];
    long int clockNumber;
    float wageRate;
    float hours;
    float overtimeHrs;
    float grossPay;
    float stateTax;
    float fedTax;
    float netPay;
    struct employee *next;
};

struct totals {
    float total_wageRate;
    float total_hours;
    float total_overtimeHrs;
    float total_grossPay;
    float total_stateTax;
    float total_fedTax;
    float total_netPay;
};

struct min_max {
    float min_wageRate, max_wageRate;
    float min_hours, max_hours;
    float min_overtimeHrs, max_overtimeHrs;
    float min_grossPay, max_grossPay;
    float min_stateTax, max_stateTax;
    float min_fedTax, max_fedTax;
    float min_netPay, max_netPay;
};

struct employee *getEmpData(void);
void calcOvertimeHrs(struct employee *head_ptr);
void calcGrossPay(struct employee *head_ptr);
void calcStateTax(struct employee *head_ptr);
void calcFedTax(struct employee *head_ptr);
void calcNetPay(struct employee *head_ptr);
void calcEmployeeTotals(struct employee *head_ptr, struct totals *emp_totals_ptr);
void calcEmployeeMinMax(struct employee *head_ptr, struct min_max *emp_minMax_ptr);
void printHeader(void);
void printEmp(struct employee *head_ptr);
void printEmpStatistics(struct totals *emp_totals_ptr, struct min_max *emp_minMax_ptr, int theSize);

int main() {
    struct employee *head_ptr = getEmpData();
    struct totals employeeTotals = {0, 0, 0, 0, 0, 0, 0};
    struct min_max employeeMinMax = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
    int theSize = 0;

    struct employee *temp_ptr = head_ptr;
    while (temp_ptr) {
        theSize++;
        temp_ptr = temp_ptr->next;
    }

    if (theSize == 0) {
        printf("\n\n**** No employee data entered. Program exiting. ****\n");
        return 0;
    }

    calcOvertimeHrs(head_ptr);
    calcGrossPay(head_ptr);
    calcStateTax(head_ptr);
    calcFedTax(head_ptr);
    calcNetPay(head_ptr);

    calcEmployeeTotals(head_ptr, &employeeTotals);
    calcEmployeeMinMax(head_ptr, &employeeMinMax);

    printHeader();
    printEmp(head_ptr);
    printEmpStatistics(&employeeTotals, &employeeMinMax, theSize);

    printf("\n\n *** End of Program *** \n");
    return 0;
}

struct employee *getEmpData(void) {
    struct employee *head_ptr = NULL, *current_ptr = NULL;
    char answer[10];

    while (1) {
        struct employee *new_node = malloc(sizeof(struct employee));
        if (!new_node) {
            printf("Memory allocation failed!\n");
            exit(1);
        }

        printf("\nEnter employee first name: ");
        scanf("%s", new_node->empName.firstName);
        printf("Enter employee last name: ");
        scanf("%s", new_node->empName.lastName);
        printf("Enter employee two-character tax state: ");
        scanf("%s", new_node->taxState);
        printf("Enter employee clock number: ");
        scanf("%ld", &new_node->clockNumber);
        printf("Enter employee hourly wage: ");
        scanf("%f", &new_node->wageRate);
        printf("Enter hours worked this week: ");
        scanf("%f", &new_node->hours);

        new_node->next = NULL;

        if (!head_ptr) {
            head_ptr = new_node;
        } else {
            current_ptr->next = new_node;
        }
        current_ptr = new_node;

        printf("Add another employee? (y/n): ");
        scanf("%s", answer);
        if (tolower(answer[0]) != 'y') {
            break;
        }
    }

    return head_ptr;
}

void calcOvertimeHrs(struct employee *head_ptr) {
    for (struct employee *current_ptr = head_ptr; current_ptr; current_ptr = current_ptr->next) {
        current_ptr->overtimeHrs = (current_ptr->hours > STD_HOURS) ? current_ptr->hours - STD_HOURS : 0;
    }
}

void calcGrossPay(struct employee *head_ptr) {
    for (struct employee *current_ptr = head_ptr; current_ptr; current_ptr = current_ptr->next) {
        float regularPay = (current_ptr->hours - current_ptr->overtimeHrs) * current_ptr->wageRate;
        float overtimePay = current_ptr->overtimeHrs * OT_RATE * current_ptr->wageRate;
        current_ptr->grossPay = regularPay + overtimePay;
    }
}

void calcStateTax(struct employee *head_ptr) {
    for (struct employee *current_ptr = head_ptr; current_ptr; current_ptr = current_ptr->next) {
        if (strcmp(current_ptr->taxState, "MA") == 0)
            current_ptr->stateTax = MA_TAX_RATE * current_ptr->grossPay;
        else if (strcmp(current_ptr->taxState, "NH") == 0)
            current_ptr->stateTax = NH_TAX_RATE * current_ptr->grossPay;
        else if (strcmp(current_ptr->taxState, "VT") == 0)
            current_ptr->stateTax = VT_TAX_RATE * current_ptr->grossPay;
        else if (strcmp(current_ptr->taxState, "CA") == 0)
            current_ptr->stateTax = CA_TAX_RATE * current_ptr->grossPay;
        else
            current_ptr->stateTax = DEFAULT_TAX_RATE * current_ptr->grossPay;
    }
}

void calcFedTax(struct employee *head_ptr) {
    for (struct employee *current_ptr = head_ptr; current_ptr; current_ptr = current_ptr->next) {
        current_ptr->fedTax = FED_TAX_RATE * current_ptr->grossPay;
    }
}

void calcNetPay(struct employee *head_ptr) {
    for (struct employee *current_ptr = head_ptr; current_ptr; current_ptr = current_ptr->next) {
        current_ptr->netPay = current_ptr->grossPay - (current_ptr->stateTax + current_ptr->fedTax);
    }
}

void calcEmployeeTotals(struct employee *head_ptr, struct totals *emp_totals_ptr) {
    for (struct employee *current_ptr = head_ptr; current_ptr; current_ptr = current_ptr->next) {
        emp_totals_ptr->total_wageRate += current_ptr->wageRate;
        emp_totals_ptr->total_hours += current_ptr->hours;
        emp_totals_ptr->total_overtimeHrs += current_ptr->overtimeHrs;
        emp_totals_ptr->total_grossPay += current_ptr->grossPay;
        emp_totals_ptr->total_stateTax += current_ptr->stateTax;
        emp_totals_ptr->total_fedTax += current_ptr->fedTax;
        emp_totals_ptr->total_netPay += current_ptr->netPay;
    }
}

void calcEmployeeMinMax(struct employee *head_ptr, struct min_max *emp_minMax_ptr) {
    struct employee *current_ptr = head_ptr;

    emp_minMax_ptr->min_grossPay = emp_minMax_ptr->max_grossPay = current_ptr->grossPay;
    emp_minMax_ptr->min_stateTax = emp_minMax_ptr->max_stateTax = current_ptr->stateTax;
    emp_minMax_ptr->min_fedTax = emp_minMax_ptr->max_fedTax = current_ptr->fedTax;
    emp_minMax_ptr->min_netPay = emp_minMax_ptr->max_netPay = current_ptr->netPay;

    current_ptr = current_ptr->next;

    while (current_ptr) {
        if (current_ptr->grossPay < emp_minMax_ptr->min_grossPay)
            emp_minMax_ptr->min_grossPay = current_ptr->grossPay;
        if (current_ptr->grossPay > emp_minMax_ptr->max_grossPay)
            emp_minMax_ptr->max_grossPay = current_ptr->grossPay;

        if (current_ptr->stateTax < emp_minMax_ptr->min_stateTax)
            emp_minMax_ptr->min_stateTax = current_ptr->stateTax;
        if (current_ptr->stateTax > emp_minMax_ptr->max_stateTax)
            emp_minMax_ptr->max_stateTax = current_ptr->stateTax;

        if (current_ptr->fedTax < emp_minMax_ptr->min_fedTax)
            emp_minMax_ptr->min_fedTax = current_ptr->fedTax;
        if (current_ptr->fedTax > emp_minMax_ptr->max_fedTax)
            emp_minMax_ptr->max_fedTax = current_ptr->fedTax;

        if (current_ptr->netPay < emp_minMax_ptr->min_netPay)
            emp_minMax_ptr->min_netPay = current_ptr->netPay;
        if (current_ptr->netPay > emp_minMax_ptr->max_netPay)
            emp_minMax_ptr->max_netPay = current_ptr->netPay;

        current_ptr = current_ptr->next;
    }
}

void printHeader(void) {
    printf("\n\n*** Pay Calculator ***\n");
    printf("\n--------------------------------------------------------------");
    printf("-------------------");
    printf("\nName                Tax  Clock# Wage   Hours  OT   Gross ");
    printf("  State  Fed      Net");
    printf("\n                   State                           Pay   ");
    printf("  Tax    Tax      Pay");
    printf("\n--------------------------------------------------------------");
    printf("-------------------");
}

void printEmp(struct employee *head_ptr) {
    for (struct employee *current_ptr = head_ptr; current_ptr; current_ptr = current_ptr->next) {
        printf("\n%-20.20s %-2.2s  %06li %5.2f  %4.1f  %4.1f %7.2f %6.2f %7.2f %8.2f",
               current_ptr->empName.firstName, current_ptr->taxState, current_ptr->clockNumber,
               current_ptr->wageRate, current_ptr->hours,
               current_ptr->overtimeHrs, current_ptr->grossPay,
               current_ptr->stateTax, current_ptr->fedTax,
               current_ptr->netPay);
    }
}

void printEmpStatistics(struct totals *emp_totals_ptr, struct min_max *emp_minMax_ptr, int theSize) {
    printf("\n--------------------------------------------------------------");
    printf("-------------------");
    printf("\nTotals:                         %5.2f %5.1f %5.1f %7.2f %6.2f %7.2f %8.2f",
           emp_totals_ptr->total_wageRate,
           emp_totals_ptr->total_hours,
           emp_totals_ptr->total_overtimeHrs,
           emp_totals_ptr->total_grossPay,
           emp_totals_ptr->total_stateTax,
           emp_totals_ptr->total_fedTax,
           emp_totals_ptr->total_netPay);

    if (theSize > 0) {
        printf("\nAverages:                       %5.2f %5.1f %5.1f %7.2f %6.2f %7.2f %8.2f",
               emp_totals_ptr->total_wageRate / theSize,
               emp_totals_ptr->total_hours / theSize,
               emp_totals_ptr->total_overtimeHrs / theSize,
               emp_totals_ptr->total_grossPay / theSize,
               emp_totals_ptr->total_stateTax / theSize,
               emp_totals_ptr->total_fedTax / theSize,
               emp_totals_ptr->total_netPay / theSize);
    }

    printf("\nMinimum:                        %5.2f %5.1f %5.1f %7.2f %6.2f %7.2f %8.2f",
           emp_minMax_ptr->min_grossPay, emp_minMax_ptr->min_stateTax,
           emp_minMax_ptr->min_fedTax, emp_minMax_ptr->min_netPay);

    printf("\nMaximum:                        %5.2f %5.1f %5.1f %7.2f %6.2f %7.2f %8.2f",
           emp_minMax_ptr->max_grossPay, emp_minMax_ptr->max_stateTax,
           emp_minMax_ptr->max_fedTax, emp_minMax_ptr->max_netPay);

    printf("\n\nThe total employees processed was: %i\n", theSize);
}
