<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<style>
    body { font-family: Arial, sans-serif; font-size: 11px; color: #1e293b; margin: 0; padding: 20px; }
    h1 { font-size: 18px; color: #0f172a; margin: 0 0 4px; }
    h2 { font-size: 13px; color: #1e40af; margin: 16px 0 6px; border-bottom: 1px solid #e2e8f0; padding-bottom: 4px; }
    .header { display: flex; justify-content: space-between; margin-bottom: 16px; }
    .meta { color: #64748b; font-size: 10px; margin-bottom: 4px; }
    table { width: 100%; border-collapse: collapse; margin-bottom: 12px; }
    th { background: #f1f5f9; color: #475569; font-size: 9px; text-transform: uppercase; padding: 5px 8px; text-align: left; border-bottom: 2px solid #e2e8f0; }
    td { padding: 5px 8px; border-bottom: 1px solid #f1f5f9; vertical-align: top; font-size: 10px; }
    .text-right { text-align: right; }
    tfoot td { font-weight: bold; border-top: 2px solid #cbd5e1; background: #f8fafc; }
    .kpi-grid { display: flex; gap: 12px; margin-bottom: 16px; }
    .kpi-box { flex: 1; border: 1px solid #e2e8f0; border-radius: 6px; padding: 10px; text-align: center; }
    .kpi-val { font-size: 18px; font-weight: 700; color: #0f172a; }
    .kpi-label { font-size: 9px; color: #94a3b8; margin-top: 2px; }
    .green { color: #16a34a; }
    .red { color: #dc2626; }
    .orange { color: #ea580c; }
    .blue { color: #2563eb; }
    .purple { color: #7c3aed; }
    .amber { color: #d97706; }
    .summary-box { max-width: 320px; margin-left: auto; border: 1px solid #e2e8f0; border-radius: 6px; padding: 10px; }
    .summary-row { display: flex; justify-content: space-between; padding: 3px 0; border-bottom: 1px solid #f1f5f9; font-size: 10px; }
    .summary-total { font-weight: 700; border-top: 2px solid #cbd5e1; padding-top: 6px; margin-top: 4px; display: flex; justify-content: space-between; }
    .footer { margin-top: 24px; font-size: 9px; color: #94a3b8; text-align: center; border-top: 1px solid #e2e8f0; padding-top: 8px; }
    .page-break { page-break-before: always; }
</style>
</head>
<body>

<div class="header">
    <div>
        <h1>Gross Profit Report</h1>
        <div class="meta">{{ $job->job_number }} · {{ $job->contact?->full_name }} · {{ $job->stageLabel() }}</div>
        <div class="meta">Generated: {{ now()->format('d M Y, g:ia') }}</div>
    </div>
    <div style="text-align:right">
        <div style="font-size:22px; font-weight:700; color:{{ $grossProfit >= 0 ? '#16a34a' : '#dc2626' }}">{{ $margin }}%</div>
        <div style="font-size:9px; color:#94a3b8">Gross Margin</div>
    </div>
</div>

<div class="kpi-grid">
    <div class="kpi-box">
        <div class="kpi-val">${{ number_format($revenue) }}</div>
        <div class="kpi-label">Revenue (ex. GST)</div>
    </div>
    <div class="kpi-box">
        <div class="kpi-val" style="color:#ea580c">${{ number_format($totalCost) }}</div>
        <div class="kpi-label">Total Costs</div>
    </div>
    <div class="kpi-box">
        <div class="kpi-val" style="color:{{ $grossProfit >= 0 ? '#16a34a' : '#dc2626' }}">${{ number_format($grossProfit) }}</div>
        <div class="kpi-label">Gross Profit</div>
    </div>
    <div class="kpi-box">
        <div class="kpi-val" style="color:{{ $margin >= 30 ? '#16a34a' : ($margin >= 15 ? '#d97706' : '#dc2626') }}">{{ $margin }}%</div>
        <div class="kpi-label">Gross Margin</div>
    </div>
</div>

{{-- Accepted Variants --}}
@if(isset($acceptedVariants) && $acceptedVariants->count())
<h2>Accepted Variant Revenue</h2>
<table>
    <thead>
        <tr>
            <th>Variant</th>
            <th>Linked Quote</th>
            <th>Accepted By</th>
            <th>Date</th>
            <th class="text-right">Price Adjustment</th>
        </tr>
    </thead>
    <tbody>
        @foreach($acceptedVariants as $v)
        <tr>
            <td>{{ $v->name }}</td>
            <td>{{ $v->quote?->quote_number ?? '—' }}</td>
            <td>{{ $v->respondedBy?->name ?? '—' }}</td>
            <td>{{ $v->responded_at?->format('d M Y') ?? '—' }}</td>
            <td class="text-right" style="color:#818cf8">{{ $v->price_adjustment >= 0 ? '+' : '' }}${{ number_format($v->price_adjustment, 2) }}</td>
        </tr>
        @endforeach
    </tbody>
    <tfoot>
        <tr>
            <td colspan="4" class="text-right">Total Variant Revenue</td>
            <td class="text-right" style="color:#818cf8">+${{ number_format($variantRevenue, 2) }}</td>
        </tr>
    </tfoot>
</table>
@endif

{{-- Invoice Deductions --}}
@if(isset($invoiceLines) && $invoiceLines->count())
<h2>Invoice Deductions <span style="font-weight:normal; font-size:10px; color:#94a3b8">(reduces gross profit)</span></h2>
<table>
    <thead>
        <tr>
            <th>Invoice #</th>
            <th>Date</th>
            <th>Note</th>
            <th>Added By</th>
            <th class="text-right">Amount (incl. GST)</th>
        </tr>
    </thead>
    <tbody>
        @foreach($invoiceLines as $inv)
        <tr>
            <td style="font-family:monospace">{{ $inv['invoice_number'] }}</td>
            <td>{{ $inv['invoice_date'] }}</td>
            <td>{{ $inv['note'] ?: '—' }}</td>
            <td style="color:#64748b">{{ $inv['added_by'] }}</td>
            <td class="text-right amber">-${{ number_format($inv['amount'], 2) }}</td>
        </tr>
        @endforeach
    </tbody>
    <tfoot>
        <tr>
            <td colspan="4" class="text-right">Total Invoice Deductions</td>
            <td class="text-right amber">-${{ number_format($invoiceDeductions, 2) }}</td>
        </tr>
    </tfoot>
</table>
@endif

{{-- Works Order Fault Deductions --}}
@if(isset($faultLines) && $faultLines->count())
<h2>Works Order Fault Costs <span style="font-weight:normal; font-size:10px; color:#94a3b8">(reduces gross profit)</span></h2>
<table>
    <thead>
        <tr>
            <th>Works Order</th>
            <th>#</th>
            <th>Fault Type</th>
            <th>Approval</th>
            <th class="text-right">Cost</th>
        </tr>
    </thead>
    <tbody>
        @foreach($faultLines as $f)
        <tr>
            <td style="font-family:monospace; font-size:9px">{{ $f['wo_number'] }}</td>
            <td style="color:#64748b">Fault {{ $f['fault_num'] }}</td>
            <td>{{ $f['fault_type'] }}</td>
            <td>{{ ucfirst($f['approved'] ?? '—') }}</td>
            <td class="text-right orange">-${{ number_format($f['cost'], 2) }}</td>
        </tr>
        @endforeach
    </tbody>
    <tfoot>
        <tr>
            <td colspan="4" class="text-right">Total Fault Deductions</td>
            <td class="text-right orange">-${{ number_format($faultDeductions, 2) }}</td>
        </tr>
    </tfoot>
</table>
@endif

{{-- Materials --}}
<h2>Material Costs (Quote Items)</h2>
@if($allItems->count())
<table>
    <thead>
        <tr>
            <th>Description</th>
            <th>Item Code</th>
            <th class="text-right">Qty</th>
            <th class="text-right">W/Sale</th>
            <th class="text-right">Retail</th>
            <th class="text-right">Total W/Sale</th>
            <th class="text-right">Total Retail</th>
        </tr>
    </thead>
    <tbody>
        @foreach($allItems as $item)
        <tr>
            <td>{{ $item->description }}</td>
            <td style="color:#64748b; font-size:9px">{{ $item->item_code ?? '—' }}</td>
            <td class="text-right">{{ number_format($item->qty, 2) }}</td>
            <td class="text-right">${{ number_format($item->wsale_price ?? 0, 2) }}</td>
            <td class="text-right">${{ number_format($item->retail_price ?? $item->unit_price ?? 0, 2) }}</td>
            <td class="text-right" style="color:#ea580c">${{ number_format($item->total_wsale ?? 0, 2) }}</td>
            <td class="text-right" style="color:#16a34a">${{ number_format($item->total_retail ?? $item->total ?? 0, 2) }}</td>
        </tr>
        @endforeach
    </tbody>
    <tfoot>
        <tr>
            <td colspan="5" class="text-right">Totals</td>
            <td class="text-right orange">${{ number_format($matCost, 2) }}</td>
            <td class="text-right green">${{ number_format($revenue, 2) }}</td>
        </tr>
    </tfoot>
</table>
@else
<p style="color:#94a3b8">No quote items.</p>
@endif

{{-- Labour --}}
<h2>Labour Costs</h2>
@if($labourLines->count())
<table>
    <thead>
        <tr>
            <th>Staff</th>
            <th>Date</th>
            <th class="text-right">Hours</th>
            <th class="text-right">Rate/hr</th>
            <th class="text-right">Cost</th>
        </tr>
    </thead>
    <tbody>
        @foreach($labourLines as $line)
        <tr>
            <td>{{ $line['staff'] }}</td>
            <td>{{ $line['date'] }}</td>
            <td class="text-right">{{ number_format($line['hours'], 2) }}h</td>
            <td class="text-right">${{ number_format($line['rate'], 2) }}</td>
            <td class="text-right blue">${{ number_format($line['cost'], 2) }}</td>
        </tr>
        @endforeach
    </tbody>
    <tfoot>
        <tr>
            <td colspan="4" class="text-right">Total Labour</td>
            <td class="text-right blue">${{ number_format($labourCost, 2) }}</td>
        </tr>
    </tfoot>
</table>
@else
<p style="color:#94a3b8">No time entries recorded.</p>
@endif

{{-- PO Allocations --}}
@if($poLines->count())
<h2>Inventory &amp; Purchase Order Costs</h2>
<table>
    <thead>
        <tr>
            <th>Description</th>
            <th>Source</th>
            <th class="text-right">Qty</th>
            <th class="text-right">Unit Cost</th>
            <th class="text-right">Total</th>
        </tr>
    </thead>
    <tbody>
        @foreach($poLines as $line)
        <tr>
            <td>{{ $line['description'] }}</td>
            <td style="font-size:9px; color:#64748b">{{ $line['source'] ?? '—' }}</td>
            <td class="text-right">{{ number_format($line['qty'], 2) }}</td>
            <td class="text-right">${{ number_format($line['unit_cost'], 2) }}</td>
            <td class="text-right purple">${{ number_format($line['total'], 2) }}</td>
        </tr>
        @endforeach
    </tbody>
    <tfoot>
        <tr>
            <td colspan="4" class="text-right">Total PO Allocations</td>
            <td class="text-right purple">${{ number_format($poCost, 2) }}</td>
        </tr>
    </tfoot>
</table>
@endif

{{-- Summary --}}
<h2>Profit Summary</h2>
<div class="summary-box">
    <div class="summary-row"><span>Revenue (ex. GST)</span><span>${{ number_format($revenue, 2) }}</span></div>
    <div class="summary-row"><span>Material Cost</span><span class="orange">-${{ number_format($matCost, 2) }}</span></div>
    <div class="summary-row"><span>Labour Cost</span><span class="blue">-${{ number_format($labourCost, 2) }}</span></div>
    @if($poCost > 0)
    <div class="summary-row"><span>Inventory &amp; PO Costs</span><span class="purple">-${{ number_format($poCost, 2) }}</span></div>
    @endif
    @if(isset($invoiceDeductions) && $invoiceDeductions > 0)
    <div class="summary-row"><span>Invoice Deductions</span><span class="amber">-${{ number_format($invoiceDeductions, 2) }}</span></div>
    @endif
    @if(isset($faultDeductions) && $faultDeductions > 0)
    <div class="summary-row"><span>WO Fault Costs</span><span class="orange">-${{ number_format($faultDeductions, 2) }}</span></div>
    @endif
    <div class="summary-total">
        <span style="color:{{ $grossProfit >= 0 ? '#16a34a' : '#dc2626' }}">Gross Profit</span>
        <span style="color:{{ $grossProfit >= 0 ? '#16a34a' : '#dc2626' }}">${{ number_format($grossProfit, 2) }}</span>
    </div>
    <div class="summary-total" style="font-size:10px; border-top:none; padding-top:4px;">
        <span style="color:{{ $margin >= 30 ? '#16a34a' : ($margin >= 15 ? '#d97706' : '#dc2626') }}">Gross Margin</span>
        <span style="color:{{ $margin >= 30 ? '#16a34a' : ($margin >= 15 ? '#d97706' : '#dc2626') }}">{{ $margin }}%</span>
    </div>
</div>

<div class="footer">Degabriele Kitchens CRM &middot; Gross Profit Report &middot; {{ $job->job_number }} &middot; {{ now()->format('d M Y') }}</div>
</body>
</html>
