<?php

require_once (dirname(__FILE__) . '/php/config.php');

$reportStartDate = $utilities->validateData($_GET, 'startDate');
$reportEndDate = $utilities->validateData($_GET, 'endDate');

if (0 == strlen($reportStartDate))
{
	// $reportStartDate = DateTime::createFromFormat('m/d/Y', date('m/d/Y', strtotime(date('o-\\WW'))));
	$reportStartDate = DateTime::createFromFormat('m/d/Y', '07/06/2026');
}
else
{
	$reportStartDate = DateTime::createFromFormat('m/d/Y', $reportStartDate);
}

if (0 == strlen($reportEndDate))
{
	// $reportEndDate = DateTime::createFromFormat('m/d/Y', date('m/d/Y', strtotime(date('o-\\WW', strtotime('next week'))) - (60 * 60 * 24)));
	$reportEndDate = DateTime::createFromFormat('m/d/Y', '07/12/2026');
}
else
{
	$reportEndDate = DateTime::createFromFormat('m/d/Y', $reportEndDate);
}

$priorWeekStart = date('m/d/Y', strtotime('-1 week', $reportStartDate->format('U')));
$priorWeekEnd = date('m/d/Y', strtotime('-1 day', $reportStartDate->format('U')));

$nextWeekStart = date('m/d/Y', strtotime('+1 week', $reportStartDate->format('U')));
$nextWeekEnd = date('m/d/Y', strtotime('+1 week', $reportEndDate->format('U')));

?>

<!doctype html>
<html lang="en">
<head>
    <title>Executive Summary</title>

	<?php include_once ('includes/head.php'); ?>

	<style>
		table
		{
			max-width: 100% !important;
		}
	</style>
</head>
<body>

	<?php include_once ('includes/nav.php'); ?>

	<div class="pageContent">
		<div class="container-fluid">
			<div class="row mt-3">
				<h3 class="text-center">
					<span class="d-block d-sm-inline-block">Executive Summary:</span>
					<span class="d-block d-sm-inline-block"><?php echo $reportStartDate->format('m/d/Y'); ?> - <?php echo $reportEndDate->format('m/d/Y'); ?></span>
				</h3>
				<div class="toolbar justify-content-between row align-items-center">
					<div class="col-sm-auto text-center text-sm-start">
						<a href="executiveSummary.php?startDate=<?php echo $priorWeekStart; ?>&endDate=<?php echo $priorWeekEnd; ?>">&laquo; <?php echo $priorWeekStart; ?> - <?php echo $priorWeekEnd; ?></a>
					</div>
					<div class="col-sm text-center mb-3 mb-sm-0 overflow-y-hidden">
					</div>
					<div class="col-sm-auto text-center text-sm-end">
						<a href="executiveSummary.php?startDate=<?php echo $nextWeekStart; ?>&endDate=<?php echo $nextWeekEnd; ?>"><?php echo $nextWeekStart; ?> - <?php echo $nextWeekEnd; ?> &raquo;</a>
					</div>
				</div>
			</div>
			<div class="row py-3 d-print-none">
				<div class="col text-center">
					<a class="btn btn-info" href="weeklyReport.php?startDate=<?php echo $reportStartDate->format('m/d/Y'); ?>">Weekly Status Report</a>
					<a class="btn btn-info" href="aiPrompts.php?startDate=<?php echo $reportStartDate->format('m/d/Y'); ?>">AI Analysis</a>
				</div>
			</div>

			<div class="row py-3">
			<?php
				$file = dirname(__FILE__) . '/demo_data/executiveSummary/' . $reportStartDate->format('Y-m-d') . '.txt';

				if (file_exists($file))
				{
					$chatgpt_summary = json_decode(file_get_contents($file), true);

					if ($chatgpt_summary)
					{
						if (isset($chatgpt_summary['choices'][0]))
						{
							$content = str_ireplace(array('```html', '```'), '', $chatgpt_summary['choices'][0]['message']['content']);

							echo $content;
						}
					}
				}
			?>
			</div>
        </div>
	</div>

	<?php include_once (dirname(__FILE__) . '/includes/footer.php'); ?>

	<?php include_once (dirname(__FILE__) . '/includes/scripts.php'); ?>
	<?php include_once (dirname(__FILE__) . '/includes/analytics.php'); ?>

</body>
</html>
