#include <iostream>
#include <vector>
#include <string>
#include <fstream>
#include <sstream>
#include <windows.h>
using namespace std; //Bibliotēku savienošana

class User {    //User klasu izveidošana datu saglabašanai
public:
    string name, password;
};

class FinancialRecord {     //FinancialRecord klasu izveidošana datu saglabašanai
public:
    string id, date, description;
    double profit, loss;

    // Konstruktors
    FinancialRecord(string id, string date, string description, double profit, double loss) : id(id), date(date), description(description), profit(profit), loss(loss) {}
};


vector<string> split(string str, char delimiter) {      //Funkcija lai sadalītu informāciju txt filā
    vector<string> result;
    stringstream ss(str);
    string item;
    while (getline(ss, item, delimiter)) {
        result.push_back(item);
    }
    return result;
}

void register_user(vector<User>& users) {       //Funkcija lai registretu lietotaju
    cout << "\nRegistering a new user\n";
    string name, password;
    cout << "Enter new user name: ";
    cin >> name;
    cout << "Enter new user password: ";
    cin >> password;
    User user;
    user.name = name;
    user.password = password;
    users.push_back(user);
    cout << "\nNew user has been registered successfully.";
}

bool login_user(const vector<User>& users, string& username, string& password) {        //Funkcija lai iejiet izveidota lietotajā
    cout << "\nLogging in...\n";
    cout << "Enter username: ";
    cin >> username;
    cout << "Enter password: ";
    cin >> password;
    for (const auto& user : users) {
        if (user.name == username && user.password == password) {
            cout << "\nLogin successful!";
            return true;
        }
    }
    cout << "\nInvalid username or password.";
    return false;
}

void display_additional_menu() {        //Izvelne lietotajam
    cout << "\nAdditional Menu:\n";
    cout << "1. Add Record\n";
    cout << "2. Edit User\n";
    cout << "3. Delete User\n";
    cout << "4. Edit Record\n";
    cout << "5. Delete Record\n";
    cout << "6. Output Analytics\n";
    cout << "7. Save Records to File\n";
    cout << "8. User Enter Check\n";
    cout << "9. Draw Graph\n";
    cout << "10. Output Profit\n";
    cout << "11. Output Loss\n";
    cout << "12. Total Value\n";
    cout << "0. Exit\n";
}

void add_record(vector<FinancialRecord>& records) {     //Funkcija lai pievienotu ierakstu
    cout << "\nAdding a new financial record...\n";
    string id, date, description;
    double profit, loss;
    cout << "Enter ID: ";
    cin >> id;
    cout << "Enter date (YYYY-MM-DD): ";
    cin >> date;
    cout << "Enter description: ";
    cin.ignore(); // Notīrīt ievades buferi
    getline(cin, description);
    cout << "Enter amount: ";
    cin >> profit;
    cout << "Enter loss amount: ";
    cin >> loss;
    int next_id = records.empty() ? 1 : stoi(records.back().id) + 1;
    records.push_back(FinancialRecord(to_string(next_id), date, description, profit, loss));
}

void edit_user(vector<User>& users) {       //Funkcija lai samainitu lietotaja datus
    cout << "\nEditing user details...\n";
    string username, new_name, new_password;
    cout << "Enter username to edit: ";
    cin >> username;
    for (User& user : users) {
        if (user.name == username) {
            cout << "Enter new name: ";
            cin >> new_name;
            cout << "Enter new password: ";
            cin >> new_password;
            user.name = new_name;
            user.password = new_password;
            cout << "User details updated successfully.\n";
            return;
        }
    }
    cout << "User not found.\n";
}

void delete_user(vector<User>& users) {     //Funkcija lai izdsestu lietotaju
    cout << "\nDeleting a user...\n";
    string username;
    cout << "Enter username to delete: ";
    cin >> username;
    for (auto it = users.begin(); it != users.end(); ++it) {
        if (it->name == username) {
            users.erase(it);
            cout << "User deleted successfully.\n";
            return;
        }
    }
    cout << "User not found.\n";
}

void edit_record(vector<FinancialRecord>& records) {        //Funkcija lai samainitu datus par izrakstu
    cout << "\nEditing a financial record...\n";
    string record_id;
    cout << "Enter the ID of the record you want to edit: ";
    cin >> record_id;

    for (FinancialRecord& record : records) {
        if (record.id == record_id) {
            cout << "Enter new date (YYYY-MM-DD): ";
            cin >> record.date;
            cout << "Enter new description: ";
            cin.ignore(); // Notīrīt ievades buferi
            getline(cin, record.description);
            cout << "Enter new profit amount: ";
            cin >> record.profit;
            cout << "Enter new loss amount: ";
            cin >> record.loss;
            cout << "Record updated successfully.\n";
            return;
        }
    }
    cout << "Record not found.\n";
}

