package com.example.springip.lab3.controller;

import com.example.springip.WebConfiguration;
import com.example.springip.lab3.dto.CompanyDto;
import com.example.springip.lab3.dto.EmployeeDto;
import com.example.springip.lab3.dto.PositionDto;
import com.example.springip.lab3.models.Employee;
import com.example.springip.lab3.models.Position;
import com.example.springip.lab3.service.EmployeeService;
import com.example.springip.lab3.service.PositionService;
import jakarta.validation.Valid;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping(WebConfiguration.REST_API + "/employee")
public class EmployeeController {
    private final EmployeeService employeeService;
    private final PositionService positionService;

    public EmployeeController(EmployeeService employeeService, PositionService positionService) {
        this.employeeService = employeeService;
        this.positionService = positionService;
    }

    @GetMapping("/{id}")
    public EmployeeDto getEmployee(@PathVariable long id) {
        return new EmployeeDto(employeeService.findEmployee(id));
    }

    @GetMapping
    public List<EmployeeDto> getAllEmployees() {
        return employeeService.findAllEmployees().stream()
                .map(EmployeeDto::new)
                .toList();
    }

    @PostMapping
    public EmployeeDto createEmployee( @RequestBody @Valid EmployeeDto employeeDto) {
        return new EmployeeDto(employeeService.addEmployee(
                employeeDto.getSurname(),
                employeeDto.getName(),
                employeeDto.getPhoneNumber()));
    }

    @PutMapping("/{id}")
    public EmployeeDto updateEmployee(@PathVariable Long id,
                                      @RequestBody @Valid EmployeeDto employeeDto) {
        return new EmployeeDto(employeeService.updateEmployee(id, employeeDto.getSurname(),
                employeeDto.getName(), employeeDto.getPhoneNumber()));
    }

    @DeleteMapping("/{id}")
    public EmployeeDto deleteEmployee(@PathVariable Long id) {
        return new EmployeeDto(employeeService.deleteEmployee(id));
    }

    @PutMapping("/{id}/addPos")
    public EmployeeDto addPosition(@PathVariable Long id,
                                      @RequestParam("position") Long position) {
        Position p = positionService.findPosition(position);
        if (p == null)
            return null;
        return new EmployeeDto(employeeService.addPosition(id, p));
    }
    @PutMapping("/{id}/delPos")
    public EmployeeDto delPosition(@PathVariable Long id,
                                   @RequestParam("position") Long position) {
        Position p = positionService.findPosition(position);
        if (p == null)
            return null;
        return new EmployeeDto(employeeService.deletePosition(id, p));
    }

    @GetMapping("/free")
    public List<EmployeeDto> getAllFreeEmployees() {
        return employeeService.findAllFreeEmployees().stream()
                .map(EmployeeDto::new)
                .toList();
    }

    @GetMapping("/position")
    public List<EmployeeDto> getEmployeesByPosition(@RequestParam Long pos) {
        Position p = positionService.findPosition(pos);
        if (p == null) {
            return null;
        }

        return employeeService.getEmployeesByPosition(p).stream()
                .map(EmployeeDto::new)
                .toList();
    }
}
