<?php
require_once __DIR__ . '/engine/engine.php';
require_once __DIR__ . '/engine/api-handling/fmp_client.php';
require_once __DIR__ . '/engine/bewertungsmethoden/bewertung_minervini.php';

$requires_search = true;
$requires_chartjs = true;
$requires_dataTables = true;

require_once __DIR__ . '/views/header.php';
require_once __DIR__ . '/views/navigation.php';

$symbol = $_GET['symbol'] ?? '';

$scoreData = $symbol ? calculate_minervini_score($symbol) : [];

// Robuste Zuordnung mit sinnvollen Defaults:
$technicalScore        = $scoreData['technicalScore']       ?? 0;
$fundamentalScore      = $scoreData['fundamentalScore']     ?? 0;
$maxScore              = $scoreData['maxScore']             ?? 14;
$score                 = $scoreData['score']                ?? ($technicalScore + $fundamentalScore);
$resultMessage         = $scoreData['resultMessage']        ?? '';
$technischeKriterien   = $scoreData['technischeKriterien']  ?? ($scoreData['technicalCriteria']   ?? []);
$fundamentaleKriterien = $scoreData['fundamentaleKriterien']?? ($scoreData['fundamentalCriteria'] ?? []);
?>
<main class="flex-grow-1">
	<div class="container pb-5">
		<div class="row">
			<div class="col-12">
				<div class="sticky-top mt-5 bg-white z-3 pb-3">
					<div class="d-flex justify-content-between">
						<h1 class="mb-4">Minervini-Kriterien</h1>
						<div>
						<?php
							if ($symbol) {
								$category = 'Minervini';
								include 'execute/watchlist/watchlist_button.php';
							}
						?>
						</div>
					</div>
					<form method="get">
						<label for="symbol" class="form-label">Symbol eingeben:</label>
						<div class="input-group">
							<input type="text" class="form-control symbol-autocomplete" name="symbol" id="symbol" placeholder="AAPL" value="<?= htmlspecialchars($symbol) ?>" required>
							<button type="submit" class="btn btn-dark">Prüfen</button>
						</div>
					</form>
				</div>
			</div>
		</div>
		
		<?php if ($symbol): ?>
			<div class="row mt-4">
				<div class="col-md-6">
					<?php if (!empty($profile['companyName'])): ?>
						<h2 class="mt-3">
							<?= htmlspecialchars($profile['companyName']) ?> 
							(<?= htmlspecialchars($symbol) ?>)
						</h2>
					<?php endif; ?>
					<?php
						$percentage = ($maxScore > 0) ? ($score / $maxScore) * 100 : 0;
						
						if ($percentage >= 80) {
							$alertClass = 'alert-success';
						} elseif ($percentage >= 50) {
							$alertClass = 'alert-warning';
						} else {
							$alertClass = 'alert-danger';
						}
					?>
					<div class="alert <?= $alertClass ?> d-flex justify-content-start align-items-center">
						  <img
							src="https://financialmodelingprep.com/image-stock/<?= rawurlencode($symbol) ?>.png"
							alt="<?= htmlspecialchars($symbol) ?> Logo"
							class="rounded-circle me-3 bg-white"
							width="60" height="60"
							style="object-fit:cover"
							loading="lazy"
							onerror="this.style.display='none'"
						  >
						<div>
							<h5 class="fw-bold">
								<?= $score ?> / <?= $maxScore ?> Punkte (<?= number_format(($score / $maxScore) * 100, 1) ?> %)
							</h5>
							<?= $resultMessage ?>
						</div>
					</div>
				</div>
				<div class="col-md-6">
					<div class="d-flex justify-content-between fw-bold">
						<small>Technik</small><small><?= $technicalScore ?>/6</small>
					</div>
					<div class="progress" style="height:10px;">
						<div class="progress-bar bg-black" role="progressbar"
							style="width: <?= (int)round($technicalScore/6*100) ?>%;">
						</div>
					</div>
					<div class="d-flex justify-content-between fw-bold mt-3">
						<small>Fundamental</small><small><?= $fundamentalScore ?>/8</small>
					</div>
					<div class="progress" style="height:10px;">
						<div class="progress-bar bg-black" role="progressbar"
							style="width: <?= (int)round($fundamentalScore/8*100) ?>%;">
						</div>
					</div>
				</div>
			</div>
			
			<div class="row mt-3">	
				<div class="col-md-6">
					<h5 class="mb-3">Technische Kriterien</h5>
					<div class="table-responsive">
						<table class="table align-middle">
							<thead class="table-dark">
								<tr>
									<th></th>
									<th>Kriterium</th>
									<th>Wert (Basis)</th>
								</tr>
							</thead>
							<tbody>
								<?php foreach ($technischeKriterien as $krit): ?>
								<tr>
									<td>
										<?php if (!empty($krit['erfüllt'])): ?>
											<i class="fas fa-check text-success" aria-hidden="true"></i> <span class="visually-hidden">Erfüllt</span>
										<?php else: ?>
											<i class="fas fa-xmark text-danger" aria-hidden="true"></i> <span class="visually-hidden">Nicht erfüllt</span>
										<?php endif; ?>
									</td>
									<td><?= htmlspecialchars($krit['Kriterium'] ?? '') ?></td>
									<td><?= htmlspecialchars($krit['wert'] ?? '') ?></td>
								</tr>
								<?php endforeach; ?>
							</tbody>
						</table>
					</div>
				</div>
	
				<div class="col-md-6">
					<h5 class="mb-3">Fundamentale Kriterien</h5>
					<div class="table-responsive">
						<table class="table align-middle">
							<thead class="table-dark">
								<tr>
								<th></th>
								<th>Kriterium</th>
								<th>Wert (Basis)</th>
								</tr>
							</thead>
							<tbody>
								<?php foreach ($fundamentaleKriterien as $krit): ?>
								<tr>
									<td>
										<?php if (!empty($krit['erfüllt'])): ?>
											<i class="fas fa-check text-success" aria-hidden="true"></i> <span class="visually-hidden">Erfüllt</span>
										<?php else: ?>
											<i class="fas fa-xmark text-danger" aria-hidden="true"></i> <span class="visually-hidden">Nicht erfüllt</span>
										<?php endif; ?>
									</td>
									<td><?= htmlspecialchars($krit['Kriterium'] ?? '') ?></td>
									<td><?= htmlspecialchars($krit['wert'] ?? '') ?></td>
								</tr>
								<?php endforeach; ?>
							</tbody>
						</table>
					</div>
				</div>
			<?php endif; ?>
		</div>
	</div>
</main>

<?php require_once __DIR__ . '/views/footer.php'; ?>