void delete_record(vector<FinancialRecord>& records) {      //Funkcija lai izdsestu izrakstu
    cout << "\nDeleting a financial record...\n";
    string record_id;
    cout << "Enter the ID of the record you want to delete: ";
    cin >> record_id;

    for (auto it = records.begin(); it != records.end(); ++it) {
        if (it->id == record_id) {
            records.erase(it);
            cout << "Record deleted successfully.\n";
            return;
        }
    }
    cout << "Record not found.\n";
}

void output_analytics(const vector<FinancialRecord>& records) {     //Funkcija lai izvadit analitiskus datus
    cout << "\nGenerating analytics data...\n";
    double total_profit = 0, total_loss = 0;
    for (const FinancialRecord& record : records) {
        total_profit += record.profit;
        total_loss += record.loss;
    }
    cout << "Total Revenue: $" << total_profit << endl;
    cout << "Total Loss: $" << total_loss << endl;
    cout << "Net Profit: $" << (total_profit - total_loss) << endl;
}

void save_records_to_file(const vector<FinancialRecord>& records) {     //Funkcija lai saglabatu izrakstus txt filā
    cout << "\nSaving records to a file...\n";
    ofstream outfile("financial_records.txt");
    if (outfile.is_open()) {
        for (const FinancialRecord& record : records) {
            outfile << record.id << "," << record.date << "," << record.description << "," << record.profit << "," << record.loss << endl;
        }
        outfile.close();
        cout << "Records saved to financial_records.txt successfully.\n";
    }
    else {
        cout << "Unable to open file to save records.\n";
    }
}

void user_enter_check() {
    // Funkcionalitāte lietotāja autentifikācijas pārbaudei
    cout << "\nChecking user authentication...";
}

void draw_graph() {
    // Grafiku zīmēšanas funkcionalitāte
    cout << "\nDrawing graphs...";
}

void total_value(const vector<FinancialRecord>& records) {   //Funkcija lai paradītu pilna darījumu summa 
    cout << "\nCalculating total value...\n";
    double total_profit = 0;
    for (const FinancialRecord& record : records) {
        total_profit += record.profit;
    }
    cout << "Total profit: $" << total_profit << endl;
}

void output_loss(const vector<FinancialRecord>& records) {     //Funkcija lai paradītu pilna darījumu summa 
    cout << "\nCalculating and displaying loss...\n";
    double total_loss = 0;
    for (const FinancialRecord& record : records) {
        total_loss += record.loss;
    }
    cout << "Total loss: $" << total_loss << endl;
}

void output_profit(const vector<FinancialRecord>& records) {        //Funkcija lai paradītu pilno peļņu
    cout << "\nCalculating and displaying profit...\n";
    double total_value = 0;
    for (const FinancialRecord& record : records) {
        total_value += record.profit - record.loss;
    }
    cout << "Total value: $" << total_value << endl;
}

int main() {

    vector<FinancialRecord> records;        //vektors kur saglabas izraksti (records)
    
    vector<User> users;
    int choice;
    string username, password;
    do {
        cout << "\nMain Menu:\n1. Register New User\n2. Login\n3. Exit\nEnter your choice: ";
        cin >> choice;
        switch (choice) {
        case 1:
            register_user(users);       //reģistrācija jauno lietotāju
            break;
        case 2:
            if (login_user(users, username, password)) {        //Iejiet jau izveidotā lietotajā
                int additional_choice;
                do {
                    display_additional_menu();
                    cout << "\nEnter your choice: ";
                    cin >> additional_choice;
                    switch (additional_choice) {
                    case 1:
                        add_record(records);
                        break;
                    case 2:
                        edit_user(users);
                        break;
                    case 3:
                        delete_user(users);
                        break;
                    case 4:
                        edit_record(records);
                        break;
                    case 5:
                        delete_record(records);
                        break;
                    case 6:
                        output_analytics(records);
                        break;
                    case 7:
                        save_records_to_file(records);
                        break;
                    case 8:
                        user_enter_check();
                        break;
                    case 9:
                        draw_graph();
                        break;
                    case 10:
                        output_profit(records);
                        break;
                    case 11:
                        output_loss(records);
                        break;
                    case 12:
                        total_value(records);
                        break;
                    case 0:
                        cout << "\nLogging out...\n";
                        break;
                    default:
                        cout << "\nInvalid choice. Please try again.\n";
                    }
                } while (additional_choice != 0);
            }
            break;
        case 3:
            cout << "\nExiting program...\n";       //exit programma
            break;
        default:
            cout << "\nInvalid choice. Please try again.\n";
        }
    } while (choice != 3);
    
    
    return 0;
}