#include <iostream>
#include <fstream>
#include<cstring>

using namespace std;

// Forward declarations
void homePage();
void adminMenu();
void displayCoursesForAdmin();
void manageCourses();
void addCourseForAdmin();
void removeCourseForAdmin();

// Custom string utility functions
int getStringLength(const char* str) {
    int length = 0;
    while (str[length] != '\0') {
        length++;
    }
    return length;
}

void copyString(char* dest, const char* src) {
    int i = 0;
    while (src[i] != '\0') {
        dest[i] = src[i];
        i++;
    }
    dest[i] = '\0';
}

void formatTeacherName(char* result, int instructorID) {
    // Format the teacher's name as "InstructorX"
    char instructorBase[] = "Instructor";
    int length = getStringLength(instructorBase);
    copyString(result, instructorBase);

    // Append the instructor ID
    int temp = instructorID, digits = 0;
    while (temp > 0) {
        digits++;
        temp /= 10;
    }

    for (int i = digits - 1; i >= 0; i--) {
        result[length + i] = (instructorID % 10) + '0';
        instructorID /= 10;
    }
    result[length + digits] = '\0';
}

class Course {
private:
    char courseName[100];
    char description[200];
    char teacherName[100]; // Teacher's name
    char students[5][100]; // List of students (assumed max 5 students for simplicity)
    int studentCount; // Number of students enrolled

public:
    Course(const char* name, const char* teacher, const char* desc = "")
    {
        copyString(courseName, name);
        copyString(teacherName, teacher);
        copyString(description, desc);
        studentCount = 0; // Initially no students enrolled
    }

    const char* getCourseName() const { return courseName; }
    const char* getDescription() const { return description; }
    const char* getTeacherName() const { return teacherName; }
    void setDescription(const char* desc) { copyString(description, desc); }

    void enrollStudent(const char* studentName) {
        if (studentCount < 5) {
            copyString(students[studentCount], studentName);
            studentCount++;
        }
        else {
            cout << "Course is full, cannot enroll more students." << endl;
        }
    }

    void displayCourse() const {
        cout << "Course Name: " << courseName << endl;
        cout << "Teacher: " << teacherName << endl;
        cout << "Description: " << description << endl;
        cout << "Enrolled Students: ";
        for (int i = 0; i < studentCount; i++) {
            cout << students[i] << (i == studentCount - 1 ? "" : ", ");
        }
        cout << endl;
    }
};

Course predefinedCourses[10] = {
    Course("Computer Basics", "Instructor1"),
    Course("Python Complete Course", "Instructor2"),
    Course("Web Development", "Instructor3"),
    Course("Data Structures and Algorithms", "Instructor4"),
    Course("Machine Learning", "Instructor5"),
    Course("Artificial Intelligence", "Instructor6"),
    Course("Database Management", "Instructor7"),
    Course("Networking Fundamentals", "Instructor8"),
    Course("Operating Systems", "Instructor9"),
    Course("Software Engineering", "Instructor10")
};

class User {
private:
    char name[100];
    char email[100];
    char password[100];
    char role[20];
    char additionalInfo[200];

public:
    User(const char* n, const char* e, const char* p, const char* r, const char* info = "")
    {
        copyString(name, n);
        copyString(email, e);
        copyString(password, p);
        copyString(role, r);
        copyString(additionalInfo, info);
    }

    const char* getName() const { return name; }
    const char* getEmail() const { return email; }
    const char* getPassword() const { return password; }
    const char* getRole() const { return role; }
    const char* getAdditionalInfo() const { return additionalInfo; }

    void displayUserInfo() const {
        cout << "Name: " << name << endl;
        cout << "Email: " << email << endl;
        cout << "Role: " << role << endl;
        if (getStringLength(additionalInfo) > 0) {
            cout << "Additional Info: " << additionalInfo << endl;
        }
    }

    void writeToFile(ofstream& outFile) const {
        outFile << name << "," << email << "," << password << "," << role;
        if (getStringLength(additionalInfo) > 0) {
            outFile << "," << additionalInfo;
        }
        outFile << endl;
    }

    static User* readFromFile(const char* line) {
        char name[100], email[100], password[100], role[20], additionalInfo[200];
        int i = 0;

        auto extractField = [](char* field, const char* line, int& i) {
            int j = 0;
            while (line[i] != ',' && line[i] != '\0') {
                field[j++] = line[i++];
            }
            field[j] = '\0';
            if (line[i] == ',') i++;
            };

        extractField(name, line, i);
        extractField(email, line, i);
        extractField(password, line, i);
        extractField(role, line, i);
        extractField(additionalInfo, line, i);

        return new User(name, email, password, role, additionalInfo);
    }
};

