package com.example.springip.lab3.controller;

import com.example.springip.WebConfiguration;
import com.example.springip.lab3.dto.CompanyDto;
import com.example.springip.lab3.dto.PositionDto;
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 + "/position")
public class PositionController {
    private final PositionService positionService;

    public PositionController(PositionService positionService) {
        this.positionService = positionService;
    }

    @GetMapping("/{id}")
    public PositionDto getCompany(@PathVariable long id) {
        return new PositionDto(positionService.findPosition(id));
    }

    @GetMapping
    public List<PositionDto> getAllCompanies() {
        return positionService.findAllPositions().stream()
                .map(PositionDto::new)
                .toList();
    }

    @PostMapping
    public PositionDto create(@RequestBody @Valid PositionDto positionDto) {
        return new PositionDto(positionService.addPosition(positionDto.getName()));
    }

    @PutMapping("/{id}")
    public PositionDto update(@PathVariable Long id,
                              @RequestBody @Valid PositionDto positionDto) {
        return new PositionDto(positionService.updatePosition(id, positionDto.getName()));
    }

    @DeleteMapping("/{id}")
    public PositionDto delete(@PathVariable Long id) {
        return new PositionDto(positionService.deletePosition(id));
    }
}
