package com.itsol.quantrivanphong.manage.timesheet.business;

import com.itsol.quantrivanphong.manage.leaveform.repository.EmployeeRepository;
import com.itsol.quantrivanphong.manage.timesheet.dto.TimeSheetDTO;
import com.itsol.quantrivanphong.manage.timesheet.repository.EProjectRepository;
import com.itsol.quantrivanphong.manage.timesheet.repository.TimeSheetRepository;
import com.itsol.quantrivanphong.model.Employee;
import com.itsol.quantrivanphong.model.Eproject;
import com.itsol.quantrivanphong.model.TimeSheet;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class TimeSheetBusiness {
    @Autowired
    TimeSheetRepository timeSheetRepository;
    @Autowired
    EProjectRepository eProjectRepository;
    @Autowired
    EmployeeRepository employeeRepository;
    public String insertTimeSheet(TimeSheetDTO timeSheetDTO) {
        String message;
        Eproject eproject = eProjectRepository.findEprojectById(timeSheetDTO.getEproject_id());
        if (eproject != null) {
            TimeSheet timeSheet = timeSheetRepository.save(TimeSheet.builder()
                    .title(timeSheetDTO.getTitle())
                    .content(timeSheetDTO.getContent())
                    .note(timeSheetDTO.getNote())
                    .eproject(eproject)
                    .status(timeSheetDTO.isStatus())
                    .checked(timeSheetDTO.getChecked())
                    .build());
            if (timeSheet != null) {
                message = "Insert Time Sheet success";
            } else {
                message = "Insert Time Sheet failed";
            }
        } else {
            message = "Eproject does not exist";
        }
        return message;
    }

    public String deleteTimeSheet(int id) {
        String message;
        TimeSheet timeSheet = timeSheetRepository.findTimeSheetById(id);
        if (timeSheet != null) {
            timeSheetRepository.delete(timeSheet);
            if (timeSheetRepository.findTimeSheetById(id) == null) {
                message = "Delete success";
            } else {
                message = "Delete failed";
            }
        } else {
            message = "Time Sheet does not exist";
        }
        return message;
    }

    public List<TimeSheet> findTimeSheetList() {
        return timeSheetRepository.timeSheetList();
    }

    @Transactional
    public String updateTimeSheet(TimeSheetDTO timeSheetDTO) {
        String message;
        TimeSheet timeSheet = timeSheetRepository.findTimeSheetById(timeSheetDTO.getId());
        Eproject eproject = eProjectRepository.findEprojectById(timeSheetDTO.getEproject_id());
        if (timeSheet != null) {
            if (eproject != null) {
                int i = timeSheetRepository.updateTimeSheet(timeSheetDTO.getTitle(), timeSheetDTO.getContent()
                        , timeSheetDTO.getNote(), eproject, timeSheetDTO.getId());
                if (i == 1) {
                    message = "Update success";
                } else {
                    message = "Update failed";
                }
            } else {
                message = "Eproject does not exist";
            }
        } else {
            message = "TimeSheet does not exist";
        }
        return message;
    }

    @Transactional
    public String updateLeaveFormStatus(TimeSheetDTO timeSheetDTO) {
        String message;
        TimeSheet timeSheet = timeSheetRepository.findTimeSheetById(timeSheetDTO.getId());
        if (timeSheet != null) {
            int i = timeSheetRepository.updateTimeSheetStatus(timeSheetDTO.isStatus(), timeSheetDTO.getId());
            if (i == 1) {
                message = "Update status success";
            } else {
                message = "Update status failed";
            }
        } else {
            message = "TimeSheet does not exist";
        }

        return message;
    }

    public List<TimeSheet> findTimeSheetById(int id) {
        Employee employee = employeeRepository.findEmployeeById(id);
        Eproject eproject = eProjectRepository.findEprojectByEmployee(employee);
        if (employee != null && eproject != null && timeSheetRepository.timeSheetListById(eproject) != null) {
            return timeSheetRepository.timeSheetListById(eproject);
        } else {
            return null;
        }
    }
}
