import models.AppUser;
import models.Company;
import models.RoleInfo;
import models.contacts.FinancialInfo;
import play.jobs.Job;
import play.jobs.OnApplicationStart;
import util.BCrypt;

import java.util.List;

/**
 * Created by dom on 29/12/2018.
 */
@OnApplicationStart
public class Bootstrap extends Job {
    public void doJob() {
        List<Company> companies = Company.findAll();
        if (companies.isEmpty()) {
            Company comp = new Company();
            comp.title = "dimicro";
            comp.financialInfo = new FinancialInfo("", "", "Dimicro AE");
            comp.save();
            comp = new Company();
            comp.title = "Plus+";
            comp.financialInfo = new FinancialInfo("", "", "Plus+");
            comp.save();
        }
        else if (companies.size() == 1)
        {
            Company comp = new Company();
            comp = new Company();
            comp.title = "Plus+";
            comp.financialInfo = new FinancialInfo("", "", "Plus+");
            comp.save();
        }
        if (AppUser.all().fetch().isEmpty()) {
            AppUser user = new AppUser();
            user.userName = "lkotoula@gmail.com";
            user.roleInfo = new RoleInfo();
            user.firstName="LEONIDAS";
            user.lastName="KOTOULAS";
            user.contactInfo.email="ddomxj@gmail.com";
            user.roleInfo.roleName = "root";
            user.password = BCrypt.hashpw("testPassword", BCrypt.gensalt(4));
            user.company = Company.findById(1L);
            user.save();
        }

        Company company = Company.find("select c from Company c where c.title='Plus+' ").first();
        AppUser user = AppUser.find("select a from AppUser a where a.company is ?1", company).first();
        if(user == null)
        {
	        user = new AppUser();
            user.userName = "stella";
            user.roleInfo = new RoleInfo();
            user.firstName="ΣΤΕΛΛΑ";
            user.lastName="ΚΟΥΤΣΟΥΡΕΛΗ";
            user.contactInfo.email="stella@gmail.com";
            user.roleInfo.roleName = "admin";
            user.password = BCrypt.hashpw("testPassword", BCrypt.gensalt(4));
            user.company = company;
            user.save();

        }
//        else if(AppUser.all().fetch().size() == 1)
//        {
//            Company company = Company.find("select c from Company c where c.title='Plus+' ").first();
//            AppUser user = new AppUser();
//            user.userName = "stella";
//            user.roleInfo = new RoleInfo();
//            user.firstName="ΣΤΕΛΛΑ";
//            user.lastName="ΚΟΥΤΣΟΥΡΕΛΗ";
//            user.contactInfo.email="stella@gmail.com";
//            user.roleInfo.roleName = "admin";
//            user.password = BCrypt.hashpw("testPassword", BCrypt.gensalt(4));
//            user.company = company;
//            user.save();
//        }


    }
}
