package com.kt.event.user.controller;

import com.kt.event.common.security.UserPrincipal;
import com.kt.event.user.dto.request.ChangePasswordRequest;
import com.kt.event.user.dto.request.LoginRequest;
import com.kt.event.user.dto.request.RegisterRequest;
import com.kt.event.user.dto.request.UpdateProfileRequest;
import com.kt.event.user.dto.response.LoginResponse;
import com.kt.event.user.dto.response.LogoutResponse;
import com.kt.event.user.dto.response.ProfileResponse;
import com.kt.event.user.dto.response.RegisterResponse;
import com.kt.event.user.service.AuthenticationService;
import com.kt.event.user.service.UserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.*;

import java.util.UUID;

/**
 * User Controller
 *
 * 사용자 인증 및 프로필 관리 API
 *
 * @author Backend Developer
 * @since 1.0
 */
@Slf4j
@RestController
@RequestMapping("/api/v1/users")
@RequiredArgsConstructor
@Tag(name = "User", description = "사용자 인증 및 프로필 관리 API")
public class UserController {

    private final UserService userService;
    private final AuthenticationService authenticationService;

    /**
     * 회원가입
     *
     * UFR-USER-010: 회원가입
     */
    @PostMapping("/register")
    @Operation(summary = "회원가입", description = "소상공인 회원가입 API")
    public ResponseEntity<RegisterResponse> register(@Valid @RequestBody RegisterRequest request) {
        log.info("회원가입 요청: phoneNumber={}, email={}", request.getPhoneNumber(), request.getEmail());
        RegisterResponse response = userService.register(request);
        log.info("회원가입 성공: userId={}", response.getUserId());
        return ResponseEntity.status(HttpStatus.CREATED).body(response);
    }

    /**
     * 로그인
     *
     * UFR-USER-020: 로그인
     */
    @PostMapping("/login")
    @Operation(summary = "로그인", description = "소상공인 로그인 API")
    public ResponseEntity<LoginResponse> login(@Valid @RequestBody LoginRequest request) {
        log.info("로그인 요청: email={}", request.getEmail());
        LoginResponse response = authenticationService.login(request);
        log.info("로그인 성공: userId={}", response.getUserId());
        return ResponseEntity.ok(response);
    }

    /**
     * 로그아웃
     *
     * UFR-USER-040: 로그아웃
     */
    @PostMapping("/logout")
    @Operation(summary = "로그아웃", description = "로그아웃 API")
    public ResponseEntity<LogoutResponse> logout(@RequestHeader("Authorization") String authHeader) {
        String token = authHeader.substring(7); // "Bearer " 제거
        log.info("로그아웃 요청");
        LogoutResponse response = authenticationService.logout(token);
        log.info("로그아웃 성공");
        return ResponseEntity.ok(response);
    }

    /**
     * 프로필 조회
     *
     * UFR-USER-030: 프로필 관리
     */
    @GetMapping("/profile")
    @Operation(summary = "프로필 조회", description = "사용자 프로필 조회 API")
    public ResponseEntity<ProfileResponse> getProfile(@AuthenticationPrincipal UserPrincipal principal) {
        UUID userId = principal.getUserId();
        log.info("프로필 조회 요청: userId={}", userId);
        ProfileResponse response = userService.getProfile(userId);
        return ResponseEntity.ok(response);
    }

    /**
     * 프로필 수정
     *
     * UFR-USER-030: 프로필 관리
     */
    @PutMapping("/profile")
    @Operation(summary = "프로필 수정", description = "사용자 프로필 수정 API")
    public ResponseEntity<ProfileResponse> updateProfile(
            @AuthenticationPrincipal UserPrincipal principal,
            @Valid @RequestBody UpdateProfileRequest request) {
        UUID userId = principal.getUserId();
        log.info("프로필 수정 요청: userId={}", userId);
        ProfileResponse response = userService.updateProfile(userId, request);
        log.info("프로필 수정 성공: userId={}", userId);
        return ResponseEntity.ok(response);
    }

    /**
     * 비밀번호 변경
     *
     * UFR-USER-030: 프로필 관리
     */
    @PutMapping("/password")
    @Operation(summary = "비밀번호 변경", description = "비밀번호 변경 API")
    public ResponseEntity<Void> changePassword(
            @AuthenticationPrincipal UserPrincipal principal,
            @Valid @RequestBody ChangePasswordRequest request) {
        UUID userId = principal.getUserId();
        log.info("비밀번호 변경 요청: userId={}", userId);
        userService.changePassword(userId, request);
        log.info("비밀번호 변경 성공: userId={}", userId);
        return ResponseEntity.ok().build();
    }
}
