#include <stdio.h>
#include <string.h>

int main() {
    char username[30];
    char discountpicked;

    int quantity[4] = {0};
    int itemSummary[4] = {0, 0, 0, 0}; 
    float prices[4] = {950.00, 399.00, 250.50, 89.50};

    float change = 0, cash = 0;
    float subtotal = 0, total = 0;
    float discount = 0, VATamount = 0;

    float totalEarnings = 0;
    int totalOrders = 0;
    int choice = 0;
    int exitChoice = 0;

    printf("You are currently logged out.\n");
    printf("Enter Username: ");
    scanf("%29s", username); 

    while (strcmp(username, "Admin") != 0) {
        printf("Wrong username! Try Again.\n");
        printf("Enter username: ");
        scanf("%29s", username);
    }

    printf("\n===== CS 101 CASHIER SIMULATOR =====\n\n");
    do {
        subtotal = 0;
        discount = 0;
        VATamount = 0;
        total = 0;
        cash = 0;
        change = 0;
        int currentTransItems = 0;
        
        for (int i = 0; i < 4; i++) quantity[i] = 0;

        printf("\n--- New Order ---\n");
        for (int i = 0; i < 4; i++) {
            printf("Item %d -> ", i + 1);

            while (scanf("%d", &quantity[i]) != 1) {
                printf("Invalid input! Numbers only.\n");
                scanf("%*s");
                printf("Item %d -> ", i + 1);
            }
            currentTransItems += quantity[i];
        }

        if (currentTransItems == 0) {
            printf("No items purchased. Transaction cancelled.\n");
            goto ASK_CHOICE; 
        }
        do {
            printf("Is Customer Senior/PWD? Y/N: ");
            scanf(" %c", &discountpicked);

            if (discountpicked != 'Y' && discountpicked != 'y' &&
                discountpicked != 'N' && discountpicked != 'n') {
                printf("Invalid input! Please enter Y or N.\n\n");
            }
        } while (discountpicked != 'Y' && discountpicked != 'y' &&
                 discountpicked != 'N' && discountpicked != 'n');
        for (int i = 0; i < 4; i++) {
            subtotal += prices[i] * quantity[i];
            itemSummary[i] += quantity[i];
        }

        if (discountpicked == 'Y' || discountpicked == 'y') {
            discount = subtotal * 0.12;
            VATamount = 0;
            total = subtotal - discount;
        } else {
            discount = 0;
            VATamount = subtotal - (subtotal / 1.12);
            total = subtotal;
        }

        printf("Total to pay: %.2f\n", total);
        do {
            printf("Customer gave: ");
            scanf("%f", &cash);
            
            if(cash < total) {
                printf("Insufficient cash! You need at least %.2f. Try again.\n", total);
            }
        } while (cash < total);

        change = cash - total;
        totalEarnings += total;
        totalOrders++;

        printf("\n======== RECEIPT ========\n");
        printf("EMTC1CT Business\n");
        printf("Lozol Taft Avenue.\n");
        printf("Cashier: %s\n\n", username);

        for (int i = 0; i < 4; i++) {
            if (quantity[i] > 0)
                printf("Item %d x %d = %.2f\n", i + 1, quantity[i], prices[i] * quantity[i]);
        }

        printf("\nSubtotal:         %.2f\n", subtotal);
        printf("Senior/PWD disc: -%.2f\n", discount);
        printf("VAT amount:       %.2f\n", VATamount);
        printf("-------------------------\n");
        printf("Items Purchased:  %d\n", currentTransItems);
        printf("Total:            %.2f\n", total);
        printf("Cash:             %.2f\n", cash);
        printf("Change:           %.2f\n", change);
        printf("=========================\n");

        ASK_CHOICE:
        printf("\n1 - Another transaction\n");
        printf("2 - Logout / End Shift\n");
        printf("Enter choice: ");
        scanf("%d", &choice);
        printf("=========================\n");

    } while (choice == 1);

    printf("\nClosing cash register manned by %s\n", username);
    printf("Total orders processed: %d\n", totalOrders);

    int grandTotalItems = itemSummary[0] + itemSummary[1] + itemSummary[2] + itemSummary[3];
    printf("Total items sold: %d\n", grandTotalItems);

    printf("Breakdown of items sold:\n");
    for (int i = 0; i < 4; i++) {
        printf("Item %d: %d pcs\n", i + 1, itemSummary[i]);
    }

    printf("Total earnings: %.2f\n", totalEarnings);
    printf("===============================\n");

    printf("1 - Login Again\n");
    printf("2 - Exit Program\n");
    printf("Enter choice: ");
    scanf("%d", &exitChoice);

    if (exitChoice == 1) {
        printf("\nRestart program to login again.\n");
    } else {
        printf("Program exiting.\n");
    }

    return 0;
}