package com.emotion.controller;

import com.emotion.common.PageResult;
import com.emotion.common.Result;
import com.emotion.dto.request.guest.GuestUserCreateRequest;
import com.emotion.dto.request.guest.GuestUserPageRequest;
import com.emotion.dto.request.guest.GuestUserUpdateRequest;
import com.emotion.dto.response.guest.GuestUserResponse;
import com.emotion.service.GuestUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * 访客用户控制器
 * 
 * @author huazhongmin
 * @date 2025-09-08
 */
@RestController
@RequestMapping("/guestUser")
public class GuestUserController {

    @Autowired
    private GuestUserService guestUserService;

    /**
     * 分页查询访客用户
     */
    @GetMapping(value = "/page")
    public Result<PageResult<GuestUserResponse>> getPage(@Validated GuestUserPageRequest request) {
        PageResult<GuestUserResponse> pageResult = guestUserService.getPageWithResponse(request);
        return Result.success(pageResult);
    }

    /**
     * 根据ID获取访客用户
     */
    @GetMapping(value = "/detail")
    public Result<GuestUserResponse> getById(@RequestParam String id) {
        GuestUserResponse response = guestUserService.getGuestUserResponseById(id);
        if (response == null) {
            return Result.notFound("访客用户不存在");
        }
        return Result.success(response);
    }

    /**
     * 创建访客用户
     */
    @PostMapping(value = "/create")
    public Result<GuestUserResponse> create(@Valid @RequestBody GuestUserCreateRequest request) {
        GuestUserResponse response = guestUserService.createGuestUserWithResponse(request);
        return Result.success(response);
    }

    /**
     * 更新访客用户
     */
    @PutMapping(value = "/update")
    public Result<GuestUserResponse> update(@Valid @RequestBody GuestUserUpdateRequest request) {
        GuestUserResponse response = guestUserService.updateGuestUserWithResponse(request);
        if (response == null) {
            return Result.notFound("访客用户不存在");
        }
        return Result.success(response);
    }

    /**
     * 删除访客用户
     */
    @DeleteMapping(value = "/delete")
    public Result<Void> delete(@RequestParam String id) {
        boolean deleted = guestUserService.deleteGuestUser(id);
        if (!deleted) {
            return Result.error("删除失败");
        }
        return Result.success();
    }
}