<?php
session_start();

include('member_session.php');
include('../coach/functions.php');
include("../coach/objects/class.member.php");

$page_action = $_GET['action'] ?? '';

$member_object = new Member();
$member = $member_object->Get($_SESSION['memberid'] ?? 0);

$meals_per_day = $member->meals_per_day ?? 0;

$grade = '';
$icon = 'success';

// SAFE POST CHECK
if (empty($page_action)) {

    if (!empty($_POST['meal_score_totals']) && !empty($_POST['posting'])) {

        if ($_POST['posting'] === "meal_score") {

            $score = (int)$_POST['meal_score_totals'];

            // SIMPLIFIED LOGIC 🔥
            if ($score === 0) {
                $grade = "A+ (Amazing Job!)";
            } elseif ($score <= 2) {
                $grade = "A (Outstanding Job!)";
            } elseif ($score <= 4) {
                $grade = "B+ (Excellent Job!)";
            } elseif ($score <= ($meals_per_day - 1)) {
                $grade = "B (Great Job!)";
            } else {
                $grade = "You Can Do Better: NEVER GIVE UP!";
                $icon = "error";
            }
        }
    }

    $page_action = $_POST['action'] ?? '';
    $memberid = $_SESSION['memberid'] ?? 0;
}

if ($page_action === 'edit') {
    $memberid = (int)($_GET['memberid'] ?? 0);
}

if ($page_action === 'update') {
    $memberid = (int)($_POST['memberid'] ?? 0);
}

$msg = 'Daily Log';

include('member-header.php');
?>