package com.ga.api.controller.notice;

import com.ga.api.service.notice.NoticeService;
import com.ga.common.annotation.DataChangeLog;
import com.ga.common.annotation.RequirePermission;
import com.ga.common.model.ApiResponse;
import com.ga.common.model.PageResponse;
import com.ga.common.util.SecurityUtil;
import com.ga.core.vo.notice.*;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Tag(name = "공지사항 & 알림")
@RestController
@RequiredArgsConstructor
public class NoticeController {

    private final NoticeService service;

    // ── Notice ────────────────────────────────────────────────────────────────

    @GetMapping("/api/notices")
    @RequirePermission(menu = "NOTICE", perm = "READ")
    public ApiResponse<PageResponse<NoticeResp>> list(@ModelAttribute NoticeSearchParam param) {
        return ApiResponse.ok(service.list(param));
    }

    @GetMapping("/api/notices/active")
    @RequirePermission(menu = "NOTICE", perm = "READ")
    public ApiResponse<List<NoticeResp>> activeList() {
        return ApiResponse.ok(service.activeList());
    }

    @GetMapping("/api/notices/{noticeId}")
    @RequirePermission(menu = "NOTICE", perm = "READ")
    public ApiResponse<NoticeResp> detail(@PathVariable Long noticeId) {
        return ApiResponse.ok(service.detail(noticeId));
    }

    @PostMapping("/api/notices")
    @RequirePermission(menu = "NOTICE", perm = "CREATE")
    @DataChangeLog(menu = "NOTICE", table = "notice")
    public ApiResponse<Long> create(@Valid @RequestBody NoticeSaveReq req) {
        return ApiResponse.ok(service.create(req));
    }

    @PutMapping("/api/notices/{noticeId}")
    @RequirePermission(menu = "NOTICE", perm = "UPDATE")
    @DataChangeLog(menu = "NOTICE", table = "notice")
    public ApiResponse<Void> update(@PathVariable Long noticeId,
                                    @Valid @RequestBody NoticeSaveReq req) {
        service.update(noticeId, req);
        return ApiResponse.ok();
    }

    @DeleteMapping("/api/notices/{noticeId}")
    @RequirePermission(menu = "NOTICE", perm = "DELETE")
    @DataChangeLog(menu = "NOTICE", table = "notice")
    public ApiResponse<Void> delete(@PathVariable Long noticeId) {
        service.delete(noticeId);
        return ApiResponse.ok();
    }

    // ── UserNotification ──────────────────────────────────────────────────────

    @GetMapping("/api/user-notifications")
    @RequirePermission(menu = "NOTICE", perm = "READ")
    public ApiResponse<PageResponse<UserNotificationResp>> listNotifications(
            @ModelAttribute UserNotificationSearchParam param) {
        return ApiResponse.ok(service.listNotifications(param));
    }

    @GetMapping("/api/user-notifications/unread-count")
    @RequirePermission(menu = "NOTICE", perm = "READ")
    public ApiResponse<Integer> unreadCount(@RequestParam(required = false) Long userId) {
        Long target = userId != null ? userId : SecurityUtil.getCurrentUserId();
        return ApiResponse.ok(service.unreadCount(target));
    }

    @PostMapping("/api/user-notifications/{notificationId}/read")
    @RequirePermission(menu = "NOTICE", perm = "READ")
    public ApiResponse<Void> markRead(@PathVariable Long notificationId,
                                      @RequestParam(required = false) Long userId) {
        Long target = userId != null ? userId : SecurityUtil.getCurrentUserId();
        service.markRead(notificationId, target);
        return ApiResponse.ok();
    }

    @PostMapping("/api/user-notifications/read-all")
    @RequirePermission(menu = "NOTICE", perm = "READ")
    public ApiResponse<Void> markAllRead(@RequestParam Long userId) {
        service.markAllRead(userId);
        return ApiResponse.ok();
    }
}
