package com.kingdee.eas.custom.integration.beisen.controller;

import com.kingdee.eas.custom.integration.base.annotation.ApiDoc;
import com.kingdee.eas.custom.integration.base.annotation.SkipAuth;
import com.kingdee.eas.custom.integration.base.annotation.SkipCallLog;
import com.kingdee.eas.custom.integration.base.controller.AbstractIntegrationController;
import com.kingdee.eas.custom.integration.base.dto.ApiResult;
import com.kingdee.eas.custom.integration.beisen.dto.AttendanceResponse;
import com.kingdee.eas.custom.integration.beisen.dto.BatchAttendanceRequest;
import com.kingdee.eas.custom.integration.beisen.service.AttendanceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.LinkedHashMap;
import java.util.Map;

/**
 * 北森考勤数据同步接口。
 * 访问路径: /beisen/attendance/*
 * 无需登录认证（供北森系统回调）。
 */
@RestController
@RequestMapping("/beisen/attendance")
public class BeisenAttendanceController extends AbstractIntegrationController {

    @Autowired
    private AttendanceService attendanceService;

    /**
     * 健康检查接口。
     */
    @SkipAuth
    @SkipCallLog
    @RequestMapping("/health")
    @ApiDoc(name = "健康检查接口", desc = "服务探活接口")
    public Map<String, Object> health() {
        Map<String, Object> result = new LinkedHashMap<>();
        result.put("status", "UP");
        result.put("service", "北森考勤数据同步接口");
        result.put("timestamp", System.currentTimeMillis());
        return result;
    }

    /**
     * 考勤日报批量写入接口。
     * 单次上限 200 条，超出抛 IntegrationException。
     */
    @PostMapping("/daily")
    @ApiDoc(name = "考勤日报批量写入", desc = "接收北森系统推送的考勤日报数据，支持批量写入")
    public ApiResult<AttendanceResponse> writeDaily(@RequestBody BatchAttendanceRequest request) {
        requireNotEmpty(request.getRecords(), "records");
        requireTrue(request.getRecords().size() <= 200,
                "单次写入记录数不能超过200条，当前: " + request.getRecords().size());

        log.info("收到考勤日报批量写入请求 - 记录数: {}", request.getRecords().size());

        AttendanceResponse response = attendanceService.writeDailyAttendance(request);
        return success(response);
    }

    /**
     * 考勤月报批量写入接口。
     * 单次上限 200 条，超出抛 IntegrationException。
     */
    @PostMapping("/monthly")
    @ApiDoc(name = "考勤月报批量写入", desc = "接收北森系统推送的考勤月报数据，支持批量写入")
    public ApiResult<AttendanceResponse> writeMonthly(@RequestBody BatchAttendanceRequest request) {
        requireNotEmpty(request.getRecords(), "records");
        requireTrue(request.getRecords().size() <= 200,
                "单次写入记录数不能超过200条，当前: " + request.getRecords().size());

        log.info("收到考勤月报批量写入请求 - 记录数: {}",
                request.getRecords() != null ? request.getRecords().size() : 0);

        AttendanceResponse response = attendanceService.writeMonthlyAttendance(request);
        return success(response);
    }
}
