<?php

namespace App\Http\Controllers\Auth;

use App\Http\Controllers\Controller;
use App\Models\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Facades\Validator;

class ProfileController extends Controller
{
    public function index()
    {
        $user = Auth::user();
        return view('auth.profile', compact('user'));
    }

    public function updateData(request $request)
    {
        $customMessage = [
            'username.required' => 'Username wajib diisi',
            'username.max' => 'Username maksimal 12 karakter',
            'username.string' => 'Username harus berupa string',
            'username.unique' => 'Username sudah terdaftar',

            'name.required' => 'Nama wajib diisi',
            'name.max' => 'Nama maksimal 255 karakter',
            'name.string' => 'Nama harus berupa string',
            'name.unique' => 'Nama sudah terdaftar',

            'email.required' => 'Email wajib diisi',
            'email.email' => 'Email tidak valid',
            'email.unique' => 'Email sudah terdaftar',
            'email.max' => 'Email maksimal 255 karakter',
            'email.string' => 'Email harus berupa string',

            'avatar.image' => 'Foto profile harus berupa gambar',
            'avatar.mimes' => 'Format foto profile tidak sesuai, harus jpeg, png, atau jpg',
            'avatar.max' => 'Ukuran foto profile maksimal 2MB',
        ];

        $validator = Validator::make($request->all(), [
            'username' => 'required|string|max:12|unique:users,username,' . Auth::user()->id,
            'name' => 'required|string|max:255|unique:users,name,' . Auth::user()->id,
            'email' => 'required|string|email|max:255|unique:users,email,' . Auth::user()->id,
            'avatar' => 'nullable|image|mimes:jpeg,png,jpg|max:2048',
        ], $customMessage);

        if ($validator->fails()) {
            toast($validator->messages()->all()[0], 'error')->position('top-right')->autoclose(3000);
            return redirect()->back()->withInput();
        }

        try {
            $user = User::findOrFail(Auth::id());
            $user->username = $request->username;
            $user->name = $request->name;
            $user->email = $request->email;

            if ($request->hasFile('avatar')) {

                $path = $request->file('avatar')->store('avatars', 'public');

                if (!empty($user->avatar) && Storage::disk('public')->exists($user->avatar)) {
                    Storage::disk('public')->delete($user->avatar);
                }
                $user->avatar = $path;
            }

            $user->save();
            toast('Data berhasil diubah', 'success')->position('top-right')->autoclose(3000);
            return redirect()->back();
        } catch (\Throwable $th) {
            toast('Terjadi kesalahan', 'error')->position('top-right')->autoclose(3000);
            return redirect()->back();
        }
    }

    public function updatePassword(request $request)
    {
        $customMessage = [
            'new_password.required' => 'Password wajib diisi',
            'new_password.confirmed' => 'Konfirmasi password tidak cocok',
            'new_password.min' => 'Password minimal 8 karakter',
        ];

        $user = User::find(Auth::user()->id);
        if (Hash::check($request->old_password, $user->password)) {

            $validator = Validator::make($request->all(), [
                'new_password' => 'required|string|min:8|confirmed',
            ], $customMessage);

            if ($validator->fails()) {
                toast($validator->messages()->all()[0], 'error')->position('top-right')->autoclose(3000);
                return redirect()->back()->withInput();
            }

            $user->password = Hash::make($request->new_password);

            try {
                $user->save();
                toast('Password berhasil diubah', 'success')->position('top-right')->autoclose(3000);
                return redirect()->back();
            } catch (\Throwable $th) {
                toast('Terjadi kesalahan', 'error')->position('top-right')->autoclose(3000);
                return redirect()->back();
            }
        } else {
            toast('Password lama tidak cocok', 'error')->position('top-right')->autoclose(3000);
            return redirect()->back();
        }
    }
}
