package com.example.springip.lab3.service;

import com.example.springip.lab3.dao.EmployeeRepository;
import com.example.springip.lab3.models.Company;
import com.example.springip.lab3.models.Employee;
import com.example.springip.lab3.models.Position;
import jakarta.persistence.EntityNotFoundException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.List;

@Service
public class EmployeeService {
    private EmployeeRepository employeeRepository;
    public EmployeeService(EmployeeRepository employeeRepository) {
        this.employeeRepository = employeeRepository;
    }

    @Transactional
    public Employee addEmployee(String surname, String name, String phoneNumber) {
        if (!StringUtils.hasText(name) ||!StringUtils.hasText(surname) || !StringUtils.hasText(phoneNumber)) {
            throw new IllegalArgumentException("Employee's data is null or empty");
        }
        final Employee employee = new Employee(surname, name, phoneNumber);
        employeeRepository.save(employee);
        return employee;
    }

    @Transactional
    public Employee findEmployee(Long id) {
        final Employee employee = employeeRepository.findById(id).orElse(null);
        if (employee == null) {
            throw new EntityNotFoundException(String.format("Employee with id [%s] is not found", id));
        }
        return employee;
    }

    @Transactional(readOnly = true)
    public List<Employee> findAllEmployees() {
        return employeeRepository.findAll();
    }
    @Transactional(readOnly = true)
    public List<Employee> findAllFreeEmployees() {
        return employeeRepository.findByCompanyIsNull();
    }

    @Transactional
    public Employee updateEmployee(Long id, String surname, String name, String phoneNumber) {
        if (!StringUtils.hasText(name) ||!StringUtils.hasText(surname) || !StringUtils.hasText(phoneNumber)) {
            throw new IllegalArgumentException("Employee's data is null or empty");
        }
        final Employee currentEmployee = findEmployee(id);
        currentEmployee.setName(name);
        currentEmployee.setSurname(surname);
        currentEmployee.setPhoneNumber(phoneNumber);

        return employeeRepository.save(currentEmployee);
    }

    @Transactional
    public Employee deleteEmployee(Long id) {
        final Employee employee = findEmployee(id);
        employeeRepository.delete(employee);
        return employee;
    }

    @Transactional
    public void deleteAllEmployees() {
        employeeRepository.deleteAll();
    }

    @Transactional
    public void addCompany(Long id, Company c) {
        final Employee employee = findEmployee(id);
        employee.setCompany(c);
        employeeRepository.save(employee);
    }

    @Transactional
    public void deleteCompany(Long id) {
        final Employee employee = findEmployee(id);
        employee.deleteCompany();
        employeeRepository.save(employee);
    }

    @Transactional
    public Employee addPosition(Long id, Position p) {
        Employee e = findEmployee(id);
        e.addNewPosition(p);
        System.out.println(e.getPositions().size());
        return employeeRepository.save(e);
    }

    @Transactional
    public Employee deletePosition(Long id, Position p) {
        Employee e = findEmployee(id);
        e.removePosition(p);
        return employeeRepository.save(e);
    }

    @Transactional
    public List<Employee> getEmployeesByPosition(Position p) {
//        List<Employee> employees = em.createQuery("select e from Employee e INNER JOIN e.positions p WHERE p.id=:posit",
//                Employee.class)
//                .setParameter("posit", p.getId())
//                .getResultList();
        List<Employee> employees = employeeRepository.findByPositions_Id(p.getId());
        return employees;
    }
}
