#include <stdio.h>
#include <string.h>
#include <time.h>

struct Habit {
    char name[50];
    int streak;
    char last_date[20];
};

void getToday(char *buffer) {
    time_t t = time(NULL);
    struct tm tm = *localtime(&t);
    sprintf(buffer, "%02d-%02d-%04d", tm.tm_mday, tm.tm_mon + 1, tm.tm_year + 1900);
}

int isSameDate(char *d1, char *d2) {
    return strcmp(d1, d2) == 0;
}

int main() {
    struct Habit habits[50];
    int count = 0;
    FILE *fp;

    // Load existing habits
    fp = fopen("habits.txt", "r");
    if (fp != NULL) {
        while (fscanf(fp, "%[^,],%d,%s\n", habits[count].name, 
                      &habits[count].streak, habits[count].last_date) != EOF) {
            count++;
        }
        fclose(fp);
    }

    int choice;
    char today[20];
    getToday(today);

    while (1) {
        printf("\n===== SMART HABIT TRACKER =====\n");
        printf("1. Add New Habit\n");
        printf("2. Mark Habit Completed Today\n");
        printf("3. View All Habits\n");
        printf("4. Performance Analysis\n");
        printf("5. Save & Exit\n");
        printf("Enter Choice: ");
        scanf("%d", &choice);

        if (choice == 1) {
            printf("Enter habit name: ");
            scanf(" %[^\n]", habits[count].name);
            habits[count].streak = 0;
            strcpy(habits[count].last_date, "00-00-0000");
            count++;
            printf("Habit Added!\n");

        } else if (choice == 2) {
            for (int i = 0; i < count; i++)
                printf("%d. %s (streak: %d)\n", i + 1, habits[i].name, habits[i].streak);

            int h;
            printf("Select habit: ");
            scanf("%d", &h);
            h--;

            if (!isSameDate(habits[h].last_date, today)) {
                habits[h].streak++;
                strcpy(habits[h].last_date, today);
                printf("Marked Completed! New Streak = %d\n", habits[h].streak);
            } else {
                printf("Already marked today!\n");
            }

        } else if (choice == 3) {
            printf("\n--- HABITS LIST ---\n");
            for (int i = 0; i < count; i++) {
                printf("%s | Streak: %d | Last Done: %s\n",
                       habits[i].name, habits[i].streak, habits[i].last_date);
            }

        } else if (choice == 4) {
            if (count == 0) {
                printf("No habits available.\n");
                continue;
            }

            int best = 0, worst = 0;
            for (int i = 1; i < count; i++) {
                if (habits[i].streak > habits[best].streak) best = i;
                if (habits[i].streak < habits[worst].streak) worst = i;
            }

            printf("\n--- PERFORMANCE REPORT ---\n");
            printf("Best Habit: %s (Streak: %d)\n", habits[best].name, habits[best].streak);
            printf("Weakest Habit: %s (Streak: %d)\n", habits[worst].name, habits[worst].streak);
            printf("Total Habits: %d\n", count);

        } else if (choice == 5) {
            fp = fopen("habits.txt", "w");
            for (int i = 0; i < count; i++) {
                fprintf(fp, "%s,%d,%s\n", habits[i].name, habits[i].streak, habits[i].last_date);
            }
            fclose(fp);
            printf("Saved Successfully!\n");
            break;
        }
    }

    return 0;
}