package com.zioinfo.kintex.system.loginslide;

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.http.MediaType;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * 로그인 슬라이드 API.
 * <ul>
 *   <li>공개: {@code GET /api/public/login-slides} — 활성만, 최신 등록순(created_at DESC) 최대 5건.</li>
 *   <li>관리자: {@code /api/admin/login-slides} CRUD — ADMIN 게이트({@link SystemAccessGuard}).</li>
 * </ul>
 */
@RestController
public class LoginSlideController {

    private final LoginSlideService service;
    private final SystemAccessGuard guard;

    public LoginSlideController(LoginSlideService service, SystemAccessGuard guard) {
        this.service = service;
        this.guard = guard;
    }

    /** 공개 — 로그인 화면 캐러셀용(인증 불필요). */
    @GetMapping("/api/public/login-slides")
    public ApiResponse<List<LoginSlideDto>> publicSlides() {
        return ApiResponse.ok(service.publicSlides());
    }

    /** 관리자 — 전체 목록(비활성 포함, 최신 등록순). */
    @GetMapping("/api/admin/login-slides")
    public ApiResponse<List<LoginSlideDto>> adminList(@AuthenticationPrincipal KintexPrincipal principal) {
        guard.requireAdmin(principal);
        return ApiResponse.ok(service.adminList());
    }

    /** 관리자 — 신규 등록(multipart: image + title/caption[/sortOrder]). */
    @Audited(action = "LOGIN_SLIDE_CREATE", targetType = "login_slide")
    @PostMapping(value = "/api/admin/login-slides", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public ApiResponse<LoginSlideDto> create(@AuthenticationPrincipal KintexPrincipal principal,
                                             @RequestPart("image") MultipartFile image,
                                             @RequestParam("title") String title,
                                             @RequestParam(value = "caption", required = false) String caption,
                                             @RequestParam(value = "sortOrder", required = false) Integer sortOrder) {
        guard.requireAdmin(principal);
        return ApiResponse.ok(service.create(image, title, caption, sortOrder));
    }

    /** 관리자 — 문구·정렬·활성 여부 수정. */
    @Audited(action = "LOGIN_SLIDE_UPDATE", targetType = "login_slide")
    @PutMapping("/api/admin/login-slides/{id}")
    public ApiResponse<LoginSlideDto> update(@AuthenticationPrincipal KintexPrincipal principal,
                                             @PathVariable long id,
                                             @Valid @RequestBody LoginSlideUpdateRequest req) {
        guard.requireAdmin(principal);
        return ApiResponse.ok(service.update(id, req));
    }

    /** 관리자 — 삭제(업로드 이미지 파일 정리 포함). */
    @Audited(action = "LOGIN_SLIDE_DELETE", targetType = "login_slide")
    @DeleteMapping("/api/admin/login-slides/{id}")
    public ApiResponse<Void> delete(@AuthenticationPrincipal KintexPrincipal principal,
                                    @PathVariable long id) {
        guard.requireAdmin(principal);
        service.delete(id);
        return ApiResponse.ok(null);
    }
}
