<!DOCTYPE html>
<html>
<head>
    <title>{{ ($member->gender == 'Male' ? 'Mr' : 'Mrs') }} {{ $member->first_name }} - {{ \Carbon\Carbon::parse($month.'-01')->format('F Y') }}</title>
    <style>
        body { font-family: sans-serif; font-size: 12px; margin: 20px; }
        h2, h3 { text-align: center; margin-bottom: 10px; }
        table { width: 100%; border-collapse: collapse; margin-bottom: 15px; }
        th, td { border: 1px solid #ccc; padding: 6px; text-align: center; }
        th { background-color: #f2f2f2; }
        .positive { color: green; font-weight: bold; }
        .negative { color: red; font-weight: bold; }
        .stable { color: #555; }
        header { text-align: center; margin-bottom: 20px; }
        .instructions { font-size: 11px; margin: 5px 0 15px; text-align: center; color: #333; }
        footer { text-align: center; font-size: 10px; color: #999; margin-top: 20px; }
    </style>
</head>
<body>

<header>
    <h2>Iron Ground Fitness Gym</h2>
    <p>Monthly Progress Report</p>
</header>

<div class="instructions">
    Dear {{ $member->first_name }}, this report summarizes your progress for 
    {{ \Carbon\Carbon::parse($month.'-01')->format('F Y') }}.<br>
    Follow your personalized diet and workout plan. Contact your trainer for any guidance.
</div>

{{-- Stats Cards --}}
@php
    $weightGainDays = 0;
    $weightLostDays = 0;
    $totalGain = 0;
    $totalLoss = 0;
    $prevWeight = null;

    foreach($progresses as $progress) {
        if($progress->weight !== null) {
            if($prevWeight !== null) {
                $diff = $progress->weight - $prevWeight;
                if($diff > 0) { $weightGainDays++; $totalGain += $diff; }
                elseif($diff < 0) { $weightLostDays++; $totalLoss += abs($diff); }
            }
            $prevWeight = $progress->weight;
        }
    }

    $netChange = ($progresses->last()->weight ?? 0) - ($progresses->first()->weight ?? 0);
    $bmiChange = ($progresses->last()->BMI ?? 0) - ($progresses->first()->BMI ?? 0);
    $genderPrefix = strtolower($member->gender) == 'male' ? 'Mr.' : 'Mrs.';
@endphp

<table>
    <tr>
        <td>Weight Gained Days: {{ $weightGainDays }} ({{ $totalGain }} kg)</td>
        <td>Weight Lost Days: {{ $weightLostDays }} ({{ $totalLoss }} kg)</td>
        <td>Net Weight Change: {{ $netChange }} kg</td>
        <td>BMI Change: {{ $bmiChange }}</td>
    </tr>
</table>

<h3>Daily Progress</h3>
<table>
    <thead>
        <tr>
            <th>Date</th>
            <th>Weight (kg)</th>
            <th>BMI</th>
            <th>Waist (cm)</th>
            <th>Hip (cm)</th>
            <th>Sleep (hrs)</th>
            <th>Trainer</th>
            <th>Workout Completed</th>
            <th>Diet Followed</th>
            <th>Hydration</th>
            <th>Notes</th>
        </tr>
    </thead>
    <tbody>
        @forelse($progresses as $progress)
        <tr>
            <td>{{ \Carbon\Carbon::parse($progress->date)->format('d M Y') }}</td>
            <td>{{ $progress->weight ?? '-' }}</td>
            <td>{{ $progress->BMI ?? '-' }}</td>
            <td>{{ $progress->waist ?? '-' }}</td>
            <td>{{ $progress->hip ?? '-' }}</td>
            <td>{{ $progress->sleep ?? '-' }}</td>
            <td>{{ $progress->trainer_name ?? 'N/A' }}</td>
            <td>{{ $progress->workout_completed ? 'Yes' : 'No' }}</td>
            <td>{{ $progress->diet_followed ? 'Yes' : 'No' }}</td>
            <td>{{ $progress->hydration_ok ? 'OK' : 'Not OK' }}</td>
            <td>{{ $progress->notes ?? '-' }}</td>
        </tr>
        @empty
        <tr>
            <td colspan="11">No progress records for this month</td>
        </tr>
        @endforelse
    </tbody>
</table>

<h3>Monthly Summary</h3>
<table>
    <thead>
        <tr>
            <th>Metric</th>
            <th>Start</th>
            <th>End</th>
            <th>Change</th>
            <th>Status</th>
        </tr>
    </thead>
    <tbody>
        @php
            $metrics = ['weight','BMI','waist','hip','sleep'];
        @endphp
        @foreach($metrics as $metric)
            @php
                $start = $progresses->first()->{$metric} ?? null;
                $end = $progresses->last()->{$metric} ?? null;
                $change = ($end && $start) ? $end - $start : 0;
                $status = $change > 0 ? 'Increased' : ($change < 0 ? 'Decreased' : 'Stable');
            @endphp
            <tr>
                <td>{{ ucfirst($metric) }}</td>
                <td>{{ $start ?? '-' }}</td>
                <td>{{ $end ?? '-' }}</td>
                <td>{{ $change ?? 0 }}</td>
                <td class="{{ $status == 'Stable' ? 'stable' : ($status == 'Increased' ? 'positive' : 'negative') }}">{{ $status }}</td>
            </tr>
        @endforeach
    </tbody>
</table>

<footer>
    {{ $genderPrefix }} {{ $member->first_name }} {{ $member->last_name }} - {{ \Carbon\Carbon::parse($month.'-01')->format('F Y') }} | Developed by Zevenx Asia
</footer>

</body>
</html>
