package com.emotion.controller;

import com.emotion.common.PageResult;
import com.emotion.common.Result;
import com.emotion.dto.request.LifeEventCreateRequest;
import com.emotion.dto.request.LifeEventPageRequest;
import com.emotion.dto.request.LifeEventUpdateRequest;
import com.emotion.dto.response.LifeEventResponse;
import com.emotion.service.LifeEventService;
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-22
 */
@RestController
@RequestMapping("/lifeEvent")
public class LifeEventController {

    @Autowired
    private LifeEventService lifeEventService;

    /**
     * 分页查询当前用户的生命事件
     */
    @GetMapping(value = "/page")
    public Result<PageResult<LifeEventResponse>> getPage(@Validated LifeEventPageRequest request) {
        PageResult<LifeEventResponse> pageResult = lifeEventService.getPageByCurrentUser(request);
        return Result.success(pageResult);
    }

    /**
     * 获取当前用户的所有生命事件列表
     */
    @GetMapping(value = "/list")
    public Result<List<LifeEventResponse>> getList() {
        List<LifeEventResponse> events = lifeEventService.getListByCurrentUser();
        return Result.success(events);
    }

    /**
     * 根据ID获取生命事件详情
     */
    @GetMapping(value = "/detail")
    public Result<LifeEventResponse> getById(@RequestParam String id) {
        LifeEventResponse event = lifeEventService.getEventById(id);
        if (event == null) {
            return Result.notFound("生命事件不存在");
        }
        return Result.success(event);
    }

    /**
     * 创建生命事件
     */
    @PostMapping(value = "/create")
    public Result<LifeEventResponse> create(@Valid @RequestBody LifeEventCreateRequest request) {
        LifeEventResponse event = lifeEventService.createEvent(request);
        if (event == null) {
            return Result.error("创建失败");
        }
        return Result.success(event);
    }

    /**
     * 更新生命事件
     */
    @PutMapping(value = "/update")
    public Result<LifeEventResponse> update(@Valid @RequestBody LifeEventUpdateRequest request) {
        LifeEventResponse event = lifeEventService.updateEvent(request);
        if (event == null) {
            return Result.error("更新失败");
        }
        return Result.success(event);
    }

    /**
     * 删除生命事件
     */
    @DeleteMapping(value = "/delete")
    public Result<Void> delete(@RequestParam String id) {
        boolean deleted = lifeEventService.deleteEvent(id);
        if (!deleted) {
            return Result.error("删除失败");
        }
        return Result.success();
    }
}
