package org.xyzh.system.controller;

import java.util.Arrays;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.xyzh.api.system.service.GuestService;
import org.xyzh.common.core.domain.ResultDomain;
import org.xyzh.common.core.page.PageRequest;
import org.xyzh.common.dto.sys.TbGuestDTO;
import org.xyzh.common.utils.validation.ValidationUtils;

import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;

/**
 * @description    来客控制器
 * @filename    GuestController.java
 * @author    yslg
 * @copyright xyzh
 * @since 2025-12-18
 */
@Validated
@RestController
@RequestMapping("/system/guest")
public class GuestController {

    @Autowired
    private GuestService guestService;


    @PostMapping
    public ResultDomain<TbGuestDTO> createGuest(TbGuestDTO guest) {
        ValidationUtils.validate(guest, Arrays.asList(
            ValidationUtils.requiredString("name", "姓名"),
            ValidationUtils.atLeastOne(
                Arrays.asList("phone", "email", "wechatId"),
                Arrays.asList("电话", "邮箱", "微信ID")
            )
        ));
        return guestService.createGuest(guest);
    }

    @PutMapping
    public ResultDomain<TbGuestDTO> updateGuest(TbGuestDTO guest) {
        ValidationUtils.validate(guest, Arrays.asList(
            ValidationUtils.requiredString("name", "姓名"),
            ValidationUtils.atLeastOne(
                Arrays.asList("phone", "email", "wechatId"),
                Arrays.asList("电话", "邮箱", "微信ID")
            )
        ));
        return guestService.updateGuest(guest);
    }

    @DeleteMapping
    public ResultDomain<TbGuestDTO> deleteGuest(@NotNull String userId) {
        return guestService.deleteGuest(userId);
    }

    @GetMapping("/wechat/{wechatId}")
    public ResultDomain<TbGuestDTO> selectGuestByWechat(@PathVariable("wechatId") @NotBlank String wechatId){
        return guestService.selectGuestByWechatId(wechatId);
    }


    @GetMapping("/list")
    public ResultDomain<TbGuestDTO> listGuest(TbGuestDTO filter) {
        return guestService.selectGuestList(filter);
    }

    @PostMapping("/page")
    public ResultDomain<TbGuestDTO> pageGuest(PageRequest<TbGuestDTO> pageRequest) {
        return guestService.selectGuestPage(pageRequest);
    }

    
}
