<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class DashboardController extends Controller
{
    public function index()
    {
        $user   = auth()->user();
        $orders = \App\Models\Order::where('user_id', $user->id)
            ->with('items')
            ->latest()
            ->get();
        return view('dashboard', compact('user', 'orders'));
    }

    public function updateProfile(Request $request)
    {
        $request->validate([
            'name'  => 'required|string|max:100',
            'phone' => 'nullable|string|max:15',
        ]);
        auth()->user()->update($request->only('name', 'phone'));
        return back()->with('success', 'Profile updated!');
    }

    public function updatePassword(Request $request)
    {
        $request->validate([
            'current_password' => 'required|current_password',
            'password'         => 'required|min:8|confirmed',
        ]);
        auth()->user()->update(['password' => \Hash::make($request->password)]);
        return back()->with('success', 'Password changed successfully!');
    }
}
