package com.expensetracker.api.auth.controller;

import com.expensetracker.api.auth.dto.AuthResponse;
import com.expensetracker.api.auth.dto.LoginRequest;
import com.expensetracker.api.auth.dto.RefreshTokenRequest;
import com.expensetracker.api.auth.dto.RegisterRequest;
import com.expensetracker.api.auth.service.AuthService;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
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("/auth")
@RequiredArgsConstructor
public class AuthController {

  private final AuthService auth;

  @PostMapping("/register")
  public ResponseEntity<Void> register(@Valid @RequestBody RegisterRequest req) {
    auth.register(req);
    return ResponseEntity.ok().build();
  }

  @PostMapping("/login")
  public AuthResponse login(@Valid @RequestBody LoginRequest req) {
    return auth.login(req);
  }

  @PostMapping("/refresh")
  public AuthResponse refresh(@Valid @RequestBody RefreshTokenRequest req) {
    return auth.refresh(req);
  }

  @PostMapping("/logout")
  public ResponseEntity<Void> logout(@Valid @RequestBody RefreshTokenRequest req) {
    auth.logout(req);
    return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
  }
}
