// deployer Services implementation
// Copyright (C) 2024 Jean-Cloud
// GNU General Public License v3

#include <sstream>
#include <cstdio>
#include <cstdlib> 
#include <fstream>
#include <string>
#include "Services.h"
#include "BashManager.h"

//constructor
Services::Services()
{
    services=readServicesFromCSV();
}
//destructor
Services::~Services(){}

//public methods
vector<Service> Services::GetServices()const
{
    return services;
}

const Service* Services::FindByUsername(string aUsername) const
{
    //this method may disappear. Serves development purposes for now.
    for (const Service & service : services){
        if (service.GetUsername().compare(aUsername)==0){
                return &service;
        }
    }
    return nullptr;
}

const Service * Services::FindByID(int aUserID) const
{
    //this method may disappear. Serves development purposes for now.
    for (const Service & service : services){
        if (service.GetUserID()==aUserID){
            return &service;
        }
    }    
    return nullptr;
}

list<const Service*> Services::FindByServer(string aServer) const
{
    //this method may disappear. Serves development purposes for now.
    list<const Service*> result;
    for (const Service & service : services){
        for (string server : service.GetServers()){
            if(server.compare(aServer)==0){
                result.push_back(&service);
            }
        }
    }    
    return result;   
}

//private methods
vector <Service> Services::readServicesFromCSV () const 
{
    //this method extracts the list of uid|username|servers from the services.csv file 
    vector <Service> result;
    char * CSV = getenv("SERVICEFILE"); //service file is an environment variable
    // Check if the environment variable is set
    if (CSV==nullptr) {
        cerr << "Error when trying to read services.csv. Environment variable servicefiles is not set." << endl;
        return result;
    }
    ifstream streamServices(CSV); 
    if (!streamServices){
        cout << "Invalid services.csv file." << endl;
    }else{
        string line; 
        string tmpUserID; //used before converting to int
        int userID;
        string username;
        string server;
        list <string> servers;
        while(getline(streamServices,line)){
            servers.clear();
            tmpUserID="";
            username="";
            server="";
            if (line.empty() || line[0] == '#') { //not taking comments and empty lines into account
                continue; 
            }
            stringstream streamLine(line);
            getline(streamLine,tmpUserID,','); //extracting the userID
            userID=stoi(tmpUserID);
            getline(streamLine,username,','); //extracting the username
            while(getline(streamLine,server,';')){ //extracting the server(s)
                if(!server.empty()){
                servers.push_back(server);
                }
            }         
            Service entry = Service(userID,username,servers);
            result.push_back(entry);
        }
    }
    return result;
}

