<?php

/** @var \App\Models\Pro $hcpPro */
/** @var \App\Models\Pro $pro */
/** @var \App\Models\Note $note */

use App\Models\Page;

$page = Page::where('client_id', $patient->id)->where('category', 'CANVAS')->where('key', 'lifestyle_rx_review')->first();

if(!@$hcpPro) {
    if(@$note) {
        $hcpPro = $note->hcpPro;
    }
    else {
        $hcpPro = $pro; // should never get here
    }
}

$contentData = [];
if($page){
    $contentData = json_decode($page->data, true);
}else{
    if ($patient->canvas_data) {
        $canvasData = json_decode($patient->canvas_data, true);
        if(!isset($canvasData["lifestyle_rx_review"])) $canvasData["lifestyle_rx_review"] = [];    
        $contentData = $canvasData["lifestyle_rx_review"];
    }
}


if(!isset($contentData[$hcpPro->id])) {
    $contentData[$hcpPro->id] = [
        'physical_activity' => [
            'type' => '',
            'frequency' => '',
            'time' => '',
            'intensity' => '',
            'enjoyment' => '',
        ],
        'barriers' => '',   // free text
        'assessment' => '', // Inactive, Insufficiently Active, Active, Highly Active
        'adherence' => [
            'seated' => [
                'active' => false,
                'memo' => '',
                'adherence' => '',
                'adherence_memo' => '',
            ],
            'arm' => [
                'active' => false,
                'memo' => '',
                'adherence' => '',
                'adherence_memo' => '',
            ],
            'aquatic' => [
                'active' => false,
                'memo' => '',
                'adherence' => '',
                'adherence_memo' => '',
            ],
            'aerobic' => [
                'active' => false,
                'walk' => [
                    'active' => false,
                    'days_per_week' => '',
                    'minutes_per_day' => '',
                    'intensity' => '', // Light, Moderate, Vigorous
                    'memo' => '',
                    'adherence' => '',
                    'adherence_memo' => '',
                ],
                'jog' => [
                    'active' => false,
                    'days_per_week' => '',
                    'minutes_per_day' => '',
                    'intensity' => '', // Light, Moderate, Vigorous
                    'memo' => '',
                    'adherence' => '',
                    'adherence_memo' => '',
                ],
                'elliptical' => [
                    'active' => false,
                    'days_per_week' => '',
                    'minutes_per_day' => '',
                    'intensity' => '', // Light, Moderate, Vigorous
                    'memo' => '',
                    'adherence' => '',
                    'adherence_memo' => '',
                ],
                'swim' => [
                    'active' => false,
                    'days_per_week' => '',
                    'minutes_per_day' => '',
                    'intensity' => '', // Light, Moderate, Vigorous
                    'memo' => '',
                    'adherence' => '',
                    'adherence_memo' => '',
                ],
                'bike' => [
                    'active' => false,
                    'days_per_week' => '',
                    'minutes_per_day' => '',
                    'intensity' => '', // Light, Moderate, Vigorous
                    'memo' => '',
                    'adherence' => '',
                    'adherence_memo' => '',
                ],
            ],
            'strength' => [
                'active' => false,
                'exercises' => [
                    [
                        'name' => '',
                        'days_per_week' => '',
                        'reps' => '',
                        'intensity' => '', // Light, Moderate, Vigorous
                        'memo' => '',
                        'adherence' => '',
                        'adherence_memo' => '',
                    ]
                ]
            ],
            'neat' => [
                'active' => false,
                'recommendations' => [
                    'park_farther' => false,
                    'park_farther_adherence' => '',
                    'park_farther_adherence_memo' => '',
                    'steps_instead_of_elevator' => false,
                    'steps_instead_of_elevator_adherence' => '',
                    'steps_instead_of_elevator_adherence_memo' => '',
                    'stand_instead_of_sit' => false,
                    'stand_instead_of_sit_adherence' => '',
                    'stand_instead_of_sit_adherence_memo' => '',
                    'steps_goal_per_day' => '',
                    'steps_goal_per_day_adherence' => '',
                    'steps_goal_per_day_adherence_memo' => '',
                    'other' => '',
                    'other_adherence' => '',
                    'other_adherence_memo' => '',
                ]
            ]
        ]
    ];
}

// if lifestyle_rx_update available, prefill with that
if (isset($contentData) &&
    isset($contentData[$hcpPro->id]) &&
    isset($contentData[$hcpPro->id]['prescription'])) {
    $contentData[$hcpPro->id]['adherence'] =$contentData[$hcpPro->id]['prescription'];
}

$contentData = $contentData[$hcpPro->id];