<!DOCTYPE html>
<html lang="es">
<head>
    <meta charset="utf-8">
    <title>Expediente {{ $sheet['animal']['ear_tag'] ?? '' }}</title>
    <style>
        body { font-family: DejaVu Sans, sans-serif; font-size: 11px; color: #1b4332; }
        h1 { font-size: 17px; margin: 0 0 4px; }
        h2 { font-size: 13px; margin: 16px 0 6px; border-bottom: 1px solid #ccc; padding-bottom: 4px; }
        .muted { color: #555; font-size: 10px; }
        table { width: 100%; border-collapse: collapse; margin-top: 6px; }
        th, td { border: 1px solid #ccc; padding: 5px 6px; text-align: left; }
        th { background: #e8f5e9; font-size: 10px; }
        .summary td:last-child { text-align: right; }
        .profit-positive { color: #2d6a4f; font-weight: bold; }
        .profit-negative { color: #9b2226; font-weight: bold; }
        .kpi-row td { width: 25%; vertical-align: top; border: none; padding: 0 8px 0 0; }
        .kpi-box { border: 1px solid #ccc; padding: 8px; background: #f8faf8; }
        .kpi-label { font-size: 9px; color: #555; }
        .kpi-value { font-size: 13px; font-weight: bold; }
    </style>
</head>
<body>
    <h1>Expediente — Arete {{ $sheet['animal']['ear_tag'] }}</h1>
    <p class="muted">
        @if(!empty($empresa)){{ $empresa }} · @endif
        Generado: {{ $generated_at }}
    </p>
    <p class="muted">
        Estatus: {{ ucfirst($sheet['animal']['status'] ?? '') }} ·
        {{ $sheet['metrics']['location_label'] ?? '' }}
    </p>

    <table style="border: none; margin-top: 10px;">
        <tr class="kpi-row">
            <td>
                <div class="kpi-box">
                    <div class="kpi-label">Último peso</div>
                    <div class="kpi-value">{{ $sheet['metrics']['last_weight_kg'] ?? '—' }} kg</div>
                </div>
            </td>
            <td>
                <div class="kpi-box">
                    <div class="kpi-label">Último GDP</div>
                    <div class="kpi-value">{{ $sheet['metrics']['last_daily_weight_gain'] ?? '—' }} kg/d</div>
                </div>
            </td>
            <td>
                <div class="kpi-box">
                    <div class="kpi-label">Meta GDP efectiva</div>
                    <div class="kpi-value">{{ $sheet['metrics']['effective_min_gdp_kg_per_day'] ?? '—' }} kg/d</div>
                </div>
            </td>
            <td>
                <div class="kpi-box">
                    <div class="kpi-label">Ganancia de peso</div>
                    <div class="kpi-value">{{ $sheet['metrics']['lifetime_weight_gain_kg'] ?? '—' }} kg</div>
                </div>
            </td>
        </tr>
    </table>

    <h2>Datos generales</h2>
    <table class="summary">
        <tr><td>Raza</td><td>{{ $sheet['animal']['breed'] ?? '—' }}</td></tr>
        <tr><td>Género</td><td>{{ ($sheet['animal']['gender'] ?? '') === 'female' ? 'Hembra' : 'Macho' }}</td></tr>
        <tr><td>Nacimiento</td><td>{{ $sheet['animal']['birth_date'] ?? '—' }}</td></tr>
        <tr><td>Edad</td><td>{{ $sheet['metrics']['age_months'] ?? '—' }} meses</td></tr>
        <tr><td>Madre</td><td>{{ $sheet['animal']['mother']['ear_tag'] ?? '—' }}</td></tr>
        <tr><td>Notas</td><td>{{ $sheet['animal']['notes'] ?? '—' }}</td></tr>
    </table>

    @php $p = $sheet['metrics']['profitability'] ?? null; @endphp
    @if($p)
        <h2>Dinero invertido y resultado</h2>
        <table class="summary">
            <tr><td>Alimento (engorda asignado)</td><td>${{ number_format($p['feed_cost'], 2) }}</td></tr>
            <tr><td>Salud</td><td>${{ number_format($p['health_cost'], 2) }}</td></tr>
            <tr><td><strong>Total invertido</strong></td><td><strong>${{ number_format($p['money_invested'] ?? $p['total_cost'], 2) }}</strong></td></tr>
            <tr><td>Ingresos (venta)</td><td>${{ number_format($p['income'], 2) }}</td></tr>
            <tr>
                <td>Utilidad neta</td>
                <td class="{{ ($p['net_profit'] ?? 0) >= 0 ? 'profit-positive' : 'profit-negative' }}">
                    ${{ number_format($p['net_profit'], 2) }}
                </td>
            </tr>
            @if(!empty($p['weight_gain_kg']))
                <tr><td>Kg ganados</td><td>{{ number_format($p['weight_gain_kg'], 2) }}</td></tr>
                <tr><td>Costo / kg ganado</td><td>${{ $p['cost_per_kg_gained'] !== null ? number_format($p['cost_per_kg_gained'], 2) : '—' }}</td></tr>
                <tr><td>Utilidad / kg ganado</td><td>${{ $p['profit_per_kg_gained'] !== null ? number_format($p['profit_per_kg_gained'], 2) : '—' }}</td></tr>
            @endif
        </table>
    @endif

    @if(!empty($sheet['weight_records']))
        @php
            $weights = collect($sheet['weight_records'])->sortBy('weighed_at')->values()->all();
            $gdpTarget = $sheet['metrics']['development_chart']['target_min_gdp_kg_per_day']
                ?? $sheet['metrics']['effective_min_gdp_kg_per_day']
                ?? null;
        @endphp
        <h2>Desarrollo (pesajes)</h2>
        <table>
            <thead>
                <tr>
                    <th>Fecha</th>
                    <th>Peso kg</th>
                    <th>Ganancia kg</th>
                    <th>GDP kg/día</th>
                </tr>
            </thead>
            <tbody>
                @foreach($weights as $row)
                    <tr>
                        <td>{{ $row['weighed_at'] }}</td>
                        <td style="text-align: right">{{ number_format($row['weight_kg'], 2) }}</td>
                        <td style="text-align: right">{{ $row['weight_gain_kg'] !== null ? number_format($row['weight_gain_kg'], 2) : '—' }}</td>
                        <td style="text-align: right">
                            @if($row['daily_weight_gain'] !== null && $gdpTarget !== null && $row['daily_weight_gain'] < $gdpTarget)
                                <span style="color: #9b2226">{{ number_format($row['daily_weight_gain'], 2) }}</span>
                            @else
                                {{ $row['daily_weight_gain'] !== null ? number_format($row['daily_weight_gain'], 2) : '—' }}
                            @endif
                        </td>
                    </tr>
                @endforeach
            </tbody>
        </table>
    @endif

    @if(!empty($sheet['fattening_stints']))
        <h2>Engorda</h2>
        <table>
            <thead>
                <tr><th>Lote</th><th>Entrada</th><th>Salida</th><th>Días</th><th>Peso inicio</th><th>Peso fin</th></tr>
            </thead>
            <tbody>
                @foreach($sheet['fattening_stints'] as $s)
                    <tr>
                        <td>{{ $s['lot_name'] }}{{ !empty($s['is_active']) ? ' (actual)' : '' }}</td>
                        <td>{{ $s['entry_date'] }}</td>
                        <td>{{ $s['exit_date'] ?? '—' }}</td>
                        <td>{{ $s['days_in_confinement'] }}</td>
                        <td>{{ $s['entry_weight_kg'] }}</td>
                        <td>{{ $s['exit_weight_kg'] ?? '—' }}</td>
                    </tr>
                @endforeach
            </tbody>
        </table>
    @endif

    @if(!empty($sheet['health_events']))
        <h2>Salud</h2>
        <table>
            <thead>
                <tr><th>Fecha</th><th>Tipo</th><th>Concepto</th><th>Costo</th></tr>
            </thead>
            <tbody>
                @foreach(array_slice($sheet['health_events'], 0, 15) as $h)
                    <tr>
                        <td>{{ $h['event_date'] }}</td>
                        <td>{{ $h['event_type'] }}</td>
                        <td>{{ $h['title'] }}</td>
                        <td style="text-align: right">{{ $h['cost'] !== null ? '$'.number_format($h['cost'], 2) : '—' }}</td>
                    </tr>
                @endforeach
            </tbody>
        </table>
    @endif

    @if(!empty($sheet['location_movements']))
        <h2>Movimientos (últimos 10)</h2>
        <table>
            <thead>
                <tr><th>Fecha</th><th>Motivo</th><th>Desde</th><th>Hacia</th></tr>
            </thead>
            <tbody>
                @foreach(array_slice($sheet['location_movements'], 0, 10) as $m)
                    <tr>
                        <td>{{ $m['moved_at'] }}</td>
                        <td>{{ $m['reason_label'] ?? $m['reason'] }}</td>
                        <td>{{ $m['from_label'] ?? '—' }}</td>
                        <td>{{ $m['to_label'] ?? '—' }}</td>
                    </tr>
                @endforeach
            </tbody>
        </table>
    @endif
</body>
</html>
