package shop.alien.storeplatform.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiOperationSupport;
import io.swagger.annotations.ApiSort;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import shop.alien.entity.result.R;
import shop.alien.entity.storePlatform.StorePlatformOperationLog;
import shop.alien.storeplatform.enums.PlatformOperationModule;
import shop.alien.storeplatform.service.PlatformOperationLogService;

import java.util.Date;

/**
 * 平台操作记录查询接口
 *
 * @author system
 * @since 2025-01-XX
 */
@Slf4j
@Api(tags = {"平台-操作记录"})
@ApiSort(99)
@RestController
@RequestMapping("/platform/operationLog")
@RequiredArgsConstructor
public class PlatformOperationLogController {

    private final PlatformOperationLogService platformOperationLogService;

    @ApiOperation("分页查询操作记录")
    @ApiOperationSupport(order = 1)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页码", dataType = "int", paramType = "query", required = true),
            @ApiImplicitParam(name = "size", value = "页容", dataType = "int", paramType = "query", required = true),
            @ApiImplicitParam(name = "module", value = "操作模块（ACCOUNT/ROLE）", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "account", value = "操作账号（模糊查询）", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "startTime", value = "开始时间", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "endTime", value = "结束时间", dataType = "String", paramType = "query")
    })
    @GetMapping("/page")
    public R<IPage<StorePlatformOperationLog>> getPage(
            @RequestParam(name = "page", defaultValue = "1") int page,
            @RequestParam(name = "size", defaultValue = "10") int size,
            @RequestParam(name = "module", required = false) String module,
            @RequestParam(name = "account", required = false) String account,
            @RequestParam(name = "startTime", required = false) @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date startTime,
            @RequestParam(name = "endTime", required = false) @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date endTime) {
        String moduleName = resolveModuleName(module);
        log.info("PlatformOperationLogController.getPage?page={}, size={}, module={}, account={}, startTime={}, endTime={}",
                page, size, moduleName, account, startTime, endTime);
        return R.data(platformOperationLogService.getOperationLogPage(page, size, moduleName, account, startTime, endTime));
    }

    private String resolveModuleName(String module) {
        if (!StringUtils.hasText(module)) {
            return null;
        }
        PlatformOperationModule operationModule = PlatformOperationModule.fromCode(module);
        return operationModule != null ? operationModule.getModuleName() : null;
    }
}
