package com.itsol.quantrivanphong.access.login.bussiness;

import com.itsol.quantrivanphong.access.login.repository.EmployeeRepositorys;
import com.itsol.quantrivanphong.access.login.repository.EmployeeRoleRepository;
import com.itsol.quantrivanphong.access.login.repository.LoginRepositoy;
import com.itsol.quantrivanphong.model.Employee;
import com.itsol.quantrivanphong.model.EmployeeRole;
import com.itsol.quantrivanphong.report.issue.common.WrapperResultObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class LoginBussunessImpl implements LoginBussiness {
    @Autowired
    private LoginRepositoy loginRepositoy;
    @Autowired
    private EmployeeRoleRepository employeeRoleRepository;
    @Autowired
    private EmployeeRepositorys employeeRepositorys;

    @Override
    public WrapperResultObject checkUserAndPass(String username, String password) {
        String views = "";
        int status = 113;
        List<String> lstRole = new ArrayList<String>();
        String Role = null;
        Employee login = loginRepositoy.findEmployeeByUsernameAndPassword(username, password);
        if (login != null) {
            Employee userId = employeeRepositorys.findEmployeeByUsername(username);
             lstRole = findRoleNameByUserId(userId.getId());
             Role = lstRole.get(0);
            status = 200;
            views = "Chúc mừng bạn dăng nhập thành công!";
        } else {
            views = "Mật khẩu hoặc pass không đúng!";
        }
        return new WrapperResultObject(status, views,lstRole,Role);
    }
    public List<String> findRoleNameByUserId(Integer userId) {
        List<String> lst = new ArrayList<>();
        List<EmployeeRole> lstUR = employeeRoleRepository.findByEmployeeId(userId);
        for (EmployeeRole employeeRole : lstUR) {
            lst.add(employeeRole.getRole().getRoleCode());
        }
        return lst;
    }
}
