<?php

namespace Modules\Linksehat\Http\Controllers\Api;

use App\Helpers\Helper;
use App\Models\User;
use Illuminate\Contracts\Support\Renderable;
use Illuminate\Http\Request;
use Illuminate\Routing\Controller;
use Illuminate\Support\Facades\Auth;
use Modules\Linksehat\Transformers\UserProfileResource;

class ProfileController extends Controller
{
    /**
     * Display a listing of the resource.
     * @return Renderable
     */
    public function index()
    {
        $user = Auth::user()->load('person');

        return Helper::responseJson(new UserProfileResource($user));
    }

    /**
     * Show the form for creating a new resource.
     * @return Renderable
     */
    public function create()
    {
        return view('linksehat::create');
    }

    /**
     * Store a newly created resource in storage.
     * @param Request $request
     * @return Renderable
     */
    public function store(Request $request)
    {
        //
    }

    /**
     * Show the specified resource.
     * @param int $id
     * @return Renderable
     */
    public function show($id)
    {
        return view('linksehat::show');
    }

    /**
     * Show the form for editing the specified resource.
     * @param int $id
     * @return Renderable
     */
    public function edit($id)
    {
        return view('linksehat::edit');
    }

    /**
     * Update the specified resource in storage.
     * @param Request $request
     * @param int $id
     * @return Renderable
     */
    public function update(Request $request)
    {
        $request->validate([
            'nik' => 'numeric',
            'name_prefix' => '',
            'name' => '',
            'name_suffix' => '',
            'gender' => 'in:P,L',
            'birth_date' => 'sometimes:date',
            // 'is_deceased' => '',
            'deceased_at' => 'sometimes:date',
            'marital_status' => 'in:S,M,C',
            'preferred_lang' => 'in:id,en'
        ]);

        $user = Auth::user();
        if ($user->person) { // Update Profile
            $user->person->fill($request->only([
                'nik', 'name_prefix', 'name', 'name_suffix', 'gender', 'birth_date', 'deceased_at', 'marital_status'
            ]))->save();

            if ($request->has('preferred_lang')) {
                $user->metas()->updateOrCreate([
                    'type' => 'preferred_lang'
                ], [
                    'system' => 'default',
                    'type' => 'preferred_lang',
                    'value' => $request->preferred_lang,
                ]);
            }
        } else { // Create If Not Exist
            $newPerson = array_merge(
                $request->only([
                    'nik', 'name_prefix', 'name', 'name_suffix', 'gender', 'birth_date', 'deceased_at', 'marital_status'
                ]),
                ['user_owner_id' => $user->id]
            );
            $newPerson = $user->person()->create($newPerson);
            $user->person_id = $newPerson->id;
            $user->save();

            if ($request->has('preferred_lang')) {
                $user->metas()->updateOrCreate([
                    'type' => 'preferred_lang'
                ], [
                    'system' => 'default',
                    'type' => 'preferred_lang',
                    'value' => $request->preferred_lang,
                ]);
            }
        }

        $user = User::find($user->id); // ReLoad User Data
        return Helper::responseJson(['users' => new UserProfileResource($user)]);
    }

    /**
     * Remove the specified resource from storage.
     * @param int $id
     * @return Renderable
     */
    public function destroy($id)
    {
        //
    }
}
