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

import com.itsol.quantrivanphong.manager.employee.repository.EmployeeRepository;
import com.itsol.quantrivanphong.model.Employee;
import com.itsol.quantrivanphong.model.LeaveForm;
import com.itsol.quantrivanphong.model.LeaveType;
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 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 insertLeaveForm(int employee_Id, LeaveFormDTO leaveFormDTO, int leavetype_Id) {
        String message;

        Employee employee = employeeRepository.findEmployeeById(employee_Id);
        LeaveType leaveType = leaveTypeRepository.findLeaveTypeById(leavetype_Id);

        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 emloyee_Id, int leaveform_Id) {
        String message;
        Employee employee = employeeRepository.findEmployeeById(emloyee_Id);
        LeaveForm leaveForm = leaveFormRepository.findLeaveFormByEmployeeAndId(employee, leaveform_Id);
        if (employee != null) {
            if (leaveForm != null) {
                leaveFormRepository.delete(leaveForm);
                if (leaveFormRepository.findLeaveFormByEmployeeAndId(employee, leaveform_Id) != 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(int employee_Id, int leavetype_Id, int leaveform_Id, LeaveFormDTO leaveFormDTO) {
        String message;
        Employee employee = employeeRepository.findEmployeeById(employee_Id);
        LeaveType leaveType = leaveTypeRepository.findLeaveTypeById(leavetype_Id);
        LeaveForm leaveForm = leaveFormRepository.findLeaveFormByEmployeeAndLeaveTypeAndId(employee, leaveType, leaveform_Id);
        if (leaveType != null) {
            if (leaveForm != null) {
                int i = leaveFormRepository.updateLeaveForm(leaveFormDTO.getTitle(), leaveFormDTO.getContent(), leaveform_Id);
                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 employee_Id, int leavetype_Id, int leaveform_Id) {
        String message;
        Employee employee = employeeRepository.findEmployeeById(employee_Id);
        LeaveType leaveType = leaveTypeRepository.findLeaveTypeById(leavetype_Id);
        LeaveForm leaveForm = leaveFormRepository.findLeaveFormByEmployeeAndLeaveTypeAndId(employee, leaveType, leaveform_Id);
        if (leaveForm != null) {
            int i = leaveFormRepository.updateLeaveFormStatus(leaveform_Id);
            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 employee_Id) {
        Employee employee = employeeRepository.findEmployeeById(employee_Id);
        if (employee != null) {
            if (leaveFormRepository.showLeaveFormById(employee) != null) {
                return leaveFormRepository.showLeaveFormById(employee);
            } else {
                return null;
            }
        } else {
            return null;
        }
    }
}
