<?php

namespace App\Http\Controllers\API;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;

class UserController extends Controller
{
    public function me()
    {
        $user = getAuthUser();

        return response()->json([
            'data' => [
                'id' => $user->id,
                'first_name' => $user->first_name,
                'last_name' => $user->last_name,
                'middle_name' => $user->middle_name,
                'language' => $user->language,
                'phone' => $user->phone,
                'gender' => $user->gender == 1 ? true : false
            ]
        ]);
    }

    public function changeLang(Request $request)
    {
        $validator = Validator::make($request->all(), [
            'language' => 'required|string|in:ru,uz',
        ]);

        if ($validator->fails()) {
            return response()->json([
                'error' => 'Validation error',
                'details' => $validator->errors()
            ], 422);
        }

        $user = getAuthUser();
        $user->update($request->only('language'));

        return response()->json([
            'data' => [
                'id' => $user->id,
                'first_name' => $user->first_name,
                'last_name' => $user->last_name,
                'middle_name' => $user->middle_name,
                'language' => $user->language,
                'phone' => $user->phone,
                'gender' => $user->gender
            ]
        ]);
    }

    public function update(Request $request)
    {
        // validate
        $request->validate([
            'first_name' => 'required|string',
            'last_name' => 'required|string',
            'middle_name' => 'nullable|string',
            'gender' => 'required|boolean',
        ]);

        getAuthUser()->update($request->only('first_name', 'last_name', 'middle_name', 'gender'));

        return response()->json([
            'data' => [
                'id' => getAuthUser()->id,
                'first_name' => getAuthUser()->first_name,
                'last_name' => getAuthUser()->last_name,
                'middle_name' => getAuthUser()->middle_name,
                'language' => getAuthUser()->language,
                'phone' => getAuthUser()->phone,
                'gender' => getAuthUser()->gender
            ]
        ]);
    }

    public function delete()
    {
        $user = getAuthUser();
        // delte name
        $user->first_name = null;
        $user->last_name = null;
        $user->middle_name = null;
        $user->avatar = null;
        $user->email = null;
        $user->verify_code = null;
        $user->birth_day = null;
        $user->postal_address = null;
        $user->category_id = null;
        $user->notification = 0;
        $user->save();
        // delete
        $user->delete();

        return response()->json([
            'message' => 'User deleted'
        ], 204);
    }
}
