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("/company")
public class CompanyMvcController {
    private final CompanyService companyService;
    private final EmployeeService employeeService;
    private final PositionService positionService;

    public CompanyMvcController(CompanyService companyService, EmployeeService employeeService, PositionService positionService) {
        this.companyService = companyService;
        this.employeeService = employeeService;
        this.positionService = positionService;
    }

    @GetMapping
    public String getCompanies(Model model) {
        model.addAttribute("companies",
                companyService.findAllCompanies().stream()
                        .map(CompanyDto::new)
                        .toList());
        return "company";
    }

    @GetMapping("/one/{id}")
    public String getCompany(@PathVariable(required = false) Long id, Model model) {
        model.addAttribute("company",
                new CompanyDto(companyService.findCompany(id)));
        model.addAttribute("freeEmployees", employeeService.findAllFreeEmployees().stream().map(EmployeeDto::new).toList());
        model.addAttribute("positions", positionService.findAllPositions().stream().map(PositionDto::new).toList());
        return "company-one";
    }

    @PostMapping(value = "/redirect")
    public String redirectToCompanyPage(@RequestParam("companyId") Long companyId) {
        return "redirect:/company/one/" + companyId;
    }

    @GetMapping(value = {"/edit", "/edit/{id}"})
    public String editCompany(@PathVariable(required = false) Long id,
                              Model model) {
        if (id == null || id <= 0) {
            model.addAttribute("companyDto", new CompanyDto());
        } else {
            model.addAttribute("companyId", id);
            model.addAttribute("companyDto", new CompanyDto(companyService.findCompany(id)));
        }
        return "company-edit";
    }

    @PostMapping(value = {"/", "/{id}"})
    public String saveCompany(@PathVariable(required = false) Long id,
                              @ModelAttribute @Valid CompanyDto companyDto,
                              BindingResult bindingResult,
                              Model model) {
        if (bindingResult.hasErrors()) {
            model.addAttribute("errors", bindingResult.getAllErrors());
            return "company-edit";
        }
        if (id == null || id <= 0) {
            companyService.addCompany(companyDto.getName());
        } else {
            companyService.updateCompany(id, companyDto.getName());
        }
        return "redirect:/company";
    }




    @PostMapping("/delete/{id}")
    public String deleteCompany(@PathVariable Long id) {
        companyService.deleteCompany(id);
        return "redirect:/company";
    }
}
