package com.example.springip.lab3.mvc;

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.service.CompanyService;
import com.example.springip.lab3.service.EmployeeService;
import com.example.springip.lab3.service.PositionService;
import jakarta.validation.Valid;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

@Controller
@RequestMapping("/employee")
public class EmployeeMvcController {
    private final EmployeeService employeeService;
    private final PositionService positionService;

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

    @GetMapping
    public String getEmployees(Model model) {
        model.addAttribute("employees",
                employeeService.findAllEmployees().stream()
                        .map(EmployeeDto::new)
                        .toList());
        return "employee";
    }

    @GetMapping(value = {"/edit/", "/edit/{id}"})
    public String editCompany(@PathVariable(required = false) Long id,
                              Model model) {
        if (id == null || id <= 0) {
            model.addAttribute("employeeDto", new EmployeeDto());
        } else {
            model.addAttribute("employeeId", id);
            model.addAttribute("employeeDto", new EmployeeDto(employeeService.findEmployee(id)));
        }
        return "employee-edit";
    }

    @PostMapping(value = {"/", "/{id}"})
    public String saveCompany(@PathVariable(required = false) Long id,
                              @ModelAttribute @Valid EmployeeDto employeeDto,
                              BindingResult bindingResult,
                              Model model) {
        if (bindingResult.hasErrors()) {
            model.addAttribute("errors", bindingResult.getAllErrors());
            return "employee-edit";
        }
        if (id == null || id <= 0) {
            employeeService.addEmployee(employeeDto.getSurname(), employeeDto.getName(), employeeDto.getPhoneNumber());
        } else {
            employeeService.updateEmployee(id, employeeDto.getSurname(), employeeDto.getName(), employeeDto.getPhoneNumber());
        }
        return "redirect:/employee";
    }


    @PostMapping("/delete/{id}")
    public String deleteEmployee(@PathVariable Long id) {
        employeeService.deleteEmployee(id);
        return "redirect:/employee";
    }

    @GetMapping("/report")
    public String getReport(Model model) {
        model.addAttribute("positions",
                positionService.findAllPositions().stream()
                        .map(PositionDto::new)
                        .toList());
        return "report";
    }
}