// Remaining code continues as in the original with no further changes


class UserManager {
private:
    User* users[100];
    int userCount = 0;
    const int maxUsers = 100;

public:
    UserManager() {
        ifstream inFile("users.txt");
        char line[300];
        while (inFile.getline(line, sizeof(line))) {
            if (line[0] != '\0') {
                users[userCount++] = User::readFromFile(line);
            }
        }
    }

    ~UserManager() {
        ofstream outFile("users.txt");
        for (int i = 0; i < userCount; i++) {
            users[i]->writeToFile(outFile);
        }
    }

    void addUser(User* user) {
        if (userCount < maxUsers) {
            users[userCount++] = user;
        }
        else {
            cout << "User capacity reached!" << endl;
        }
    }

    void displayAllUsers() const {
        cout << "===========================================" << endl;
        cout << "          All Registered Users             " << endl;
        cout << "===========================================" << endl;
        for (int i = 0; i < userCount; i++) {
            users[i]->displayUserInfo();
            cout << "-------------------------------------------" << endl;
        }
    }

    User* login(const char* email, const char* password) const {
        for (int i = 0; i < userCount; i++) {
            if (strcmp(users[i]->getEmail(), email) == 0 && strcmp(users[i]->getPassword(), password) == 0) {
                return users[i];
            }
        }
        return nullptr;
    }
};

void displayCoursesForAdmin() {
    cout << "===========================================" << endl;
    cout << "                 Courses                   " << endl;
    cout << "===========================================" << endl;
    for (int i = 0; i < 10; i++) {
        predefinedCourses[i].displayCourse();
        cout << "-------------------------------------------" << endl;
    }
}

void addCourseForAdmin() {
    char courseName[100], description[200];
    cout << "Enter new Course Name: ";
    cin.ignore();
    cin.getline(courseName, sizeof(courseName));
    cout << "Enter Course Description: ";
    cin.getline(description, sizeof(description));

    // Create new Teacher based on course name and description
    char teacherName[100];
    snprintf(teacherName, sizeof(teacherName), "Instructor%d", rand() % 100 + 1);  // Random Instructor ID

    Course newCourse(courseName, teacherName, description);
    predefinedCourses[9] = newCourse;  // Replace last course (simplification)

    cout << "Course added successfully!" << endl;
}

void removeCourseForAdmin() {
    int courseId;
    cout << "Enter Course ID to remove (1-10): ";
    cin >> courseId;

    if (courseId >= 1 && courseId <= 10) {
        for (int i = courseId - 1; i < 9; i++) {
            predefinedCourses[i] = predefinedCourses[i + 1]; // Shift courses left
        }
        cout << "Course removed successfully!" << endl;
    }
    else {
        cout << "Invalid Course ID. Please try again." << endl;
    }
}

void manageCourses() {
    while (true) {
        cout << "===========================================" << endl;
        cout << "          Manage Courses Menu              " << endl;
        cout << "===========================================" << endl;
        cout << "1. Add Course" << endl;
        cout << "2. Remove Course" << endl;
        cout << "3. Return to Admin Menu" << endl;
        int choice;
        cout << "Enter your choice: ";
        cin >> choice;
        switch (choice) {
        case 1:
            addCourseForAdmin();
            break;
        case 2:
            removeCourseForAdmin();
            break;
        case 3:
            adminMenu();
            break;
        default:
            cout << "Invalid choice. Please try again." << endl;
        }
    }
}

void adminMenu() {
    while (true) {
        cout << "===========================================" << endl;
        cout << "              Admin Menu                   " << endl;
        cout << "===========================================" << endl;
        cout << "1. View Courses" << endl;
        cout << "2. Manage Courses" << endl;
        cout << "3. Return to Home Page" << endl;
        int choice;
        cout << "Enter your choice: ";
        cin >> choice;

        switch (choice) {
        case 1:
            displayCoursesForAdmin();
            break;
        case 2:
            manageCourses();
            break;
        case 3:
            homePage();  // Return to Home Page
            break;
        default:
            cout << "Invalid choice. Please try again." << endl;
        }
    }
}

