<?php
/**
 * MonthlyTargetRendererTest.php
 *
 * Verifies that the monthly line in goals_progress_alert_renderer uses
 * the fixed monthly target (criteria->target) instead of a day-derived target.
 */

namespace {
    if (!defined('WD')) { define('WD', dirname(__DIR__, 2)); }
}

// --- Lightweight stubs to avoid DB access ---
namespace objects {
    class departments_o {
        public int $id = 0;
        public $name;
        public function select(int $id): self {
            $o = new self();
            $o->id = $id;
            $o->name = new class($id) {
                private int $id;
                public function __construct(int $id) { $this->id = $id; }
                public function value(): string { return 'Afdeling ' . $this->id; }
            };
            return $o;
        }
        public function exists(): bool { return true; }
    }
}

namespace {
    require_once WD . '/modules/goals/helpers/goals_criteria_type.php';
    require_once WD . '/modules/goals/helpers/goals_criteria_progress_alert_frequency.php';
    require_once WD . '/modules/goals/helpers/goals_criteria_progress_alert_destination.php';
    require_once WD . '/modules/goals/helpers/goals_criteria_progress_alert_progress_type.php';
    require_once WD . '/modules/goals/helpers/goals_criteria_progress_alert_style.php';
    require_once WD . '/modules/goals/helpers/goals_criteria_progress_alert_weekday.php';

    require_once WD . '/modules/goals/traits/goals_timeframe_t.php';
    require_once WD . '/modules/goals/traits/goals_target_t.php';
    require_once WD . '/modules/goals/traits/goals_result_parser_t.php';
    require_once WD . '/modules/goals/traits/goals_label_t.php';

    require_once WD . '/modules/goals/interfaces/goals_criteria_i.php';
    require_once WD . '/modules/goals/interfaces/goals_criteria_users_i.php';
    require_once WD . '/modules/goals/interfaces/goals_criteria_departments_i.php';
    require_once WD . '/modules/goals/interfaces/goals_criteria_products_i.php';

    require_once WD . '/modules/goals/traits/goals_criteria_users_t.php';
    require_once WD . '/modules/goals/traits/goals_criteria_departments_t.php';
    require_once WD . '/modules/goals/traits/goals_criteria_products_t.php';

    require_once WD . '/modules/goals/classes/goals_criteria_users.php';
    require_once WD . '/modules/goals/classes/goals_criteria_departments.php';
    require_once WD . '/modules/goals/classes/goals_criteria_products.php';
    require_once WD . '/modules/goals/classes/goals_criteria.php';

    require_once WD . '/modules/goals/traits/goals_criteria_progress_alert_format_t.php';
    require_once WD . '/modules/goals/formats/slack_progress_alert_format.php';
    require_once WD . '/modules/goals/formats/email_progress_alert_format.php';
    require_once WD . '/modules/goals/formats/sms_progress_alert_format.php';
    require_once WD . '/modules/goals/services/goals_progress_alert_renderer.php';

    use goals\classes\goals_criteria;
    use goals\helpers\goals_criteria_type as Type;
    use goals\helpers\goals_criteria_progress_alert_destination as Dest;
    use goals\helpers\goals_criteria_progress_alert_progress_type as PType;

    $criteria = new goals_criteria();
    $criteria->type = Type::NONE; // keeps count deterministic (0)
    $criteria->target = 70; // fixed monthly target we want to enforce
    $criteria->label = 'Maanedsmaal test';
    $criteria->start = new \DateTime('first day of this month 00:00:00');
    $criteria->end = new \DateTime('now');
    $criteria->progress_alert_destination = Dest::NONE;
    $criteria->progress_alert_progress_type = PType::ALL;
    $criteria->departments->set([
        (new \objects\departments_o())->select(12)
    ]);

    $msg = \goals\services\goals_progress_alert_renderer::render($criteria);

    echo "--- Message ---\n" . $msg . "\n---------------\n";

    $ok = true;

    if (strpos($msg, 'ud af 70') === false) {
        echo "FAIL: Monthly target is not rendered as 70.\n";
        $ok = false;
    } else {
        echo "OK: Monthly target rendered with denominator 70.\n";
    }

    if (strpos($msg, 'Afdeling 12: 0 ud af 70 (0.00%)') === false) {
        echo "FAIL: Department monthly line does not use fixed monthly target 70.\n";
        $ok = false;
    } else {
        echo "OK: Department monthly line uses fixed monthly target.\n";
    }

    exit($ok ? 0 : 1);
}

