<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Facades\Redirect;
use Illuminate\Support\Facades\File;
use Illuminate\Validation\Rule;
use Illuminate\Validation\Rules\Password;

class ProfileController extends Controller
{
    protected function storagePath()
    {
        return env('STORAGE_PATH', public_path('penyimpanan'));
    }

    /**
     * Menampilkan halaman form profil.
     */
    public function show()
    {
        return view('profile.profile', [
            'user' => Auth::user(),
        ]);
    }

    /**
     * Memperbarui informasi profil pengguna.
     */
    public function update(Request $request)
    {
        $user = $request->user();

        $request->validate([
            'name' => ['required', 'string', 'max:255'],
            'email' => ['required', 'string', 'email', 'max:255', Rule::unique('users')->ignore($user)],
            'foto' => ['nullable', 'image', 'mimes:jpg,jpeg,png,gif,bmp,webp', 'max:2048'],
        ]);

        $user->fill($request->only('name', 'email'));

        if ($user->isDirty('email')) {
            $user->email_verified_at = null;
        }

        if ($request->hasFile('foto')) {
            $storagePath = $this->storagePath();
            
            // Hapus foto lama jika ada
            if ($user->foto && file_exists($storagePath . '/' . $user->foto)) {
                unlink($storagePath . '/' . $user->foto);
            }
            
            // Simpan foto ke folder penyimpanan eksternal
            $file = $request->file('foto');
            $filename = time() . '_' . preg_replace('/[^a-zA-Z0-9._-]/', '_', $file->getClientOriginalName());
            $file->move($storagePath . '/profile-photos', $filename);
            $user->foto = 'profile-photos/' . $filename;
        }

        $user->save();

        return Redirect::route('profile')->with('status', 'profile-updated');
    }

    /**
     * Memperbarui password pengguna.
     */
    public function updatePassword(Request $request)
    {
        $validated = $request->validate([
            'current_password' => ['required', 'current_password'],
            'password' => ['required', Password::defaults(), 'confirmed'],
        ]);

        $request->user()->update([
            'password' => Hash::make($validated['password']),
        ]);

        Auth::logout();

        $request->session()->invalidate();
        $request->session()->regenerateToken();

        return Redirect::route('login')->with('status', 'Password berhasil diubah. Silakan login kembali dengan kata sandi baru Anda.');
    }
}