package com.nisum.myteam.controller;


import com.nisum.myteam.exception.handler.MyTeamException;
import com.nisum.myteam.model.dao.EffectiveLoginData;
import com.nisum.myteam.model.dao.Employee;
import com.nisum.myteam.service.IEffectiveLoginTimeService;
import com.nisum.myteam.service.IEmployeeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@RestController
@Slf4j
public class EffectiveLoginTimeController {

    @Autowired
    IEffectiveLoginTimeService effectiveLoginTimeService;

    @Autowired
    private IEmployeeService employeeService;

    @GetMapping("/updateLogins")
    public List<EffectiveLoginData> getLoginData() throws MyTeamException {
        List<EffectiveLoginData> loginData = new ArrayList<>();
        List<Employee> allEmployees = employeeService.getActiveEmployees();
        for(Employee employee:allEmployees){
            EffectiveLoginData effectiveLoginData =
                    effectiveLoginTimeService.calculateEffectiveLoginTimeForEmpAndSave(employee);
            loginData.add(effectiveLoginData);
        }
        return loginData;
    }
    @GetMapping("/effectiveLogin")
    public Map<String,Object> getAllEmployeesLoginData(
            @RequestParam("employeeId") long employeeId,
            @RequestParam("fromDate") @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) Date fromDate,
            @RequestParam("toDate") @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) Date toDate) throws ParseException {

        return effectiveLoginTimeService.getEmployeesEffLoginData(employeeId,fromDate,toDate);
    }
}
