package com.seer.teach.mp.app.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.stp.StpUtil;
import com.seer.teach.common.PageListBean;
import com.seer.teach.common.ResultBean;
import com.seer.teach.common.annotation.LogPrint;
import com.seer.teach.common.enums.ResultCodeEnum;
import com.seer.teach.mp.app.controller.req.AppAgentEmployeeRelationQueryReq;
import com.seer.teach.mp.app.controller.req.AppAgentEmployeeRelationReq;
import com.seer.teach.mp.app.controller.resp.AppAgentEmployeeRelationResp;
import com.seer.teach.mp.app.service.IAppAgentEmployeeRelationService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@LogPrint
@RequiredArgsConstructor
@RestController
@Tag(name = "App - 代理商员工管理")
@RequestMapping("/app/agent/employee")
public class AppAgentEmployeeRelationController {

    private final IAppAgentEmployeeRelationService agentEmployeeRelationService;

    @Operation(summary = "我的员工列表")
    @PostMapping("/page-list")
    @SaCheckLogin
    public ResultBean<PageListBean<AppAgentEmployeeRelationResp>> pageList(@RequestBody @Validated AppAgentEmployeeRelationQueryReq query) {
        Integer agentId = StpUtil.getLoginIdAsInt();
        query.setAgentId(agentId);
        return ResultBean.success(agentEmployeeRelationService.pageList(query));
    }

    @Operation(summary = "新增员工")
    @PostMapping("/add")
    @SaCheckLogin
    public ResultBean<Boolean> addEmployee(@RequestBody @Validated AppAgentEmployeeRelationReq request) {
        Integer agentId = StpUtil.getLoginIdAsInt();
        request.setAgentId(agentId);
        return ResultBean.success(agentEmployeeRelationService.addEmployee(request));
    }

    @Operation(summary = "更新员工信息")
    @PostMapping("/update")
    @SaCheckLogin
    public ResultBean<Boolean> updateEmployee(@RequestBody @Validated AppAgentEmployeeRelationReq request) {
        Integer agentId = StpUtil.getLoginIdAsInt();
        boolean belongsToAgent = agentEmployeeRelationService.isEmployeeBelongsToAgent(request.getId(), agentId);
        if (!belongsToAgent) {
            return ResultBean.error(ResultCodeEnum.UNAUTHORIZED_UPDATE_EMPLOYEE);
        }
        return ResultBean.success(agentEmployeeRelationService.updateEmployee(request));
    }

    @Operation(summary = "删除员工")
    @DeleteMapping("/{id}")
    @SaCheckLogin
    public ResultBean<Boolean> deleteEmployee(@PathVariable Integer id) {
        Integer agentId = StpUtil.getLoginIdAsInt();
        boolean belongsToAgent = agentEmployeeRelationService.isEmployeeBelongsToAgent(id, agentId);
        if (!belongsToAgent) {
            return ResultBean.error(ResultCodeEnum.UNAUTHORIZED_UPDATE_EMPLOYEE);
        }
        return ResultBean.success(agentEmployeeRelationService.deleteEmployee(id));
    }

    @Operation(summary = "员工详情")
    @GetMapping("/{id}")
    @SaCheckLogin
    public ResultBean<AppAgentEmployeeRelationResp> getEmployee(@PathVariable Integer id) {
        Integer agentId = StpUtil.getLoginIdAsInt();
        boolean belongsToAgent = agentEmployeeRelationService.isEmployeeBelongsToAgent(id, agentId);
        if (!belongsToAgent) {
            return ResultBean.error(ResultCodeEnum.UNAUTHORIZED_UPDATE_EMPLOYEE);
        }
        AppAgentEmployeeRelationResp result = agentEmployeeRelationService.getById(id);
        return ResultBean.success(result);
    }

    @Operation(summary = "获取所有员工列表")
    @GetMapping("/all")
    @SaCheckLogin
    public ResultBean<List<AppAgentEmployeeRelationResp>> getAllEmployees() {
        Integer agentId = StpUtil.getLoginIdAsInt();
        return ResultBean.success(agentEmployeeRelationService.getAllEmployeesByAgentId(agentId));
    }
}