package com.emotion.controller;

import com.emotion.common.PageResult;
import com.emotion.common.Result;
import com.emotion.dto.request.userprofile.UserProfileCreateRequest;
import com.emotion.dto.request.userprofile.UserProfilePageRequest;
import com.emotion.dto.request.userprofile.UserProfileUpdateRequest;
import com.emotion.dto.response.userprofile.UserProfileResponse;
import com.emotion.service.UserProfileService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * 用户档案控制器
 *
 * @author huazhongmin
 * @date 2025-12-21
 */
@RestController
@RequestMapping("/user-profile")
public class UserProfileController {

    @Autowired
    private UserProfileService userProfileService;

    /**
     * 新增档案
     */
    @PostMapping("/create")
    public Result<UserProfileResponse> create(@Valid @RequestBody UserProfileCreateRequest request) {
        UserProfileResponse response = userProfileService.createProfile(request);
        return Result.success(response);
    }

    /**
     * 删除档案
     */
    @DeleteMapping("/delete")
    public Result<Void> delete(@RequestParam String id) {
        boolean success = userProfileService.deleteProfile(id);
        if (success) {
            return Result.success();
        } else {
            return Result.error("删除失败");
        }
    }

    /**
     * 修改档案
     */
    @PutMapping("/update")
    public Result<UserProfileResponse> update(@Valid @RequestBody UserProfileUpdateRequest request) {
        UserProfileResponse response = userProfileService.updateProfile(request);
        return Result.success(response);
    }

    /**
     * 根据ID查询详情
     */
    @GetMapping("/detail")
    public Result<UserProfileResponse> getById(@RequestParam String id) {
        UserProfileResponse response = userProfileService.getProfileById(id);
        if (response == null) {
            return Result.notFound("档案不存在");
        }
        return Result.success(response);
    }

    /**
     * 获取当前登录用户的档案
     */
    @GetMapping("/me")
    public Result<UserProfileResponse> getCurrentProfile() {
        UserProfileResponse response = userProfileService.getCurrentUserProfile();
        // 如果不存在，返回null data，不报错
        return Result.success(response);
    }

    /**
     * 分页查询
     */
    @GetMapping("/page")
    public Result<PageResult<UserProfileResponse>> getPage(@Validated UserProfilePageRequest request) {
        PageResult<UserProfileResponse> pageResult = userProfileService.getProfilePage(request);
        return Result.success(pageResult);
    }

    /**
     * 列表查询
     */
    @GetMapping("/list")
    public Result<List<UserProfileResponse>> getList(@Validated UserProfilePageRequest request) {
        List<UserProfileResponse> list = userProfileService.getProfileList(request);
        return Result.success(list);
    }

    /**
     * 迁移用户档案中的生命事件数据到t_life_event表
     * 将childhood、peak、valley数据迁移到生命事件表
     * 注意：此接口为一次性数据迁移接口，迁移完成后可删除
     */
    @PostMapping("/migrateLifeEvents")
    public Result<Integer> migrateLifeEvents() {
        int count = userProfileService.migrateLifeEventsFromProfiles();
        return Result.success(count);
    }
}
