package com.lesingle.creation.controller;

import com.lesingle.creation.common.core.Result;
import com.lesingle.creation.common.security.UserPrincipal;
import com.lesingle.creation.dto.config.CreateConfigDTO;
import com.lesingle.creation.dto.config.UpdateConfigDTO;
import com.lesingle.creation.service.ConfigService;
import com.lesingle.creation.vo.config.ConfigDetailVO;
import com.lesingle.creation.vo.config.ConfigListVO;
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.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * 系统配置管理控制器
 */
@Tag(name = "系统配置管理")
@RestController
@RequestMapping("/api/config")
@RequiredArgsConstructor
public class ConfigController {

    private final ConfigService configService;

    @PostMapping
    @Operation(summary = "创建配置")
    @PreAuthorize("hasAuthority('config:create')")
    public Result<ConfigDetailVO> create(
            @AuthenticationPrincipal UserPrincipal userPrincipal,
            @RequestBody @Validated CreateConfigDTO dto) {
        Long tenantId = userPrincipal.getTenantId();
        Long operatorId = userPrincipal.getUserId();
        ConfigDetailVO result = configService.create(dto, tenantId, operatorId);
        return Result.success(result);
    }

    @GetMapping
    @Operation(summary = "配置列表")
    @PreAuthorize("hasAuthority('config:read')")
    public Result<com.baomidou.mybatisplus.extension.plugins.pagination.Page<ConfigListVO>> pageList(
            @AuthenticationPrincipal UserPrincipal userPrincipal,
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "10") int pageSize) {
        Long tenantId = userPrincipal.getTenantId();
        com.baomidou.mybatisplus.extension.plugins.pagination.Page<ConfigListVO> result =
                configService.pageList(tenantId, page, pageSize);
        return Result.success(result);
    }

    @GetMapping("/key/{key}")
    @Operation(summary = "根据键查询配置")
    @PreAuthorize("hasAuthority('config:read')")
    public Result<ConfigDetailVO> getByKey(
            @AuthenticationPrincipal UserPrincipal userPrincipal,
            @PathVariable String key) {
        Long tenantId = userPrincipal.getTenantId();
        ConfigDetailVO result = configService.getByKey(key, tenantId);
        return Result.success(result);
    }

    @GetMapping("/{id}")
    @Operation(summary = "配置详情")
    @PreAuthorize("hasAuthority('config:read')")
    public Result<ConfigDetailVO> detail(
            @AuthenticationPrincipal UserPrincipal userPrincipal,
            @PathVariable Long id) {
        Long tenantId = userPrincipal.getTenantId();
        ConfigDetailVO result = configService.detail(id, tenantId);
        return Result.success(result);
    }

    @PutMapping("/{id}")
    @Operation(summary = "更新配置")
    @PreAuthorize("hasAuthority('config:update')")
    public Result<ConfigDetailVO> update(
            @AuthenticationPrincipal UserPrincipal userPrincipal,
            @PathVariable Long id,
            @RequestBody @Validated UpdateConfigDTO dto) {
        Long tenantId = userPrincipal.getTenantId();
        ConfigDetailVO result = configService.update(id, dto, tenantId);
        return Result.success(result);
    }

    @DeleteMapping("/{id}")
    @Operation(summary = "删除配置")
    @PreAuthorize("hasAuthority('config:delete')")
    public Result<Void> delete(
            @AuthenticationPrincipal UserPrincipal userPrincipal,
            @PathVariable Long id) {
        Long tenantId = userPrincipal.getTenantId();
        configService.delete(id, tenantId);
        return Result.success();
    }
}
