package com.yimeng.okr.controller;

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

import java.util.List;

@RestController
@RequestMapping("/api/kpi")
@RequiredArgsConstructor
public class KpiController {

    private final KpiService kpiService;

    // Templates

    @GetMapping("/templates")
    public ApiResult<List<KpiTemplate>> listTemplates() {
        return ApiResult.success(kpiService.listTemplates());
    }

    @PostMapping("/templates")
    @OperationLogAnnotation(action = "CREATE_KPI_TEMPLATE", entityType = "kpi_template")
    public ApiResult<KpiTemplate> createTemplate(@RequestBody KpiTemplate template) {
        if (!SecurityUtils.hasRole(UserRole.HR_ADMIN)) {
            throw new BusinessException(403, "无权限，需要HR管理员或以上权限");
        }
        return ApiResult.success(kpiService.createTemplate(template));
    }

    @PutMapping("/templates/{id}")
    @OperationLogAnnotation(action = "UPDATE_KPI_TEMPLATE", entityType = "kpi_template")
    public ApiResult<Void> updateTemplate(@PathVariable Long id, @RequestBody KpiTemplate template) {
        if (!SecurityUtils.hasRole(UserRole.HR_ADMIN)) {
            throw new BusinessException(403, "无权限，需要HR管理员或以上权限");
        }
        template.setId(id);
        kpiService.updateTemplate(template);
        return ApiResult.success();
    }

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

    // Configs

    @GetMapping("/configs/{periodId}/{userId}")
    public ApiResult<KpiConfig> getConfig(@PathVariable Long periodId, @PathVariable Long userId) {
        return ApiResult.success(kpiService.getConfig(periodId, userId, SecurityUtils.getCurrentUserId()));
    }

    @PostMapping("/configs")
    @OperationLogAnnotation(action = "SAVE_KPI_CONFIG", entityType = "kpi_config")
    public ApiResult<KpiConfig> saveConfig(@RequestBody KpiConfig config) {
        return ApiResult.success(kpiService.saveConfig(config, SecurityUtils.getCurrentUserId()));
    }

    @PutMapping("/configs/{id}/lock")
    @OperationLogAnnotation(action = "LOCK_KPI_CONFIG", entityType = "kpi_config")
    public ApiResult<Void> lockConfig(@PathVariable Long id) {
        kpiService.lockConfig(id, SecurityUtils.getCurrentUserId());
        return ApiResult.success();
    }

    @GetMapping("/configs/period/{periodId}")
    public ApiResult<List<KpiConfig>> getConfigsByPeriod(@PathVariable Long periodId) {
        return ApiResult.success(kpiService.getConfigsByPeriod(periodId, SecurityUtils.getCurrentUserId()));
    }

    @GetMapping("/templates/by-position")
    public ApiResult<KpiTemplate> getTemplateByPosition(@RequestParam String position) {
        return ApiResult.success(kpiService.getTemplateByPosition(position));
    }

    @GetMapping("/templates/names")
    public ApiResult<List<String>> getTemplateNames() {
        return ApiResult.success(kpiService.listTemplates().stream().map(KpiTemplate::getName).toList());
    }
}
