package com.seer.teach.user.admin.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.seer.teach.common.ResultBean;
import com.seer.teach.user.convert.HomeLayoutConvert;
import com.seer.teach.user.entity.HomeLayoutEntity;
import com.seer.teach.user.request.HomeLayoutReq;
import com.seer.teach.user.response.HomeLayoutResp;
import com.seer.teach.user.service.IHomeLayoutService;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Tag(name = "管理端 - 首页布局")
@Validated
@RequiredArgsConstructor
@RequestMapping("/sys/home/layout")
public class AdminHomeLayoutController {

    private final IHomeLayoutService homeLayoutService;

    @Operation(summary = "获取首页布局")
    @GetMapping("/list")
    @SaCheckPermission("admin:sys:layout:list")
    public ResultBean<List<HomeLayoutResp>> getHomeLayoutList() {
        return ResultBean.success(HomeLayoutConvert.INSTANCE.convertList(homeLayoutService.list()));
    }

    @Operation(summary = "获取首页布局")
    @GetMapping("/detail")
    @SaCheckPermission("admin:sys:layout:detail")
    public ResultBean<HomeLayoutResp> getHomeLayoutDetail(@RequestParam("id") Long id) {
        return ResultBean.success(HomeLayoutConvert.INSTANCE.convertOne(homeLayoutService.getById(id)));
    }
    
    @Operation(summary = "创建首页布局")
    @PostMapping("/create")
    @SaCheckPermission("admin:sys:layout:create")
    public ResultBean<Boolean> createHomeLayout(@RequestBody @Validated HomeLayoutReq req) {
        HomeLayoutEntity homeLayout =HomeLayoutConvert.INSTANCE.convertOne2Entity(req);
        return ResultBean.success(homeLayoutService.save(homeLayout));
    }
    
    @Operation(summary = "更新首页布局")
    @PutMapping("/update")
    @SaCheckPermission("admin:sys:layout:update")
    public ResultBean<Boolean> updateHomeLayout(@RequestBody @Validated HomeLayoutReq req) {
        HomeLayoutEntity homeLayout =HomeLayoutConvert.INSTANCE.convertOne2Entity(req);
        return ResultBean.success(homeLayoutService.updateById(homeLayout));
    }
    
    @Operation(summary = "删除首页布局")
    @DeleteMapping("/delete")
    @SaCheckPermission("admin:sys:layout:delete")
    public ResultBean<Boolean> deleteHomeLayout(@RequestParam("id") Long id) {
        return ResultBean.success(homeLayoutService.removeById(id));
    }
}