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

import com.backendsys.modules.cms.navigation.entity.Navigation;
import com.backendsys.modules.cms.navigation.service.NavigationService;
import com.backendsys.modules.common.aspect.SysLog;
import com.backendsys.modules.common.config.security.annotations.Anonymous;
import com.backendsys.modules.common.config.security.utils.HttpRequestUtil;
import com.backendsys.modules.common.utils.Result;
import com.backendsys.utils.LanguageUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@Validated
@RestController
@Tag(name = "导航管理")
public class NavigationController {

    @Autowired
    private LanguageUtil languageUtil;

    @Autowired
    private NavigationService navigationService;

    @Operation(summary = "获取导航列表")
    @PreAuthorize("@sr.hasPermission('10.6')")
    @GetMapping("/api/cms/navigation/getNavigationList")
    public Result getNavigation(@Validated Navigation navigation) {
        return Result.success().put("data", navigationService.selectNavigationList(navigation));
    }

    @Anonymous
    @Operation(summary = "获取导航列表 (公共)")
    @GetMapping("/api/app/cms/navigation/getNavigationList")
    public Result getNavigationPublic() {
        String lang = languageUtil.getLang();
        return Result.success().put("data", navigationService.selectNavigationTemplate(lang));
    }

    @Operation(summary = "获取导航详情")
    @PreAuthorize("@sr.hasPermission('10.6.1')")
    @GetMapping("/api/cms/navigation/getNavigationDetail")
    public Result getNavigationDetail(@Validated(Navigation.Detail.class) Navigation navigation) {
        return Result.success().put("data", navigationService.selectNavigationDetail(navigation));
    }

    @SysLog("创建导航")
    @Operation(summary = "创建导航")
    @PreAuthorize("@sr.hasPermission('10.6.2')")
    @PostMapping("/api/cms/navigation/createNavigation")
    public Result createNavigation(@Validated(Navigation.Create.class) @RequestBody Navigation navigation) {
        return Result.success().put("data", navigationService.insertNavigation(navigation));
    }

    @SysLog("编辑导航")
    @Operation(summary = "编辑导航")
    @PreAuthorize("@sr.hasPermission('10.6.3')")
    @PutMapping("/api/cms/navigation/updateNavigation")
    public Result updateNavigation(@Validated(Navigation.Update.class) @RequestBody Navigation navigation) {
        return Result.success().put("data", navigationService.updateNavigation(navigation));
    }

    @SysLog("删除导航")
    @Operation(summary = "删除导航")
    @PreAuthorize("@sr.hasPermission('10.6.4')")
    @DeleteMapping("/api/cms/navigation/deleteNavigation")
    public Result deleteNavigation(@Validated(Navigation.Delete.class) @RequestBody Navigation navigation) {
        return Result.success().put("data", navigationService.deleteNavigation(navigation));
    }

    @SysLog("删除导航 (批量)")
    @Operation(summary = "删除导航 (批量)")
    @PreAuthorize("@sr.hasPermission('10.6.4')")
    @DeleteMapping("/api/cms/navigation/deleteNavigationBatch")
    public Result deleteNavigationBatch(@Validated(Navigation.DeleteBatch.class) @RequestBody Navigation navigation) {
        return Result.success().put("data", navigationService.deleteNavigationBatch(navigation));
    }

}
