package com.itsol.quantrivanphong.report.leaveform.business;

import com.itsol.quantrivanphong.report.leaveform.dto.LeaveFormDTO;
import com.itsol.quantrivanphong.report.leaveform.repository.LeaveFormRepository;
import com.itsol.quantrivanphong.report.leaveform.repository.LeaveTypeRepository;
import com.itsol.quantrivanphong.model.Employee;
import com.itsol.quantrivanphong.model.LeaveForm;
import com.itsol.quantrivanphong.model.LeaveType;
import com.itsol.quantrivanphong.repository.EmployeeRepository;
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 LeaveFormBusiness {
    @Autowired
    LeaveFormRepository leaveFormRepository;
    @Autowired
    LeaveTypeRepository leaveTypeRepository;
    @Autowired
    EmployeeRepository employeeRepository;

    public List<LeaveForm> findAll() {
        return leaveFormRepository.findAll();
    }

    public String addLeaveForm(LeaveFormDTO leaveFormDTO) {
        String message;

        Employee employee = employeeRepository.findEmployeeById(leaveFormDTO.getEmployeeId());
        LeaveType leaveType = leaveTypeRepository.findLeaveTypeById(leaveFormDTO.getLeaveTypeId());

        if (employee != null) {
            if (leaveType != null) {
                LeaveForm leaveForm = leaveFormRepository.save(LeaveForm.builder()
                        .title(leaveFormDTO.getTitle())
                        .content(leaveFormDTO.getContent())
                        .status(false)
                        .employee(employee)
                        .leaveType(leaveType).build());

                if (leaveForm != null) {
                    message = "Insert leave form success";
                } else {
                    message = "Insert leave form failed";
                }
            } else {
                message = "Leave Type does not exist";
            }
        } else {
            message = "Employee does not exist";
        }
        return message;
    }

    @Transactional
    public String deleteLeaveForm(int leaveFormId) {
        String message;
        LeaveForm leaveForm = leaveFormRepository.findLeaveFormById(leaveFormId);

        if (leaveForm != null) {
            leaveFormRepository.delete(leaveForm);
            if (leaveFormRepository.findLeaveFormById(leaveFormId) != null) {
                message = "Delete failed!";
            } else {
                message = "Delete success!";
            }
        } else {
            message = "Leave Form does not exist";
        }
        return message;
    }

    @Transactional
    public String updateLeaveForm(LeaveFormDTO leaveFormDTO) {
        String message;
        Employee employee = employeeRepository.findEmployeeById(leaveFormDTO.getEmployeeId());
        LeaveType leaveType = leaveTypeRepository.findLeaveTypeById(leaveFormDTO.getLeaveTypeId());
        LeaveForm leaveForm = leaveFormRepository.findLeaveFormByEmployeeAndLeaveTypeAndId(employee, leaveType, leaveFormDTO.getId());
        if (leaveType != null) {
            if (leaveForm != null) {
                int i = leaveFormRepository.updateLeaveForm(leaveFormDTO.getTitle(), leaveFormDTO.getContent(), leaveFormDTO.getId());
                if (i == 1) {
                    message = "Update success";
                } else {
                    message = "Update failed";
                }
            } else {
                message = "Leave Type does not exist";
            }
        } else {
            message = "Leave Form does not exist";
        }

        return message;
    }

    @Transactional
    public String updateLeaveFormStatus(int leaveformId) {
        String message;
        LeaveForm leaveForm = leaveFormRepository.findLeaveFormById(leaveformId);
        if (leaveForm != null) {
            int i = leaveFormRepository.updateLeaveFormStatus(leaveformId);
            if (i == 1) {
                message = "Update status success";
            } else {
                message = "Update status failed";
            }
        } else {
            message = "Leave Form does not exist";
        }

        return message;
    }

    public List<LeaveForm> showLeaveFormByEmployeeId(int employeeId) {
        Employee employee = employeeRepository.findEmployeeById(employeeId);
        if (employee != null) {
            if (leaveFormRepository.showLeaveFormById(employee) != null) {
                return leaveFormRepository.showLeaveFormById(employee);
            } else {
                return null;
            }
        } else {
            return null;
        }
    }
}
