package com.backendsys.modules.cms.page.controller;

import com.backendsys.modules.cms.page.entity.Page;
import com.backendsys.modules.cms.page.service.PageService;
import com.backendsys.modules.common.config.security.annotations.Anonymous;
import com.backendsys.modules.common.utils.Result;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@Validated
@RestController
@Tag(name = "单页管理")
public class PageController {

    @Autowired
    private PageService pageService;

    @Operation(summary = "获取单页列表")
    @PreAuthorize("@sr.hasPermission('10.4')")
    @GetMapping("/api/cms/page/getPageList")
    public Result getPage(@Validated Page page) {
        return Result.success().put("data", pageService.selectPageList(page));
    }

    @Operation(summary = "获取单页详情")
    @PreAuthorize("@sr.hasPermission('10.4.1')")
    @GetMapping("/api/cms/page/getPageDetail")
    public Result getPageDetail(@Validated(Page.Detail.class) Page page) {
        return Result.success().put("data", pageService.selectPageDetail(page));
    }

    @Anonymous
    @Operation(summary = "获取单页详情 (公开)")
    @GetMapping("/api/public/cms/page/getPageDetail")
    public Result getPageDetailPublic(@Validated(Page.Detail.class) Page page) {
        return Result.success().put("data", pageService.selectPageDetailPublic(page));
    }

    @Operation(summary = "创建单页")
    @PreAuthorize("@sr.hasPermission('10.4.2')")
    @PostMapping("/api/cms/page/createPage")
    public Result createPage(@Validated(Page.Create.class) @RequestBody Page page) {
        return Result.success().put("data", pageService.insertPage(page));
    }

    @Operation(summary = "编辑单页")
    @PreAuthorize("@sr.hasPermission('10.4.3')")
    @PutMapping("/api/cms/page/updatePage")
    public Result updatePage(@Validated(Page.Update.class) @RequestBody Page page) {
        return Result.success().put("data", pageService.updatePage(page));
    }

    @Operation(summary = "删除单页")
    @PreAuthorize("@sr.hasPermission('10.4.4')")
    @DeleteMapping("/api/cms/page/deletePage")
    public Result deletePage(@Validated(Page.Delete.class) @RequestBody Page page) {
        return Result.success().put("data", pageService.deletePage(page));
    }

}
