package de.oaa.xxx.registration;

import java.net.URI;

import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/activation")
public class ActivationController {

    private final RegistrationService registrationService;

    public ActivationController(RegistrationService registrationService) {
        this.registrationService = registrationService;
    }

    @GetMapping("/{uuid}")
    public ResponseEntity<Void> activate(@PathVariable("uuid") String uuid) {
        try {
            String email = registrationService.activate(uuid);
            String redirect = "/login.html?email=" + java.net.URLEncoder.encode(email, java.nio.charset.StandardCharsets.UTF_8);
            return ResponseEntity.status(302).location(URI.create(redirect)).build();
        } catch (IllegalStateException e) {
            // Bereits aktiviert → trotzdem zum Login weiterleiten (idempotent)
            return ResponseEntity.noContent().build();
        } catch (IllegalArgumentException e) {
            return ResponseEntity.noContent().build();
        } catch (Exception e) {
            return ResponseEntity.internalServerError().build();
        }
    }
}
