<?php
namespace App\Http\Controllers\Api;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;

class UserController extends Controller
{
    public function update(Request $request)
    {
        $data = $request->validate(['name'=>'string|max:100','date_of_birth'=>'nullable|date','gender'=>'nullable|in:male,female,other','district'=>'nullable|string']);
        $request->user()->update($data);
        return response()->json(['success'=>true,'user'=>$request->user()]);
    }
    public function uploadAvatar(Request $request)
    {
        $request->validate(['avatar'=>'required|image|mimes:jpg,jpeg,png|max:2048']);
        $path = $request->file('avatar')->store('avatars','public');
        $request->user()->update(['avatar'=>Storage::url($path)]);
        return response()->json(['success'=>true,'avatar'=>Storage::url($path)]);
    }
    public function loyaltyPoints(Request $request)
    {
        return response()->json(['success'=>true,'points'=>$request->user()->loyalty_points]);
    }
}
