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

import com.zioinfo.cms.common.ApiResponse;
import com.zioinfo.cms.uiws.system.dto.IdsRequest;
import com.zioinfo.cms.uiws.system.dto.PageResponse;
import com.zioinfo.cms.uiws.system.dto.ProgramDto;
import com.zioinfo.cms.uiws.system.dto.ProgramSaveDto;
import com.zioinfo.cms.uiws.system.service.ProgramService;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/** 2.8 프로그램(programs) — 목록/검색/CRUD/다중삭제. base: /api/system/programs */
@RestController
@RequestMapping("/api/system/programs")
@RequiredArgsConstructor
public class ProgramController {

    private final ProgramService programService;

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

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

    @PostMapping
    public ApiResponse<ProgramDto> create(@Valid @RequestBody ProgramSaveDto dto) {
        return ApiResponse.ok(programService.create(dto));
    }

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

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

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