<?php

namespace Modules\Linksehat\Transformers\User;

use Illuminate\Http\Resources\Json\JsonResource;
use App\Models\OLDLMS\User;
use App\Models\OLDLMS\UserDetail;
use App\Models\OLDLMS\UserInsurance;
use DB;


class ShowProfileResource extends JsonResource
{
    /**
     * Transform the resource into an array.
     *
     * @param  \Illuminate\Http\Request
     * @return array
     */

    protected $connection = 'oldlms';

    public function toArray($request)
    {
        
        // Principal
        if ($this->detail){
            switch ($this->detail->nIDGolonganDarah) {
                case 1:
                    $goldar = 'A';
                    break;
                case 2: 
                    $goldar = 'B';
                    break;
                case 3:
                    $goldar = 'AB';
                    break;
                case 4:
                    $goldar = 'O';
                    break;
                
                default:
                   $goldar = '-';
                    break;
            }
            $urlAvatarDefault = $this->detail->nIDJenisKelamin == 1 ? 'https://linksehat.dev/assets/img/users/male-avatar.png' : 'https://linksehat.dev/assets/img/users/female-avatar.png';
             // Marital Status
            $maritalStatus = DB::connection('oldlms')->table('tm_status_pernikahan')->where('nID', $this->detail->sMartialStatus)->first('sStatusPernikahan');

            // Hubungan Keluarga
            $relationship = DB::connection('oldlms')->table('tm_hubungan_keluarga')->where('nID', $this->nIDHubunganKeluarga)->first('sHubunganKeluarga');
            // dd( $this->detail->nIDGolonganDarah);
            $sWeight = $this->detail->sWeight ? $this->detail->sWeight : 0;
            $sHeight = $this->detail->sHeight ? $this->detail->sHeight : 0;
            $nIDJenisKelamin = $this->detail->nIDJenisKelamin == 1 ? 'Male' : 'Female';
        } else {
            $urlAvatarDefault =  'https://linksehat.dev/assets/img/users/male-avatar.png';
            $goldar = '-';
            $relationship = false;
            $maritalStatus = false;
            $sWeight = 0;
            $sHeight = 0;
            $nIDJenisKelamin = false;
        }
        $avatar = $this->detail->sImage ?? $urlAvatarDefault;
       
       
        $userInsurance = UserInsurance::where('nIDUser', $this->nID)->get()->first();
        $memberId  = Null;
        if($userInsurance){
            $memberId  = $userInsurance->sNoPolis;
        }
        return [
            'id' => $this->nID,
            'first_name' => $this->sFirstName,
            'last_name' => $this->sLastName,
            'date_of_birth' => $this->detail ? $this->detail->dTanggalLahir : null,
            'avatar' => $avatar,
            'gender' => $nIDJenisKelamin ? $nIDJenisKelamin : '-',
            'phone' => $this->sPhone,
            'email' => $this->sEmail,
            'blood_type' => $goldar,
            'marital_status' => $maritalStatus ? $maritalStatus->sStatusPernikahan : '-',
            'relationship' => $relationship ?  $relationship->sHubunganKeluarga : '-',
            'weight' => $sWeight,
            'height' => $sHeight,
            'member_id' =>  $memberId,
        ];
    }
}
