package com.nanri.aiimage.modules.shopkey.controller;

import com.nanri.aiimage.common.api.ApiResponse;
import com.nanri.aiimage.modules.shopkey.model.dto.ShopKeyCreateRequest;
import com.nanri.aiimage.modules.shopkey.model.dto.ShopKeyUpdateRequest;
import com.nanri.aiimage.modules.shopkey.model.vo.ShopKeyItemVo;
import com.nanri.aiimage.modules.shopkey.model.vo.ShopKeyPageVo;
import com.nanri.aiimage.modules.shopkey.service.ShopKeyService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequiredArgsConstructor
@RequestMapping("/api/admin/shop-keys")
@Tag(name = "店铺密钥管理", description = "维护店铺密钥，支持增删改查。")
public class ShopKeyController {

    private final ShopKeyService shopKeyService;

    @GetMapping
    @Operation(summary = "分页查询店铺密钥", description = "分页查询店铺密钥列表。")
    @ApiResponses({
            @io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "200", description = "查询成功", content = @Content(schema = @Schema(implementation = ShopKeyPageVo.class)))
    })
    public ApiResponse<ShopKeyPageVo> page(
            @Parameter(description = "页码") @RequestParam(defaultValue = "1") Long page,
            @Parameter(description = "每页数量") @RequestParam(defaultValue = "15") Long pageSize) {
        return ApiResponse.success(shopKeyService.page(page, pageSize));
    }

    @PostMapping
    @Operation(summary = "新增店铺密钥", description = "新增一条店铺密钥记录。")
    @ApiResponses({
            @io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "200", description = "创建成功", content = @Content(schema = @Schema(implementation = ShopKeyItemVo.class))),
            @io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "400", description = "参数不合法")
    })
    public ApiResponse<ShopKeyItemVo> create(@Valid @RequestBody ShopKeyCreateRequest request) {
        return ApiResponse.success("创建成功", shopKeyService.create(request));
    }

    @PutMapping("/{id}")
    @Operation(summary = "更新店铺密钥", description = "按 ID 更新一条店铺密钥记录。")
    @ApiResponses({
            @io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "200", description = "更新成功", content = @Content(schema = @Schema(implementation = ShopKeyItemVo.class))),
            @io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "400", description = "参数不合法"),
            @io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "404", description = "记录不存在")
    })
    public ApiResponse<ShopKeyItemVo> update(
            @Parameter(description = "主键ID", required = true) @PathVariable Long id,
            @Valid @RequestBody ShopKeyUpdateRequest request) {
        return ApiResponse.success("更新成功", shopKeyService.update(id, request));
    }

    @DeleteMapping("/{id}")
    @Operation(summary = "删除店铺密钥", description = "按 ID 删除一条店铺密钥记录。")
    @ApiResponses({
            @io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "200", description = "删除成功"),
            @io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "404", description = "记录不存在")
    })
    public ApiResponse<Void> delete(@Parameter(description = "主键ID", required = true) @PathVariable Long id) {
        shopKeyService.delete(id);
        return ApiResponse.success("删除成功", null);
    }
}
