<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;

/**
 * @property string $id
 * @property string $patients_id
 * @property string $date
 * @property integer $active
 * @property string $especiality
 * @property string $created_at
 * @property string $updated_at
 * @property Patient $patient
 */
class Appointments extends Model
{
    /**
     * The "type" of the auto-incrementing ID.
     * 
     * @var string
     */
    protected $keyType = 'string';

    /**
     * Indicates if the IDs are auto-incrementing.
     * 
     * @var bool
     */
    public $incrementing = false;

    /**
     * @var array
     */
    protected $fillable = ['id','patients_id','code', 'date', 'active', 'especiality', 'created_at', 'updated_at'];

    /**
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     */
    public function patient()
    {
        return $this->belongsTo('App\Patient', 'patients_id');
    }

    /**
     * 
     */
    public static function factoryCreate($data,$patientId){
        $appointment = [];
        $date                           = date_create_from_format('d/m/Y',$data["appointmentDate"]);
        $appointment["id"]              = md5($patientId . $date->format('Y-m-d H:i:s'));
        $appointment["patients_id"]     = $patientId;
        $appointment["active"]          = 1;
        $appointment["date"]            = $date;
        $appointment["especiality"]     = $data["especiality"];
        $appointment["code"]            = "MED-".DB::table('appointments')->get()->count();
        return $appointment;
    }
}
