<!DOCTYPE html>
<html lang="es">
<head>
    <meta charset="utf-8">
    <title>Lote engorda — {{ $lot->name }}</title>
    <style>
        body { font-family: DejaVu Sans, sans-serif; font-size: 10px; color: #1b4332; }
        h1 { font-size: 16px; margin: 0 0 4px; }
        h2 { font-size: 12px; margin: 14px 0 6px; border-bottom: 1px solid #ccc; padding-bottom: 3px; }
        .muted { color: #555; font-size: 9px; }
        table { width: 100%; border-collapse: collapse; margin-top: 4px; }
        th, td { border: 1px solid #ccc; padding: 4px 5px; text-align: left; }
        th { background: #e8f5e9; font-size: 9px; }
        .num { text-align: right; }
        .kpi-row td { width: 25%; vertical-align: top; border: none; padding: 0 6px 0 0; }
        .kpi-box { border: 1px solid #ccc; padding: 6px; background: #f8faf8; }
        .kpi-label { font-size: 8px; color: #555; }
        .kpi-value { font-size: 12px; font-weight: bold; }
        .profit-positive { color: #2d6a4f; font-weight: bold; }
        .profit-negative { color: #9b2226; font-weight: bold; }
    </style>
</head>
<body>
    <h1>Lote de engorda — {{ $lot->name }}</h1>
    <p class="muted">
        @if(!empty($empresa)){{ $empresa }} · @endif
        Generado: {{ $generated_at }}
    </p>
    <p class="muted">
        Entrada: {{ $lot->entry_date?->format('Y-m-d') }} ·
        Estatus: {{ $lot->status === 'open' ? 'Abierto' : 'Cerrado' }} ·
        Periodo: {{ $evaluation['planned_duration_days'] ?? 90 }} días
        @if(!empty($evaluation['planned_exit_date']))
            · Fin planificado: {{ $evaluation['planned_exit_date'] }}
        @endif
        @if(!empty($evaluation['pasture_accrual_start_date']))
            · Pastura desde: {{ $evaluation['pasture_accrual_start_date'] }}
        @endif
    </p>
    @if($lot->description)
        <p class="muted">{{ $lot->description }}</p>
    @endif

    <table style="border: none; margin-top: 8px;">
        <tr class="kpi-row">
            <td>
                <div class="kpi-box">
                    <div class="kpi-label">Días confinamiento</div>
                    <div class="kpi-value">{{ $evaluation['days_in_confinement'] ?? 0 }}</div>
                </div>
            </td>
            <td>
                <div class="kpi-box">
                    <div class="kpi-label">Inversión alimento</div>
                    <div class="kpi-value">${{ number_format($evaluation['total_feed_cost'] ?? 0, 2) }}</div>
                </div>
            </td>
            <td>
                <div class="kpi-box">
                    <div class="kpi-label">Pastura acumulada</div>
                    <div class="kpi-value">${{ number_format($evaluation['pasture_cost'] ?? 0, 2) }}</div>
                </div>
            </td>
            <td>
                <div class="kpi-box">
                    <div class="kpi-label">Inversión total</div>
                    <div class="kpi-value">${{ number_format($evaluation['total_investment'] ?? 0, 2) }}</div>
                </div>
            </td>
        </tr>
    </table>

    @if($lot->status === 'open' && !empty($projection))
        <h2>Proyección al cierre ({{ $projection['planned_exit_date'] ?? '' }})</h2>
        <table>
            <tr>
                <th>Precio $/kg</th>
                <th>Ingreso estimado</th>
                <th>Costos</th>
                <th>Utilidad neta</th>
            </tr>
            <tr>
                <td class="num">${{ number_format($projection['price_per_kg_used'] ?? 0, 2) }}</td>
                <td class="num">${{ number_format($projection['projected_income'] ?? 0, 2) }}</td>
                <td class="num">${{ number_format($projection['projected_total_cost'] ?? 0, 2) }}</td>
                <td class="num {{ ($projection['projected_net_profit'] ?? 0) >= 0 ? 'profit-positive' : 'profit-negative' }}">
                    ${{ number_format($projection['projected_net_profit'] ?? 0, 2) }}
                </td>
            </tr>
        </table>
    @endif

    <h2>Animales del grupo ({{ count($animals) }})</h2>
    @php $pricePerKg = $animals[0]['price_per_kg_used'] ?? ($lot->target_price_per_kg ?? 0); @endphp
    @if($pricePerKg)
        <p class="muted">Venta estimada a ${{ number_format($pricePerKg, 2) }}/kg (peso actual × precio objetivo)</p>
    @endif
    <table>
        <thead>
            <tr>
                <th>Arete</th>
                <th class="num">Peso entrada</th>
                <th class="num">Peso actual</th>
                <th class="num">Ganancia kg</th>
                <th class="num">Inversión est.</th>
                <th class="num">Venta total</th>
                <th class="num">Ganancia $</th>
            </tr>
        </thead>
        <tbody>
            @foreach($animals as $a)
                <tr>
                    <td>{{ $a['cattle']['ear_tag'] ?? '—' }}</td>
                    <td class="num">{{ $a['entry_weight_kg'] ?? '—' }} kg</td>
                    <td class="num">{{ $a['current_weight_kg'] ?? '—' }} kg</td>
                    <td class="num">{{ $a['weight_gain_kg'] ?? '—' }} kg</td>
                    <td class="num">${{ number_format($a['estimated_total_investment'] ?? 0, 2) }}</td>
                    <td class="num">${{ number_format($a['estimated_sale_total'] ?? 0, 2) }}</td>
                    <td class="num {{ ($a['estimated_net_profit'] ?? 0) >= 0 ? 'profit-positive' : 'profit-negative' }}">
                        ${{ number_format($a['estimated_net_profit'] ?? 0, 2) }}
                    </td>
                </tr>
            @endforeach
        </tbody>
    </table>

    @if(!empty($feedInventory))
        <h2>Inventario de alimento</h2>
        <table>
            <thead><tr><th>Producto</th><th class="num">Disponible</th></tr></thead>
            <tbody>
                @foreach($feedInventory as $row)
                    <tr>
                        <td>{{ $row['name'] }}</td>
                        <td class="num">{{ number_format($row['stock_quantity'], 1) }} {{ $row['unit'] }}</td>
                    </tr>
                @endforeach
            </tbody>
        </table>
    @endif

    <h2>Raciones registradas ({{ count($intakes) }})</h2>
    @if(count($intakes) === 0)
        <p class="muted">Sin raciones.</p>
    @else
        <table>
            <thead>
                <tr>
                    <th>Fecha</th>
                    <th>Cabezas</th>
                    <th>Detalle</th>
                    <th class="num">Total</th>
                </tr>
            </thead>
            <tbody>
                @foreach($intakes as $intake)
                    <tr>
                        <td>{{ $intake['intake_date'] ?? '' }}</td>
                        <td class="num">{{ $intake['animals_count_snapshot'] ?? '—' }}</td>
                        <td>
                            @foreach($intake['items'] ?? [] as $item)
                                {{ $item['material_type']['name'] ?? '' }}: {{ $item['quantity'] }}{{ !$loop->last ? ' · ' : '' }}
                            @endforeach
                        </td>
                        <td class="num">${{ number_format($intake['total_cost'] ?? 0, 2) }}</td>
                    </tr>
                @endforeach
            </tbody>
        </table>
    @endif
</body>
</html>
