package com.example.springip;

import com.example.springip.lab3.models.Company;
import com.example.springip.lab3.models.Employee;
import com.example.springip.lab3.service.CompanyService;
import com.example.springip.lab3.service.EmployeeService;
import jakarta.persistence.EntityNotFoundException;
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;

import java.util.NoSuchElementException;

@SpringBootTest
public class CompanyServiceTests {
    @Autowired
    private CompanyService companyService;

    @Autowired
    private EmployeeService employeeService;


    @Test
    public void testDeleteAllCompanies() {
        companyService.deleteAllCompanies();
        int n = 3;
        for (int i = 0; i < n; i++) {
            String name = "Company" + i;
            companyService.addCompany(name);
        }
        companyService.deleteAllCompanies();
        Assertions.assertEquals(companyService.findAllCompanies().size(), 0);
    }

    @Test
    public void testDeleteCompany() {
        companyService.deleteAllCompanies();
        String name = "Company1";
        Company c = companyService.addCompany(name);
        companyService.deleteCompany(c.getId());
        Assertions.assertThrows(EntityNotFoundException.class, () -> {companyService.findCompany(c.getId());});
    }

    @Test
    public void testAddCompany() {
        companyService.deleteAllCompanies();
        String name = "Company1";
        Company c = companyService.addCompany(name);
        Assertions.assertNotNull(companyService.findCompany(c.getId()));
        companyService.deleteAllCompanies();
    }

    @Test
    public void testUpdateCompany() {
        companyService.deleteAllCompanies();
        String name = "Company1";
        Company c = companyService.addCompany(name);
        String name2 = "Company2";
        companyService.updateCompany(c.getId(), name2);
        Assertions.assertNotNull(companyService.findCompany(c.getId()));
        companyService.deleteAllCompanies();
    }


    @Test
    public void testFindAllCompanies() {
        companyService.deleteAllCompanies();
        int n = 3;
        for (int i = 0; i < n; i++) {
            String name = "Company" + i;
            companyService.addCompany(name);
        }

        Assertions.assertEquals(companyService.findAllCompanies().size(), n);
        companyService.deleteAllCompanies();
    }

    @Test
    public void testAddEmployee() {

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


        final String name = "Company";
        Company c = companyService.addCompany(name);
        Employee newEmployee = employeeService.addEmployee("cha", "chacha", "111");
        companyService.addNewEmployee(c.getId(), newEmployee);

        Assertions.assertTrue(companyService.findCompany(c.getId()).getEmployees().contains(newEmployee));


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

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


        final String name = "Company";
        Company c = companyService.addCompany(name);
        Employee newEmployee = employeeService.addEmployee("cha", "chacha", "111");

        companyService.addNewEmployee(c.getId(), newEmployee);
        Assertions.assertTrue(companyService.findCompany(c.getId()).getEmployees().contains(newEmployee));

        companyService.deleteEmployee(c.getId(), newEmployee);
        Assertions.assertFalse(companyService.findCompany(c.getId()).getEmployees().contains(newEmployee));

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

    }
}
