package top.continew.admin.controller.business;

import com.alibaba.fastjson.JSONObject;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import top.continew.admin.business.model.query.SignInQuery;
import top.continew.admin.business.model.req.SignInAddReq;
import top.continew.admin.business.model.req.SignInEditReq;
import top.continew.admin.business.model.resp.SignInResp;
import top.continew.admin.business.service.SignInService;
import top.continew.starter.extension.crud.model.query.PageQuery;
import top.continew.starter.extension.crud.model.query.SortQuery;
import top.continew.starter.extension.crud.model.resp.PageResp;
import top.continew.starter.web.model.R;

import java.util.List;
import java.util.Map;

/**
 * 签到管理 API
 *
 * @author jiay
 * @since 2025/06/26 10:24
 */
@Tag(name = "签到 API")
@RestController
@RequiredArgsConstructor
@RequestMapping(value = "/signIn")
public class SignInController {
    private final SignInService signInService;

    @Operation(summary = "添加签到", description = "添加签到")
    @PostMapping("/add")
    public R<Void> add(@RequestBody Map data) {
        try {
            Map obj = (Map)((Map)((Map)data.get("data")).get("ref_data")).get("fields");
            String course = (String)obj.get("课程名称");
            String no = (String)obj.get("学号");
            String name = (String)obj.get("姓名");
            String phone = (String)obj.get("手机");
            String location = JSONObject.toJSONString(obj.get("定位"));

            SignInAddReq req = new SignInAddReq();
            req.setSignIn(1);
            req.setCourseName(course);
            req.setLocation(location);
            req.setUserName(name);
            req.setPhone(phone);
            req.setStuNo(no);
            return R.ok(signInService.addSignIn(req));
        } catch (RuntimeException exception) {
            exception.printStackTrace();
        }
        return R.fail();
    }

    @Operation(summary = "编辑签到", description = "编辑签到")
    @PostMapping("/edit")
    public R<Void> edit(@RequestBody @Valid SignInEditReq req) {
        return signInService.editSignIn(req) ? R.ok() : R.fail();
    }

    @Operation(summary = "删除签到", description = "删除签到")
    @DeleteMapping("/delete")
    public R<Void> delete(@RequestParam Long id) {
        return signInService.deleteSignIn(id) ? R.ok() : R.fail();
    }

    @Operation(summary = "获取签到", description = "获取签到")
    @GetMapping("/get")
    public R<SignInResp> getSignIn(@RequestParam(name = "id", required = true) Long id) {
        return R.ok(signInService.getSignIn(id));
    }

    @Operation(summary = "查询今日未签到列表", description = "查询签到列表")
    @PutMapping("/getTodayNosignInList")
    public R<List<SignInResp>> getTodayNosignInList(SignInQuery query) {
        return R.ok(signInService.getTodayNosignInList(query));
    }

    @Operation(summary = "分页查询历史签到列表", description = "分页查询历史签到列表")
    @PutMapping("/getHisSignInList")
    public R<PageResp<SignInResp>> getHisSignInList(SignInQuery query, PageQuery pageQuery) {
        return R.ok(signInService.getHisSignInList(query, pageQuery));
    }

    @Operation(summary = "导出历史签到列表", description = "导出历史签到列表")
    @GetMapping("/exportHisSignInList")
    public void exportHisSignInList(SignInQuery query, SortQuery sortQuery, HttpServletResponse response) {
        signInService.exportHisSignInList(query, sortQuery, response);
    }

    @Operation(summary = "导出今日未签到列表", description = "导出今日未签到列表")
    @GetMapping("/exportTodayNosignInList")
    public void exportTodayNosignInList(SignInQuery query, SortQuery sortQuery, HttpServletResponse response) {
        signInService.exportTodayNosignInList(query, sortQuery, response);
    }
}