package com.lesingle.modules.device.controller;

import com.lesingle.common.annotation.RateLimit;
import com.lesingle.common.result.PageResult;
import com.lesingle.common.result.Result;
import com.lesingle.common.util.SecurityUtil;
import com.lesingle.modules.device.vo.DeviceLoginVo;
import com.lesingle.modules.device.vo.DeviceWorkDetailVo;
import com.lesingle.modules.device.vo.DeviceWorkItemVo;
import com.lesingle.modules.pub.dto.PublicSmsLoginDto;
import com.lesingle.modules.pub.dto.SendSmsCodeDto;
import com.lesingle.modules.pub.service.PublicAuthService;
import com.lesingle.modules.pub.service.PublicUserWorkService;
import com.lesingle.modules.pub.service.SmsCodeService;
import com.lesingle.modules.ugc.entity.UgcWork;
import com.lesingle.security.annotation.Public;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 终端设备接口
 * 为终端设备提供独立的认证和作品查询接口
 */
@Tag(name = "终端设备接口")
@RestController
@RequestMapping("/device")
@RequiredArgsConstructor
public class DeviceController {

    private final PublicAuthService publicAuthService;
    private final SmsCodeService smsCodeService;
    private final PublicUserWorkService publicUserWorkService;

    @Public
    @PostMapping("/auth/sms/send")
    @RateLimit(permits = 1, duration = 60)
    @Operation(summary = "终端设备-发送短信验证码")
    public Result<Void> sendSmsCode(@Valid @RequestBody SendSmsCodeDto dto) {
        smsCodeService.sendCode(dto.getPhone());
        return Result.success(null);
    }

    @Public
    @PostMapping("/auth/login/sms")
    @RateLimit(permits = 5, duration = 1)
    @Operation(summary = "终端设备-手机验证码登录")
    public Result<DeviceLoginVo> loginBySms(@Valid @RequestBody PublicSmsLoginDto dto) {
        Map<String, Object> authResult = publicAuthService.loginBySms(dto);
        return Result.success(DeviceLoginVo.fromAuthResult(authResult));
    }

    @GetMapping("/works")
    @Operation(summary = "终端设备-用户作品列表")
    public Result<PageResult<DeviceWorkItemVo>> listWorks(
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "10") int pageSize,
            @RequestParam(required = false) String status,
            @RequestParam(required = false) String keyword) {
        Long userId = SecurityUtil.getCurrentUserId();
        PageResult<UgcWork> workPage = publicUserWorkService.findMyWorks(userId, page, pageSize, status, keyword);
        List<DeviceWorkItemVo> voList = workPage.getList().stream()
                .map(DeviceWorkItemVo::fromEntity)
                .collect(Collectors.toList());
        return Result.success(new PageResult<>(voList, workPage.getTotal(), workPage.getPage(), workPage.getPageSize()));
    }

    @GetMapping("/works/{id}")
    @Operation(summary = "终端设备-作品详情")
    public Result<DeviceWorkDetailVo> getWorkDetail(@PathVariable Long id) {
        Long userId = SecurityUtil.getCurrentUserId();
        Map<String, Object> detailMap = publicUserWorkService.findDetail(id, userId);
        return Result.success(DeviceWorkDetailVo.fromDetailMap(detailMap));
    }
}
