package com.rtlabs.signup.api;

import jakarta.validation.constraints.Pattern;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/api/v1/signup")
public class SignupController {

  private final SignupService signupService;

  public SignupController(SignupService signupService) {
    this.signupService = signupService;
  }

  @PostMapping
  public ResponseEntity<String> signup(@RequestBody SignupRequest request) {
    signupService.register(request.username(), request.email(), request.password());
    return ResponseEntity.ok("created");
  }

  public record SignupRequest(
      @Pattern(regexp = "^(\\w+\\.?)+$", message = "username may only use letters/digits")
      String username,
      @Pattern(regexp = "^([\\w\\.-]+)+@[a-z0-9.-]+\\.[a-z]{2,6}$", message = "email invalid")
      String email,
      String password
  ) {}
}
