<?php

namespace App\Http\Controllers\Api\Users;

use App\Exceptions\StoreResourceFailedException;
use App\Http\Controllers\Controller;
use App\Models\Companies\CompanyMember;
use App\Transformers\Companies\CompanyMemberTransformer;
use App\Transformers\Users\UserTransformer;
use Illuminate\Contracts\Hashing\Hasher;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;

class ProfileController extends Controller
{
	public function membership(Request $request) {
		return fractal($request->user()->membership, new CompanyMemberTransformer())->respond();
	}

	public function currentMember(Request $request) {
		return fractal($request->user()->selectedMember, new CompanyMemberTransformer());
	}

	public function selectMember(Request $request) {
		$this->validate($request, [
			'member_id' => 'required|exists:company_members,uuid'
		]);
		CompanyMember::byUuid($request->get('member_id'))->firstOrFail()->select();
		return fractal($request->user()->membership, new CompanyMemberTransformer())->respond();
	}



    public function index()
    {
        return fractal(Auth::user(), new UserTransformer())->respond();
    }

    public function update(Request $request)
    {
        $user = Auth::user();
        $rules = [
            'name' => 'required',
            'email' => 'required|email|unique:users,email,'.$user->id,
        ];
        if ($request->method() == 'PATCH') {
            $rules = [
                'name' => 'sometimes|required',
                'email' => 'sometimes|required|email|unique:users,email,'.$user->id,
            ];
        }
        $this->validate($request, $rules);
        // Except password as we don't want to let the users change a password from this endpoint
        $user->update($request->except('_token', 'password'));

        return fractal($user->fresh(), new UserTransformer())->respond();
    }

    public function updatePassword(Request $request)
    {
        $user = Auth::user();
        $this->validate($request, [
            'current_password' => 'required',
            'password' => 'required|min:8|confirmed',
        ]);
        // verify the old password given is valid
        if (! app(Hasher::class)->check($request->get('current_password'), $user->password)) {
            throw new StoreResourceFailedException('Validation Issue', [
                'old_password' => 'The current password is incorrect',
            ]);
        }
        $user->password = bcrypt($request->get('password'));
        $user->save();

        return fractal($user->fresh(), new UserTransformer())->respond();
    }
}
