package com.zioinfo.mes.uiws.system.controller;

import com.zioinfo.mes.common.ApiResponse;
import com.zioinfo.mes.uiws.system.dto.CompanyDto;
import com.zioinfo.mes.uiws.system.dto.CompanySaveDto;
import com.zioinfo.mes.uiws.system.dto.IdsRequest;
import com.zioinfo.mes.uiws.system.dto.PageResponse;
import com.zioinfo.mes.uiws.system.service.CompanyService;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/** 2.4 거래처(companies) — 목록/검색팝업/CRUD/다중삭제. base: /api/system/companies */
@RestController
@RequestMapping("/api/system/companies")
@RequiredArgsConstructor
public class CompanyController {

    private final CompanyService companyService;

    @GetMapping
    public ApiResponse<PageResponse<CompanyDto>> list(
            @RequestParam(required = false) String keyword,
            @RequestParam(defaultValue = "0") int page,
            @RequestParam(defaultValue = "20") int size) {
        return ApiResponse.ok(companyService.list(keyword, page, size));
    }

    @GetMapping("/search")
    public ApiResponse<List<CompanyDto>> search(@RequestParam(required = false) String keyword) {
        return ApiResponse.ok(companyService.searchPopup(keyword));
    }

    @PostMapping
    public ApiResponse<CompanyDto> create(@Valid @RequestBody CompanySaveDto dto) {
        return ApiResponse.ok(companyService.create(dto));
    }

    @GetMapping("/{id}")
    public ApiResponse<CompanyDto> get(@PathVariable("id") String id) {
        return ApiResponse.ok(companyService.get(id));
    }

    @PutMapping("/{id}")
    public ApiResponse<CompanyDto> update(@PathVariable("id") String id, @Valid @RequestBody CompanySaveDto dto) {
        return ApiResponse.ok(companyService.update(id, dto));
    }

    @DeleteMapping
    public ApiResponse<Void> delete(@Valid @RequestBody IdsRequest req) {
        companyService.delete(req.ids());
        return ApiResponse.ok(null);
    }
}
