package com.yimeng.okr.controller;

import com.yimeng.okr.dto.ApiResult;
import com.yimeng.okr.annotation.OperationLogAnnotation;
import com.yimeng.okr.entity.AssessmentDimension;
import com.yimeng.okr.enums.UserRole;
import com.yimeng.okr.exception.BusinessException;
import com.yimeng.okr.security.SecurityUtils;
import com.yimeng.okr.service.DimensionService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/dimensions")
@RequiredArgsConstructor
public class DimensionController {

    private final DimensionService service;

    @GetMapping
    public ApiResult<List<AssessmentDimension>> list() {
        return ApiResult.success(service.listAll());
    }

    @PostMapping
    @OperationLogAnnotation(action = "CREATE_DIMENSION", entityType = "assessment_dimension")
    public ApiResult<AssessmentDimension> create(@RequestBody AssessmentDimension dim) {
        if (!SecurityUtils.hasRole(UserRole.HR_ADMIN)) {
            throw new BusinessException(403, "无权限，需要HR管理员或以上权限");
        }
        return ApiResult.success(service.create(dim));
    }

    @PutMapping("/{id}")
    @OperationLogAnnotation(action = "UPDATE_DIMENSION", entityType = "assessment_dimension")
    public ApiResult<Void> update(@PathVariable Long id, @RequestBody AssessmentDimension dim) {
        if (!SecurityUtils.hasRole(UserRole.HR_ADMIN)) {
            throw new BusinessException(403, "无权限，需要HR管理员或以上权限");
        }
        dim.setId(id);
        service.update(dim);
        return ApiResult.success();
    }

    @DeleteMapping("/{id}")
    @OperationLogAnnotation(action = "DELETE_DIMENSION", entityType = "assessment_dimension")
    public ApiResult<Void> delete(@PathVariable Long id) {
        if (!SecurityUtils.hasRole(UserRole.HR_ADMIN)) {
            throw new BusinessException(403, "无权限，需要HR管理员或以上权限");
        }
        service.delete(id);
        return ApiResult.success();
    }
}
