package com.test.bijihoudaun.controller;

import com.test.bijihoudaun.common.response.R;
import com.test.bijihoudaun.entity.User;
import com.test.bijihoudaun.service.UserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@Tag(name = "用户接口")
@RestController
@RequestMapping("/api/user")
public class UserController {
    @Autowired
    private UserService userService;

    @Operation(summary = "用户注册")
    @Parameters({
            @Parameter(name = "username", description = "用户名",required = true),
            @Parameter(name = "password", description = "密码",required = true),
            @Parameter(name = "email", description = "邮箱",required = true)
    })
    @PostMapping("/register")
    public R<User> register(String username, String password, String email){
        return R.success(userService.register(username,password,email));
    }

    @Operation(summary = "用户登录")
    @Parameters({
            @Parameter(name = "username", description = "用户名",required = true),
            @Parameter(name = "password", description = "密码",required = true)
    })
    @PostMapping("/login")
    public R<Map<String, String>> login(String username, String password){
        String token = userService.login(username, password);
        Map<String, String> tokenMap = new HashMap<>();
        tokenMap.put("token", token);
        return R.success(tokenMap);
    }

    @Operation(summary = "用户删除")
    @Parameters({
            @Parameter(name = "id", description = "用户id",required = true)
    })
    @DeleteMapping("/deleteUser")
    public R<String> deleteUser(Integer id){
        userService.deleteUser(id);
        return R.success("删除成功");
    }



}
