<?php

namespace App\Http\Controllers;

use App\Http\Requests\ChangePasswordRequest;
use App\Http\Requests\UpdateProfileRequest;
use App\Services\ProfileService;
use Illuminate\Http\JsonResponse;
use Illuminate\View\View;

class ProfileController extends Controller
{
    public function __construct(
        private readonly ProfileService $profileService
    ) {}

    /**
     * 프로필 페이지
     */
    public function index(): View
    {
        return view('profile.index');
    }

    /**
     * 프로필 정보 수정
     */
    public function update(UpdateProfileRequest $request): JsonResponse
    {
        $user = auth()->user();
        $result = $this->profileService->updateProfile($user, $request->validated());

        if ($result) {
            return response()->json([
                'success' => true,
                'message' => '프로필이 수정되었습니다.',
            ]);
        }

        return response()->json([
            'success' => false,
            'message' => '프로필 수정에 실패했습니다.',
        ], 500);
    }

    /**
     * 비밀번호 변경
     */
    public function changePassword(ChangePasswordRequest $request): JsonResponse
    {
        $user = auth()->user();
        $result = $this->profileService->changePassword(
            $user,
            $request->current_password,
            $request->password
        );

        return response()->json($result, $result['success'] ? 200 : 422);
    }
}
