<?php

namespace App\Models;

# use Illuminate\Database\Eloquent\Model;

class Note extends Model
{
    protected $table = "note";

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

    public function hcpPro()
    {
        return $this->hasOne(Pro::class, 'id', 'hcp_pro_id');
    }

    public function noteTemplate()
    {
        return $this->hasOne(NoteTemplate::class, 'id', 'note_template_id');
    }

    public function createdSession()
    {
        return $this->hasOne(AppSession::class, 'id', 'created_by_session_id');
    }

    public function allyPro()
    {
        return $this->hasOne(Pro::class, 'id', 'ally_pro_id');
    }

    public function bills()
    {
        return $this->hasMany(Bill::class, 'note_id', 'id');
    }

    public function addendums()
    {
        return $this->hasMany(NoteAddendum::class, 'note_id', 'id')->where('is_removed', false);
    }

    public function sections()
    {
        return $this->hasMany(Section::class, 'note_id', 'id')
            ->where('is_active', true)
            ->orderBy('position_index', 'asc');
    }

    public function reasons()
    {
        return $this->hasMany(NoteReason::class, 'note_id', 'id');
    }

    public function claims()
    {
        return $this->hasMany(Claim::class, 'note_id', 'id');
    }

    public function summary()
    {
        $parts = [];
        foreach ($this->sections as $section) {
            $parts[] = $section->summary_html;
        }
        $parts = array_map(function($_part) {
             return '<div class="note-section-summary">' . $_part . '</div>';
        }, $parts);
        return implode("", $parts);
    }
}
