<?php

namespace App\Http\Controllers\api;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Models\Perawat;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Hash;

class RegisterController extends Controller
{
    private $apiKey = "lkjnsfaJHoahslkf889w4najnKLNLKGjnaljnsdio8";

    public function register(Request $req){
        $errApiKey = "You don't have Authorization.";
        if($this->apiKey != $req->header('apiKey')){
            return response([
                'status'=>'Error',
                'message'=>$errApiKey,
            ], 200);
        }

        $scsMessage = "Berhasil mendaftar. Silahkan login aplikasi.";
        $errMessage = "Id perawat sudah ada. Silahkan login aplikasi.";
        $data = Perawat::where('id_perawat', $req->id_perawat)->first();
        if (!$data) {
            $data = new Perawat();
            $data->id_perawat = $req->id_perawat;
            $data->nama = $req->nama;
            $data->email = $req->email;
            $data->password =  Hash::make($req->password);
            $data->save();

            return response([
                'status' => 'Ok',
                'message' => $scsMessage,
                'data' => $data
            ], 200);
        }else{
            return response([
                'status' => 'Error',
                'message' => $errMessage,
                'data' => $data
            ], 200);
        }
    }
}
