package com.platform2easy.genesis.web.controller;

import com.platform2easy.genesis.domain.model.Company;
import com.platform2easy.genesis.domain.service.CompanyService;
import com.platform2easy.genesis.web.dto.CompanyWithUserDTO;
import com.platform2easy.genesis.web.dto.CompanyResponseDTO;
import lombok.AllArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import com.platform2easy.genesis.web.dto.CompanyUserResponseDTO;

import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/api/company")
@AllArgsConstructor
public class CompanyController {

    private final CompanyService service;

    @GetMapping
    @ResponseStatus(HttpStatus.OK)
    public List<Company> listAll() {
        return service.listarTodos();
    }

    @GetMapping("/{id}")
    @ResponseStatus(HttpStatus.OK)
    public Company getById(@PathVariable Long id) {
        return service.buscarPorId(id);
    }

    @PostMapping
    public ResponseEntity<CompanyResponseDTO> create(@RequestBody Company company) {
        company.setId(null);
        Company savedCompany = service.salvar(company);
        return ResponseEntity.status(HttpStatus.CREATED).body(CompanyResponseDTO.fromEntity(savedCompany));
    }

    @PutMapping("/{id}")
    @ResponseStatus(HttpStatus.OK)
    public Company update(@PathVariable Long id, @RequestBody Company company) {
        company.setId(id);
        return service.salvar(company);
    }

    @DeleteMapping("/{id}")
    @ResponseStatus(HttpStatus.NO_CONTENT)
    public void delete(@PathVariable Long id) {
        service.deletarPorId(id);
    }

    @PostMapping("/register-with-user")
    @ResponseStatus(HttpStatus.CREATED)
    public CompanyUserResponseDTO registerCompanyWithUser(@RequestBody CompanyWithUserDTO dto) {
        return service.registerCompanyWithUser(dto);
    }
}