<?php

namespace App\Http\Controllers\Employee;

use App\Http\Controllers\Controller;
use App\Http\Requests\Employee\UpdateProfileRequest;
use App\Services\UserService;
use Illuminate\Http\Request;
use Inertia\Inertia;

class ProfileController extends Controller
{
    public function __construct(protected UserService $userService) {}

    public function edit(Request $request)
    {
        $user = $request->user()->load('employeeProfile');

        return Inertia::render('Employee/Pages/Profile', [
            'user' => $user,
        ]);
    }

    public function updateInfo(Request $request)
    {
        // Currently info is mostly disabled in UI, but if we add it:
        // $data = $request->validate([...]);
        // $this->userService->update($request->user(), $data);

        return redirect()->back()->with('success', 'Profile info updated successfully.');
    }

    public function updateImage(Request $request)
    {
        $request->validate([
            'profile_image' => ['required', 'image', 'max:2048'],
        ]);

        $this->userService->update($request->user(), [], $request->file('profile_image'));

        return redirect()->back()->with('success', 'Profile image updated successfully.');
    }

    public function updatePassword(UpdateProfileRequest $request)
    {
        $data = $request->validated();

        if (filled($data['password'] ?? null)) {
            $this->userService->update($request->user(), [
                'password' => $data['password'],
            ]);
        }

        return redirect()->back()->with('success', 'Password updated successfully.');
    }
}

