package com.zioinfo.kintex.system.program;

import com.zioinfo.kintex.auth.KintexPrincipal;
import com.zioinfo.kintex.common.ApiResponse;
import com.zioinfo.kintex.common.audit.Audited;
import com.zioinfo.kintex.system.SystemAccessGuard;
import jakarta.validation.Valid;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/** 시스템관리 — 프로그램(화면) 등록부 API. ADMIN 게이트. */
@RestController
@RequestMapping("/api/admin/programs")
public class ProgramController {

    private final ProgramService service;
    private final SystemAccessGuard guard;

    public ProgramController(ProgramService service, SystemAccessGuard guard) {
        this.service = service;
        this.guard = guard;
    }

    @GetMapping
    public ApiResponse<List<ProgramDto>> list(@AuthenticationPrincipal KintexPrincipal principal,
                                              @RequestParam(required = false) String keyword,
                                              @RequestParam(required = false) String category) {
        guard.requireAdmin(principal);
        return ApiResponse.ok(service.list(keyword, category));
    }

    @Audited(action = "PROGRAM_SAVE", targetType = "sys_program")
    @PostMapping
    public ApiResponse<Void> save(@AuthenticationPrincipal KintexPrincipal principal,
                                  @Valid @RequestBody ProgramSaveRequest req) {
        guard.requireAdmin(principal);
        service.save(req);
        return ApiResponse.ok(null);
    }

    @Audited(action = "PROGRAM_DELETE", targetType = "sys_program")
    @DeleteMapping("/{id}")
    public ApiResponse<Void> delete(@AuthenticationPrincipal KintexPrincipal principal,
                                    @PathVariable String id) {
        guard.requireAdmin(principal);
        service.delete(id);
        return ApiResponse.ok(null);
    }
}
