package com.lesingle.creation.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lesingle.creation.common.core.Result;
import com.lesingle.creation.common.security.UserPrincipal;
import com.lesingle.creation.dto.log.LogQueryDTO;
import com.lesingle.creation.service.SysLogService;
import com.lesingle.creation.vo.log.LogStatisticsVO;
import com.lesingle.creation.vo.log.LogVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 系统日志控制器
 */
@Tag(name = "系统日志", description = "日志查询、统计等接口")
@RestController
@RequestMapping("/api/sys-log")
@RequiredArgsConstructor
public class SysLogController {

    private final SysLogService sysLogService;

    @Operation(summary = "分页查询日志列表")
    @GetMapping("/page")
    @PreAuthorize("hasAuthority('log:read')")
    public Result<Page<LogVO>> pageQuery(
            @AuthenticationPrincipal UserPrincipal userPrincipal,
            @ModelAttribute LogQueryDTO queryDTO) {
        Long tenantId = userPrincipal.getTenantId();
        Page<LogVO> result = sysLogService.pageQuery(queryDTO, tenantId);
        return Result.success(result);
    }

    /**
     * 获取日志详情
     */
    @Operation(summary = "获取日志详情")
    @GetMapping("/{id}")
    @PreAuthorize("hasAuthority('log:read')")
    public Result<LogVO> getDetail(
            @AuthenticationPrincipal UserPrincipal userPrincipal,
            @PathVariable Long id) {
        Long tenantId = userPrincipal.getTenantId();
        LogVO result = sysLogService.getDetail(id, tenantId);
        return Result.success(result);
    }

    /**
     * 获取日志统计信息
     */
    @Operation(summary = "获取日志统计信息")
    @GetMapping("/statistics")
    @PreAuthorize("hasAuthority('log:read')")
    public Result<LogStatisticsVO> getStatistics(
            @AuthenticationPrincipal UserPrincipal userPrincipal,
            @RequestParam(required = false, defaultValue = "7") Integer days) {
        Long tenantId = userPrincipal.getTenantId();
        LogStatisticsVO result = sysLogService.getStatistics(days, tenantId);
        return Result.success(result);
    }

    /**
     * 批量删除日志
     */
    @Operation(summary = "批量删除日志")
    @DeleteMapping
    @PreAuthorize("hasAuthority('log:delete')")
    public Result<Integer> deleteByIds(
            @AuthenticationPrincipal UserPrincipal userPrincipal,
            @RequestParam("ids") List<Long> ids) {
        Long tenantId = userPrincipal.getTenantId();
        int result = sysLogService.deleteByIds(ids, tenantId);
        return Result.success(result);
    }

    /**
     * 清理过期日志
     */
    @Operation(summary = "清理过期日志")
    @PostMapping("/clean")
    @PreAuthorize("hasAuthority('log:delete')")
    public Result<Integer> cleanOldLogs(
            @AuthenticationPrincipal UserPrincipal userPrincipal,
            @RequestParam(required = false) Integer days) {
        Long tenantId = userPrincipal.getTenantId();
        int result = sysLogService.cleanOldLogs(days, tenantId);
        return Result.success(result);
    }
}
