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

import com.itsol.quantrivanphong.manage.leaveform.dto.LeaveFormDTO;
import com.itsol.quantrivanphong.manage.leaveform.repository.EmployeeRepository;
import com.itsol.quantrivanphong.manage.leaveform.repository.LeaveFormRepository;
import com.itsol.quantrivanphong.manage.leaveform.repository.LeaveTypeRepository;
import com.itsol.quantrivanphong.model.Employee;
import com.itsol.quantrivanphong.model.LeaveForm;
import com.itsol.quantrivanphong.model.LeaveType;
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> findAllLeaveForm() {
        return leaveFormRepository.findAllLeaveForm();
    }

    public String insertLeaveForm(LeaveFormDTO leaveFormDTO) {
        String message;

        Employee employee = employeeRepository.findEmployeeById(leaveFormDTO.getEmployee_id());
        LeaveType leaveType = leaveTypeRepository.findLeaveTypeById(leaveFormDTO.getLeave_type_id());

        if (employee != null) {
            if (leaveType != null) {
                LeaveForm leaveForm = leaveFormRepository.save(LeaveForm.builder()
                        .title(leaveFormDTO.getTitle())
                        .content(leaveFormDTO.getContent())
                        .status(leaveFormDTO.isStatus())
                        .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 idemp, int idleave) {
        String message;
        Employee employee = employeeRepository.findEmployeeById(idemp);
        LeaveForm leaveForm = leaveFormRepository.findLeaveFormByEmployeeAndId(employee, idleave);
        if (employee != null) {
            if (leaveForm != null) {
                leaveFormRepository.delete(leaveForm);
                if (leaveFormRepository.findLeaveFormByEmployeeAndId(employee, idleave) != null) {
                    message = "Delete failed!";
                } else {
                    message = "Delete success!";
                }
            } else {
                message = "Leave Form does not exist";
            }
        } else {
            message = "Employee does not exist";
        }
        return message;
    }

    @Transactional
    public String updateLeaveForm(LeaveFormDTO leaveFormDTO) {
        String message;
        LeaveType leaveType = leaveTypeRepository.findLeaveTypeById(leaveFormDTO.getLeave_type_id());
        LeaveForm leaveForm = leaveFormRepository.findLeaveFormById(leaveFormDTO.getId());
        if (leaveForm != null) {
            if (leaveType != null) {
                int i = leaveFormRepository.updateLeaveForm(leaveFormDTO.getTitle(), leaveFormDTO.getContent(), leaveType, 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(LeaveFormDTO leaveFormDTO) {
        String message;
        LeaveForm leaveForm = leaveFormRepository.findLeaveFormById(leaveFormDTO.getEmployee_id());
        if (leaveForm != null) {
            int i = leaveFormRepository.updateLeaveFormStatus(leaveFormDTO.isStatus(), leaveFormDTO.getId());
            if (i == 1) {
                message = "Update status success";
            } else {
                message = "Update status failed";
            }
        } else {
            message = "Leave Form does not exist";
        }

        return message;
    }

    public List<LeaveForm> leaveFormById(int id) {
        Employee employee = employeeRepository.findEmployeeById(id);
        if (employee != null) {
            if (leaveFormRepository.leaveFormListById(employee) != null) {
                return leaveFormRepository.leaveFormListById(employee);
            } else {
                return null;
            }
        } else {
            return null;
        }
    }
}