void homePage() {
    cout << "===========================================" << endl;
    cout << "               Welcome to ParhoPK           " << endl;
    cout << "===========================================" << endl;
    cout << "1. Admin Login" << endl;
    cout << "2. Exit" << endl;

    int choice;
    cout << "Enter your choice: ";
    cin >> choice;

    switch (choice) {
    case 1:
    {
        char email[100], password[100];
        cout << "Enter Email: ";
        cin >> email;
        cout << "Enter Password: ";
        cin >> password;

        UserManager userManager;
        User* user = userManager.login(email, password);
        if (user && strcmp(user->getRole(), "Admin") == 0) {
            adminMenu();
        }
        else {
            cout << "Invalid credentials or not an Admin. Please try again." << endl;
        }
        break;
    }
    case 2:
        cout << "Exiting program..." << endl;
        exit(0);  // Exit the program
    default:
        cout << "Invalid choice. Please try again." << endl;
    }
}

int main() {
    homePage();
    return 0;
}//in this code , add a class of students and instructors so that they can work using text file handling , here is the text file                                                                                                                                                                                     # Format: ID, Name, Email, Password, Role, Additional Information
# Admins
ad1, Admin1, <PRESIDIO_ANONYMIZED_EMAIL_ADDRESS>, admin123, Admin
ad2, Admin2, <PRESIDIO_ANONYMIZED_EMAIL_ADDRESS>, admin456, Admin

# Instructors
ins1, Instructor1, <PRESIDIO_ANONYMIZED_EMAIL_ADDRESS>, instr123, Instructor, Computer Basics
ins2, Instructor2, <PRESIDIO_ANONYMIZED_EMAIL_ADDRESS>, instr456, Instructor, Python Complete Course
ins3, Instructor3, <PRESIDIO_ANONYMIZED_EMAIL_ADDRESS>, instr789, Instructor, Web Development
ins4, Instructor4, <PRESIDIO_ANONYMIZED_EMAIL_ADDRESS>, instr147, Instructor, Data Science
ins5, Instructor5, <PRESIDIO_ANONYMIZED_EMAIL_ADDRESS>, instr258, Instructor, Artificial Intelligence
ins6, Instructor6, <PRESIDIO_ANONYMIZED_EMAIL_ADDRESS>, instr369, Instructor, Cybersecurity
ins7, Instructor7, <PRESIDIO_ANONYMIZED_EMAIL_ADDRESS>, instr321, Instructor, Cloud Computing
ins8, Instructor8, <PRESIDIO_ANONYMIZED_EMAIL_ADDRESS>, instr654, Instructor, Blockchain Development
ins9, Instructor9, <PRESIDIO_ANONYMIZED_EMAIL_ADDRESS>, instr987, Instructor, Software Testing
ins10, Instructor10, <PRESIDIO_ANONYMIZED_EMAIL_ADDRESS>, instr741, Instructor, Mobile App Development

