<?php

namespace App\Models;

# use Illuminate\Database\Eloquent\Model;

class Segment extends Model
{
    protected $table = 'segment';

    public function segmentTemplate() {
        return $this->hasOne(SegmentTemplate::class, 'id', 'segment_template_id');
    }

    public function note() {
        return $this->hasOne(Note::class, 'id', 'note_id');
    }

    public function client() {
        return $this->hasOne(Client::class, 'id', 'client_id');
    }

    public function shortName(){
        return trim(array_reverse(explode('>', $this->display_title))[0]);
    }

    public function summarySuggestions()
    {
        return $this->hasMany(SegmentSummarySuggestion::class, 'segment_id', 'id')->orderBy('created_at', 'DESC');
    }

    public function proposedSegmentSummarySuggestion() {
        return $this->hasOne(SegmentSummarySuggestion::class, 'id', 'proposed_segment_summary_suggestion_id');
    }

    public function acceptedSegmentSummarySuggestion() {
        return $this->hasOne(SegmentSummarySuggestion::class, 'id', 'accepted_segment_summary_suggestion_id');
    }

    public function getRecalculatedHtml($performer, $sessionKey, $getSummaryOnly = false){
        $pro = $performer->pro;
        $segment = $this; 
        $segmentTemplate = $this->segmentTemplate;

        $note = $this->note;
        $patient = $note->client;

        $data = compact('performer', 'pro', 'segment', 'segmentTemplate', 'note', 'patient', 'sessionKey');


        $summaryHtml = view('app.patient.segment-templates.' . $segmentTemplate->internal_name . '/summary', $data)->render();

        $wizardPowered = [
            'intake_medications',
            'plan_medications',
            'intake_problems',
            'plan_problems',
            'intake_goals',
            'plan_goals',
            'intake_allergies',
            'plan_allergies',
            'intake_care_team',
            'plan_care_team',
            'intake_supplements',
            'plan_supplements'
        ];
	$editHtml = null;
        if(!in_array($segmentTemplate->internal_name, $wizardPowered) && !$getSummaryOnly) {
            $editHtml = view('app.patient.segment-templates.' . $segmentTemplate->internal_name . '/edit', $data)->render();
        }

        return [
            'summaryHtml'=> $summaryHtml ,
            'editHtml' => $editHtml
        ];
    }

}
