package com.lesingle.creation.controller;

import com.lesingle.creation.common.core.Result;
import com.lesingle.creation.common.security.UserPrincipal;
import com.lesingle.creation.dto.dict.CreateDictDTO;
import com.lesingle.creation.dto.dict.UpdateDictDTO;
import com.lesingle.creation.service.DictService;
import com.lesingle.creation.vo.dict.DictDetailVO;
import com.lesingle.creation.vo.dict.DictListVO;
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.*;

import java.util.List;

/**
 * 字典管理控制器
 */
@Tag(name = "字典管理")
@RestController
@RequestMapping("/api/dict")
@RequiredArgsConstructor
public class DictController {

    private final DictService dictService;

    @PostMapping
    @Operation(summary = "创建字典")
    @PreAuthorize("hasAuthority('dict:create')")
    public Result<DictDetailVO> create(
            @AuthenticationPrincipal UserPrincipal userPrincipal,
            @RequestBody @Validated CreateDictDTO dto) {
        Long tenantId = userPrincipal.getTenantId();
        Long operatorId = userPrincipal.getUserId();
        DictDetailVO result = dictService.create(dto, tenantId, operatorId);
        return Result.success(result);
    }

    @GetMapping
    @Operation(summary = "字典列表")
    @PreAuthorize("hasAuthority('dict:read')")
    public Result<com.baomidou.mybatisplus.extension.plugins.pagination.Page<DictListVO>> 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<DictListVO> result =
                dictService.pageList(tenantId, page, pageSize);
        return Result.success(result);
    }

    @GetMapping("/code/{code}")
    @Operation(summary = "根据编码查询字典")
    @PreAuthorize("hasAuthority('dict:read')")
    public Result<DictDetailVO> getByCode(
            @AuthenticationPrincipal UserPrincipal userPrincipal,
            @PathVariable String code) {
        Long tenantId = userPrincipal.getTenantId();
        DictDetailVO result = dictService.getByCode(code, tenantId);
        return Result.success(result);
    }

    @GetMapping("/{id}")
    @Operation(summary = "字典详情")
    @PreAuthorize("hasAuthority('dict:read')")
    public Result<DictDetailVO> detail(
            @AuthenticationPrincipal UserPrincipal userPrincipal,
            @PathVariable Long id) {
        Long tenantId = userPrincipal.getTenantId();
        DictDetailVO result = dictService.detail(id, tenantId);
        return Result.success(result);
    }

    @PutMapping("/{id}")
    @Operation(summary = "更新字典")
    @PreAuthorize("hasAuthority('dict:update')")
    public Result<DictDetailVO> update(
            @AuthenticationPrincipal UserPrincipal userPrincipal,
            @PathVariable Long id,
            @RequestBody @Validated UpdateDictDTO dto) {
        Long tenantId = userPrincipal.getTenantId();
        DictDetailVO result = dictService.update(id, dto, tenantId);
        return Result.success(result);
    }

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