# Students
stu1, Student1, <PRESIDIO_ANONYMIZED_EMAIL_ADDRESS>, stud123, Student
stu2, Student2, <PRESIDIO_ANONYMIZED_EMAIL_ADDRESS>, stud456, Student
stu3, Student3, <PRESIDIO_ANONYMIZED_EMAIL_ADDRESS>, stud789, Student
stu4, Student4, <PRESIDIO_ANONYMIZED_EMAIL_ADDRESS>, stud147, Student
stu5, Student5, <PRESIDIO_ANONYMIZED_EMAIL_ADDRESS>, stud258, Student
stu6, Student6, <PRESIDIO_ANONYMIZED_EMAIL_ADDRESS>, stud369, Student
stu7, Student7, <PRESIDIO_ANONYMIZED_EMAIL_ADDRESS>, stud321, Student
stu8, Student8, <PRESIDIO_ANONYMIZED_EMAIL_ADDRESS>, stud654, Student
stu9, Student9, <PRESIDIO_ANONYMIZED_EMAIL_ADDRESS>, stud987, Student
stu10, Student10, <PRESIDIO_ANONYMIZED_EMAIL_ADDRESS>, stud741, Student
stu11, Student11, <PRESIDIO_ANONYMIZED_EMAIL_ADDRESS>, stud852, Student
stu12, Student12, <PRESIDIO_ANONYMIZED_EMAIL_ADDRESS>, stud963, Student
stu13, Student13, <PRESIDIO_ANONYMIZED_EMAIL_ADDRESS>, stud159, Student
stu14, Student14, <PRESIDIO_ANONYMIZED_EMAIL_ADDRESS>, stud753, Student
stu15, Student15, <PRESIDIO_ANONYMIZED_EMAIL_ADDRESS>, stud951, Student
stu16, Student16, <PRESIDIO_ANONYMIZED_EMAIL_ADDRESS>, stud357, Student
stu17, Student17, <PRESIDIO_ANONYMIZED_EMAIL_ADDRESS>, stud951, Student
stu18, Student18, <PRESIDIO_ANONYMIZED_EMAIL_ADDRESS>, stud753, Student
stu19, Student19, <PRESIDIO_ANONYMIZED_EMAIL_ADDRESS>, stud852, Student
stu20, Student20, <PRESIDIO_ANONYMIZED_EMAIL_ADDRESS>, stud654, Student
stu21, Student21, <PRESIDIO_ANONYMIZED_EMAIL_ADDRESS>, stud951, Student
stu22, Student22, <PRESIDIO_ANONYMIZED_EMAIL_ADDRESS>, stud369, Student
stu23, Student23, <PRESIDIO_ANONYMIZED_EMAIL_ADDRESS>, stud159, Student
stu24, Student24, <PRESIDIO_ANONYMIZED_EMAIL_ADDRESS>, stud258, Student
stu25, Student25, <PRESIDIO_ANONYMIZED_EMAIL_ADDRESS>, stud147, Student
stu26, Student26, <PRESIDIO_ANONYMIZED_EMAIL_ADDRESS>, stud963, Student
stu27, Student27, <PRESIDIO_ANONYMIZED_EMAIL_ADDRESS>, stud741, Student
stu28, Student28, <PRESIDIO_ANONYMIZED_EMAIL_ADDRESS>, stud753, Student
stu29, Student29, <PRESIDIO_ANONYMIZED_EMAIL_ADDRESS>, stud321, Student
stu30, Student30, <PRESIDIO_ANONYMIZED_EMAIL_ADDRESS>, stud147, Student
stu31, Student31, <PRESIDIO_ANONYMIZED_EMAIL_ADDRESS>, stud951, Student
stu32, Student32, <PRESIDIO_ANONYMIZED_EMAIL_ADDRESS>, stud369, Student
stu33, Student33, <PRESIDIO_ANONYMIZED_EMAIL_ADDRESS>, stud753, Student
stu34, Student34, <PRESIDIO_ANONYMIZED_EMAIL_ADDRESS>, stud852, Student
stu35, Student35, <PRESIDIO_ANONYMIZED_EMAIL_ADDRESS>, stud951, Student
stu36, Student36, <PRESIDIO_ANONYMIZED_EMAIL_ADDRESS>, stud321, Student
stu37, Student37, <PRESIDIO_ANONYMIZED_EMAIL_ADDRESS>, stud159, Student
stu38, Student38, <PRESIDIO_ANONYMIZED_EMAIL_ADDRESS>, stud654, Student
stu39, Student39, <PRESIDIO_ANONYMIZED_EMAIL_ADDRESS>, stud852, Student
stu40, Student40, <PRESIDIO_ANONYMIZED_EMAIL_ADDRESS>, stud456, Student
stu41, Student41, <PRESIDIO_ANONYMIZED_EMAIL_ADDRESS>, stud963, Student
stu42, Student42, <PRESIDIO_ANONYMIZED_EMAIL_ADDRESS>, stud147, Student
stu43, Student43, <PRESIDIO_ANONYMIZED_EMAIL_ADDRESS>, stud753, Student
stu44, Student44, <PRESIDIO_ANONYMIZED_EMAIL_ADDRESS>, stud951, Student
stu45, Student45, <PRESIDIO_ANONYMIZED_EMAIL_ADDRESS>, stud369, Student
stu46, Student46, <PRESIDIO_ANONYMIZED_EMAIL_ADDRESS>, stud852, Student
stu47, Student47, <PRESIDIO_ANONYMIZED_EMAIL_ADDRESS>, stud654, Student
stu48, Student48, <PRESIDIO_ANONYMIZED_EMAIL_ADDRESS>, stud753, Student
stu49, Student49, <PRESIDIO_ANONYMIZED_EMAIL_ADDRESS>, stud159, Student
stu50, Student50, <PRESIDIO_ANONYMIZED_EMAIL_ADDRESS>, stud456, Student