package com.example.springip;

import com.example.springip.lab3.models.Company;
import com.example.springip.lab3.models.Employee;
import com.example.springip.lab3.models.Position;
import com.example.springip.lab3.service.CompanyService;
import com.example.springip.lab3.service.EmployeeService;
import com.example.springip.lab3.service.PositionService;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest
public class EmployeeServiceTests {
    @Autowired
    private EmployeeService employeeService;

    @Autowired
    private CompanyService companyService;

    @Autowired
    private PositionService positionService;

    @Test
    public void testAddEmployee() {
        employeeService.deleteAllEmployees();

        employeeService.addEmployee("name", "surname", "111");
        Assertions.assertEquals(1, employeeService.findAllEmployees().size());
        employeeService.deleteAllEmployees();
    }

    @Test
    public void testUpdateEmployee() {
        employeeService.deleteAllEmployees();
        final String newName = "name";

        Employee e  = employeeService.addEmployee("surname", "name", "111");
        employeeService.updateEmployee(e.getId(),e.getSurname(), newName, e.getPhoneNumber());
        Assertions.assertEquals(newName, employeeService.findEmployee(e.getId()).getName());
        employeeService.deleteAllEmployees();
    }

    @Test
    public void testDeleteEmployee() {
        employeeService.deleteAllEmployees();

        Employee e = employeeService.addEmployee("name", "surname", "111");
        employeeService.deleteEmployee(e.getId());
        Assertions.assertEquals(0, employeeService.findAllEmployees().size());
        employeeService.deleteAllEmployees();
    }

    @Test
    public void testFindAllEmployee() {
        employeeService.deleteAllEmployees();
        int n = 3;
        for (int i = 0; i < n; i++) {
            employeeService.addEmployee("name", "surname", "111");
        }
        Assertions.assertEquals(n, employeeService.findAllEmployees().size());
        employeeService.deleteAllEmployees();
    }

    @Test
    public void testAddCompany() {
        employeeService.deleteAllEmployees();
        companyService.deleteAllCompanies();

        Employee e = employeeService.addEmployee("name", "surname", "111");
        Company c = companyService.addCompany("Comp");
        companyService.addNewEmployee(c.getId(), e);
        Assertions.assertEquals(c, employeeService.findEmployee(e.getId()).getCompany());

        employeeService.deleteAllEmployees();
        companyService.deleteAllCompanies();
    }

    @Test
    public void testDeleteFromCompany() {
        employeeService.deleteAllEmployees();
        companyService.deleteAllCompanies();

        Employee e = employeeService.addEmployee("name", "surname", "111");
        Company c = companyService.addCompany("Comp");

        employeeService.addCompany(e.getId(), c);
        Assertions.assertEquals(c, employeeService.findEmployee(e.getId()).getCompany());

        employeeService.deleteCompany(e.getId());
        Assertions.assertNull(employeeService.findEmployee(e.getId()).getCompany());

        employeeService.deleteAllEmployees();
        companyService.deleteAllCompanies();
    }

    @Test
    public void testGetByPosition() {
        employeeService.deleteAllEmployees();
        positionService.deleteAllPositions();

        Position p = positionService.addPosition("Position 1");
        Position p2 = positionService.addPosition("Position 2");

        final int n = 10;
        for (int i = 0; i < n; i++) {
            Employee e = employeeService.addEmployee("1111", "name" + i, "surname");
            if (i % 2 == 0) {
                employeeService.addPosition(e.getId(), p);
            }
            else {
                employeeService.addPosition(e.getId(), p2);
            }
        }

        Assertions.assertEquals(n / 2, employeeService.getEmployeesByPosition(p).size());
        Assertions.assertEquals(n / 2, employeeService.getEmployeesByPosition(p2).size());

        employeeService.deleteAllEmployees();
        positionService.deleteAllPositions();
    }
}
