package com.example.springip.lab3.service;

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

import java.util.List;

@Service
public class EmployeeService {
    @PersistenceContext
    private EntityManager em;

    @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);
        em.persist(employee);
        return employee;
    }

    @Transactional
    public Employee findEmployee(Long id) {
        final Employee employee = em.find(Employee.class, id);
        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 em.createQuery("select e from Employee e", Employee.class)
                .getResultList();
    }

    @Transactional
    public Employee updateCompany(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 em.merge(currentEmployee);
    }

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

    @Transactional
    public void deleteAllEmployees() {
        em.createQuery("delete from Employee").executeUpdate();
    }

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

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

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

    @Transactional
    public void deletePosition(Long id, Position p) {
        Employee e = findEmployee(id);
        e.removePosition(p);
        em.merge(e);
        em.flush();
    }

    @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();

        for (Employee e : employees) {
            System.out.println(e);
        }
        return employees;
    }
}
