<?php

namespace App\Http\Controllers;

use App\Actions\Profile\UpdatePlayerProfileAction;
use App\Actions\Profile\UploadProfileImageAction;
use App\Http\Requests\Profile\UpdatePasswordRequest;
use App\Http\Requests\Profile\UpdatePhoneRequest;
use App\Http\Requests\Profile\UpdateProfileRequest;
use App\Http\Requests\Profile\UploadProfileImageRequest;
use App\Services\PhoneNormalizationService;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Hash;
use Illuminate\View\View;
use InvalidArgumentException;

class SettingsController extends Controller
{
    public function index(Request $request, string $tab = 'account'): View
    {
        abort_unless(in_array($tab, ['account', 'security', 'notifications'], true), 404);

        $user = $request->user();
        $profile = $user->playerProfile;

        if ($profile) {
            $this->authorize('update', $profile);
        }

        return view('pages.settings.index', [
            'tab' => $tab,
            'player' => [
                'username' => $user->username,
                'real_name' => $user->display_name,
                'bio' => $profile?->bio ?? '',
            ],
        ]);
    }

    public function updateProfile(UpdateProfileRequest $request, UpdatePlayerProfileAction $updateProfile): RedirectResponse
    {
        $profile = $request->user()->playerProfile;

        abort_if($profile === null, 404);

        $updateProfile->execute($profile, $request->validated());

        return back()->with('status', 'profile-updated');
    }

    public function updateProfileImage(
        UploadProfileImageRequest $request,
        UploadProfileImageAction $uploadProfileImage,
    ): RedirectResponse {
        $profile = $request->user()->playerProfile;

        abort_if($profile === null, 404);

        try {
            $uploadProfileImage->execute(
                $profile,
                $request->file('image'),
                $request->validated('type'),
            );
        } catch (InvalidArgumentException $exception) {
            return back()->withErrors(['image' => $exception->getMessage()]);
        }

        return back()->with('status', 'profile-image-updated');
    }

    public function updatePassword(UpdatePasswordRequest $request): RedirectResponse
    {
        $request->user()->update([
            'password' => Hash::make($request->validated('password')),
        ]);

        return back()->with('status', 'password-updated');
    }

    public function updatePhone(UpdatePhoneRequest $request, PhoneNormalizationService $phoneNormalizer): RedirectResponse
    {
        try {
            $phoneData = $phoneNormalizer->normalize(
                $request->validated('phone_country_code'),
                $request->validated('phone'),
            );

            if ($phoneData === null) {
                return back()->withErrors(['phone' => __('validation.phone_invalid')]);
            }

            $request->user()->update([
                ...$phoneData,
                'phone_verified_at' => null,
            ]);
        } catch (InvalidArgumentException $exception) {
            return back()->withErrors(['phone' => $exception->getMessage()]);
        }

        return back()->with('status', 'phone-updated');
    }
}
