@extends('layouts.app')
@section('title', 'Gross Profit — ' . $job->job_number)
@section('breadcrumb')<a href="{{ route('reports.gross-profit') }}" class="hover:text-white">Gross Profit</a> <i class="fas fa-chevron-right text-xs mx-1 text-slate-600"></i> <span class="text-white">{{ $job->job_number }}</span>@endsection

@section('content')

<div class="flex items-center justify-between mb-6">
    <div>
        <h1 class="mb-1">Gross Profit Report</h1>
        <p>{{ $job->job_number }} · {{ $job->contact?->full_name }} · {{ $job->stageLabel() }}</p>
    </div>
    <div class="flex gap-2">
        <a href="{{ route('reports.gross-profit.pdf', $job) }}" class="btn" style="background:#7c3aed;color:#ede9fe;">
            <i class="fas fa-file-pdf mr-1"></i> Export PDF
        </a>
        <a href="{{ route('reports.gross-profit') }}" class="btn btn-secondary">
            <i class="fas fa-arrow-left mr-1"></i> Back to Report
        </a>
    </div>
</div>

{{-- KPI Summary --}}
<div class="grid grid-cols-2 lg:grid-cols-4 gap-4 mb-6">
    <div class="card p-4 text-center">
        <div class="text-xs text-slate-400 mb-1">Revenue</div>
        <div class="text-2xl font-bold text-white">${{ number_format($revenue) }}</div>
        <div class="text-xs text-slate-500 mt-1">
            Ex. GST
            @if($variantRevenue > 0)
            · <span class="text-indigo-400">incl. ${{ number_format($variantRevenue) }} variants</span>
            @endif
        </div>
    </div>
    <div class="card p-4 text-center">
        <div class="text-xs text-slate-400 mb-1">Total Costs</div>
        <div class="text-2xl font-bold text-orange-400">${{ number_format($totalCost) }}</div>
        <div class="text-xs text-slate-500 mt-1">Mat + Labour + PO</div>
    </div>
    <div class="card p-4 text-center border {{ $grossProfit >= 0 ? 'border-green-700 bg-green-900/10' : 'border-red-700 bg-red-900/10' }}">
        <div class="text-xs text-slate-400 mb-1">Gross Profit</div>
        <div class="text-2xl font-bold {{ $grossProfit >= 0 ? 'text-green-400' : 'text-red-400' }}">${{ number_format($grossProfit) }}</div>
    </div>
    <div class="card p-4 text-center">
        <div class="text-xs text-slate-400 mb-1">Gross Margin</div>
        <div class="text-2xl font-bold {{ $margin >= 30 ? 'text-green-400' : ($margin >= 15 ? 'text-yellow-400' : 'text-red-400') }}">{{ $margin }}%</div>
        <div class="w-full h-2 bg-slate-700 rounded-full overflow-hidden mt-2">
            <div class="h-full {{ $margin >= 30 ? 'bg-green-500' : ($margin >= 15 ? 'bg-yellow-400' : 'bg-red-500') }} rounded-full" style="width:{{ min(100, max(0, $margin)) }}%"></div>
        </div>
    </div>
</div>

{{-- Accepted Variants Revenue --}}
@if($acceptedVariants->count())
<div class="card overflow-hidden mb-6">
    <div class="p-4 border-b border-slate-700 flex items-center justify-between">
        <h3><i class="fas fa-code-branch text-indigo-400 mr-2"></i>Accepted Variant Revenue</h3>
        <span class="text-sm font-semibold text-indigo-400">+${{ number_format($variantRevenue, 2) }}</span>
    </div>
    <table class="w-full text-sm">
        <thead>
            <tr class="text-xs text-slate-400 border-b border-slate-700">
                <th class="px-4 py-2 text-left">Variant</th>
                <th class="px-4 py-2 text-left">Linked Quote</th>
                <th class="px-4 py-2 text-left">Accepted By</th>
                <th class="px-4 py-2 text-left">Date</th>
                <th class="px-4 py-2 text-right">Price Adjustment</th>
            </tr>
        </thead>
        <tbody>
            @foreach($acceptedVariants as $v)
            <tr class="table-row border-b border-slate-800">
                <td class="px-4 py-2 text-white">{{ $v->name }}</td>
                <td class="px-4 py-2 text-slate-400">
                    @if($v->quote)
                    <a href="{{ route('quotes.show', $v->quote) }}" class="text-blue-400 hover:text-blue-300">{{ $v->quote->quote_number }}</a>
                    @else
                    <span class="text-slate-600">—</span>
                    @endif
                </td>
                <td class="px-4 py-2 text-slate-400">{{ $v->respondedBy?->name ?? '—' }}</td>
                <td class="px-4 py-2 text-slate-400">{{ $v->responded_at?->format('d M Y') ?? '—' }}</td>
                <td class="px-4 py-2 text-right font-medium {{ $v->price_adjustment >= 0 ? 'text-indigo-400' : 'text-red-400' }}">
                    {{ $v->price_adjustment >= 0 ? '+' : '' }}${{ number_format($v->price_adjustment, 2) }}
                </td>
            </tr>
            @endforeach
        </tbody>
        <tfoot>
            <tr class="font-semibold border-t-2 border-slate-600">
                <td colspan="4" class="px-4 py-2 text-right text-slate-300">Total Variant Revenue</td>
                <td class="px-4 py-2 text-right text-indigo-400">+${{ number_format($variantRevenue, 2) }}</td>
            </tr>
        </tfoot>
    </table>
</div>
@endif

{{-- Job Invoice Deductions --}}
@if($invoiceLines->count())
<div class="card overflow-hidden mb-6">
    <div class="p-4 border-b border-slate-700 flex items-center justify-between">
        <h3><i class="fas fa-file-invoice-dollar text-amber-400 mr-2"></i>Invoice Deductions <span class="text-xs text-slate-500 font-normal ml-2">(reduces gross profit)</span></h3>
        <span class="text-sm font-semibold text-amber-400">-${{ number_format($invoiceDeductions, 2) }}</span>
    </div>
    <table class="w-full text-sm">
        <thead>
            <tr class="text-xs text-slate-400 border-b border-slate-700">
                <th class="px-4 py-2 text-left">Invoice #</th>
                <th class="px-4 py-2 text-left">Date</th>
                <th class="px-4 py-2 text-left">Note</th>
                <th class="px-4 py-2 text-left">Added By</th>
                <th class="px-4 py-2 text-right">Amount (incl GST)</th>
            </tr>
        </thead>
        <tbody>
            @foreach($invoiceLines as $inv)
            <tr class="table-row border-b border-slate-800">
                <td class="px-4 py-2 text-white font-mono">{{ $inv['invoice_number'] }}</td>
                <td class="px-4 py-2 text-slate-400">{{ $inv['invoice_date'] }}</td>
                <td class="px-4 py-2 text-slate-400">{{ $inv['note'] ?: '—' }}</td>
                <td class="px-4 py-2 text-slate-500 text-xs">{{ $inv['added_by'] }}</td>
                <td class="px-4 py-2 text-right text-amber-400 font-medium">-${{ number_format($inv['amount'], 2) }}</td>
            </tr>
            @endforeach
        </tbody>
        <tfoot>
            <tr class="font-semibold border-t-2 border-slate-600">
                <td colspan="4" class="px-4 py-2 text-right text-slate-300">Total Invoice Deductions</td>
                <td class="px-4 py-2 text-right text-amber-400">-${{ number_format($invoiceDeductions, 2) }}</td>
            </tr>
        </tfoot>
    </table>
</div>
@endif

{{-- Works Order Fault Deductions --}}
@if($faultLines->count())
<div class="card overflow-hidden mb-6">
    <div class="p-4 border-b border-slate-700 flex items-center justify-between">
        <h3><i class="fas fa-exclamation-triangle text-orange-400 mr-2"></i>Works Order Fault Costs <span class="text-xs text-slate-500 font-normal ml-2">(reduces gross profit)</span></h3>
        <span class="text-sm font-semibold text-orange-400">-${{ number_format($faultDeductions, 2) }}</span>
    </div>
    <table class="w-full text-sm">
        <thead>
            <tr class="text-xs text-slate-400 border-b border-slate-700">
                <th class="px-4 py-2 text-left">Works Order</th>
                <th class="px-4 py-2 text-left">#</th>
                <th class="px-4 py-2 text-left">Fault Type</th>
                <th class="px-4 py-2 text-left">Approval</th>
                <th class="px-4 py-2 text-right">Cost</th>
            </tr>
        </thead>
        <tbody>
            @foreach($faultLines as $f)
            <tr class="table-row border-b border-slate-800">
                <td class="px-4 py-2 text-blue-300 font-mono text-xs">{{ $f['wo_number'] }}</td>
                <td class="px-4 py-2 text-slate-500">Fault {{ $f['fault_num'] }}</td>
                <td class="px-4 py-2 text-white">{{ $f['fault_type'] }}</td>
                <td class="px-4 py-2">
                    @if($f['approved'] === 'approved') <span class="text-xs text-green-400"><i class="fas fa-check mr-1"></i>Approved</span>
                    @elseif($f['approved'] === 'rejected') <span class="text-xs text-red-400"><i class="fas fa-times mr-1"></i>Rejected</span>
                    @elseif($f['approved'] === 'pending') <span class="text-xs text-amber-400"><i class="fas fa-clock mr-1"></i>Pending</span>
                    @else <span class="text-xs text-slate-500">—</span> @endif
                </td>
                <td class="px-4 py-2 text-right text-orange-400 font-medium">-${{ number_format($f['cost'], 2) }}</td>
            </tr>
            @endforeach
        </tbody>
        <tfoot>
            <tr class="font-semibold border-t-2 border-slate-600">
                <td colspan="4" class="px-4 py-2 text-right text-slate-300">Total Fault Deductions</td>
                <td class="px-4 py-2 text-right text-orange-400">-${{ number_format($faultDeductions, 2) }}</td>
            </tr>
        </tfoot>
    </table>
</div>
@endif

{{-- Materials --}}
<div class="card overflow-hidden mb-6">
    <div class="p-4 border-b border-slate-700 flex items-center justify-between">
        <h3><i class="fas fa-boxes text-orange-400 mr-2"></i>Material Costs (Quote)</h3>
        <span class="text-sm font-semibold text-orange-400">${{ number_format($matCost, 2) }}</span>
    </div>
    @if($allItems->count())
    <div class="overflow-x-auto">
        <table class="w-full text-sm">
            <thead>
                <tr class="text-xs text-slate-400 border-b border-slate-700">
                    <th class="px-4 py-2 text-left">Description</th>
                    <th class="px-4 py-2 text-right">Qty</th>
                    <th class="px-4 py-2 text-right">W/Sale</th>
                    <th class="px-4 py-2 text-right">Retail</th>
                    <th class="px-4 py-2 text-right">Total W/Sale</th>
                    <th class="px-4 py-2 text-right">Total Retail</th>
                </tr>
            </thead>
            <tbody>
                @foreach($allItems as $item)
                <tr class="table-row border-b border-slate-800">
                    <td class="px-4 py-2 text-white">{{ $item->description }}
                        @if($item->item_code)<span class="text-xs text-slate-500 ml-1">[{{ $item->item_code }}]</span>@endif
                    </td>
                    <td class="px-4 py-2 text-right text-slate-400">{{ number_format($item->qty, 2) }}</td>
                    <td class="px-4 py-2 text-right text-slate-400">${{ number_format($item->wsale_price ?? 0, 2) }}</td>
                    <td class="px-4 py-2 text-right text-slate-300">${{ number_format($item->retail_price ?? $item->unit_price ?? 0, 2) }}</td>
                    <td class="px-4 py-2 text-right text-orange-400">${{ number_format($item->total_wsale ?? 0, 2) }}</td>
                    <td class="px-4 py-2 text-right text-green-400">${{ number_format($item->total_retail ?? $item->total ?? 0, 2) }}</td>
                </tr>
                @endforeach
            </tbody>
            <tfoot>
                <tr class="font-semibold border-t-2 border-slate-600">
                    <td colspan="4" class="px-4 py-2 text-right text-slate-300">Totals</td>
                    <td class="px-4 py-2 text-right text-orange-400">${{ number_format($matCost, 2) }}</td>
                    <td class="px-4 py-2 text-right text-green-400">${{ number_format($revenue, 2) }}</td>
                </tr>
            </tfoot>
        </table>
    </div>
    @else
    <div class="p-6 text-center text-slate-500">No quote items found.</div>
    @endif
</div>

{{-- Labour --}}
<div class="card overflow-hidden mb-6">
    <div class="p-4 border-b border-slate-700 flex items-center justify-between">
        <h3><i class="fas fa-user-clock text-blue-400 mr-2"></i>Labour Costs</h3>
        <span class="text-sm font-semibold text-blue-400">${{ number_format($labourCost, 2) }}</span>
    </div>
    @if($labourLines->count())
    <table class="w-full text-sm">
        <thead>
            <tr class="text-xs text-slate-400 border-b border-slate-700">
                <th class="px-4 py-2 text-left">Staff</th>
                <th class="px-4 py-2 text-left">Date</th>
                <th class="px-4 py-2 text-right">Hours</th>
                <th class="px-4 py-2 text-right">Rate</th>
                <th class="px-4 py-2 text-right">Cost</th>
            </tr>
        </thead>
        <tbody>
            @foreach($labourLines as $line)
            <tr class="table-row border-b border-slate-800">
                <td class="px-4 py-2 text-white">{{ $line['staff'] }}</td>
                <td class="px-4 py-2 text-slate-400">{{ $line['date'] }}</td>
                <td class="px-4 py-2 text-right text-slate-300">{{ number_format($line['hours'], 2) }}h</td>
                <td class="px-4 py-2 text-right text-slate-400">${{ number_format($line['rate'], 2) }}/hr</td>
                <td class="px-4 py-2 text-right text-blue-400 font-medium">${{ number_format($line['cost'], 2) }}</td>
            </tr>
            @endforeach
        </tbody>
        <tfoot>
            <tr class="font-semibold border-t-2 border-slate-600">
                <td colspan="4" class="px-4 py-2 text-right text-slate-300">Total Labour</td>
                <td class="px-4 py-2 text-right text-blue-400">${{ number_format($labourCost, 2) }}</td>
            </tr>
        </tfoot>
    </table>
    @else
    <div class="p-6 text-center text-slate-500">No time entries recorded for this job.</div>
    @endif
</div>

{{-- PO Allocations --}}
@if($poLines->count())
<div class="card overflow-hidden mb-6">
    <div class="p-4 border-b border-slate-700 flex items-center justify-between">
        <h3><i class="fas fa-file-invoice text-purple-400 mr-2"></i>Inventory & Purchase Order Costs</h3>
        <span class="text-sm font-semibold text-purple-400">${{ number_format($poCost, 2) }}</span>
    </div>
    <table class="w-full text-sm">
        <thead>
            <tr class="text-xs text-slate-400 border-b border-slate-700">
                <th class="px-4 py-2 text-left">Description</th>
                <th class="px-4 py-2 text-left">Source</th>
                <th class="px-4 py-2 text-right">Qty</th>
                <th class="px-4 py-2 text-right">Unit Cost</th>
                <th class="px-4 py-2 text-right">Total</th>
            </tr>
        </thead>
        <tbody>
            @foreach($poLines as $line)
            <tr class="table-row border-b border-slate-800">
                <td class="px-4 py-2 text-white">{{ $line['description'] }}</td>
                <td class="px-4 py-2 font-mono text-blue-300 text-xs">{{ $line['source'] ?? '—' }}</td>
                <td class="px-4 py-2 text-right text-slate-400">{{ number_format($line['qty'], 2) }}</td>
                <td class="px-4 py-2 text-right text-slate-400">${{ number_format($line['unit_cost'], 2) }}</td>
                <td class="px-4 py-2 text-right text-purple-400 font-medium">${{ number_format($line['total'], 2) }}</td>
            </tr>
            @endforeach
        </tbody>
        <tfoot>
            <tr class="font-semibold border-t-2 border-slate-600">
                <td colspan="4" class="px-4 py-2 text-right text-slate-300">Total PO Costs</td>
                <td class="px-4 py-2 text-right text-purple-400">${{ number_format($poCost, 2) }}</td>
            </tr>
        </tfoot>
    </table>
</div>
@endif

{{-- Summary footer --}}
<div class="card p-5">
    <h3 class="mb-4">Summary</h3>
    <div class="max-w-xs ml-auto space-y-2">
        <div class="flex justify-between text-sm"><span class="text-slate-400">Revenue (ex. GST)</span><span class="text-white">${{ number_format($revenue, 2) }}</span></div>
        <div class="flex justify-between text-sm"><span class="text-slate-400">Material Cost</span><span class="text-orange-400">-${{ number_format($matCost, 2) }}</span></div>
        <div class="flex justify-between text-sm"><span class="text-slate-400">Labour Cost</span><span class="text-blue-400">-${{ number_format($labourCost, 2) }}</span></div>
        @if($poCost > 0)
        <div class="flex justify-between text-sm"><span class="text-slate-400">Inventory & PO Costs</span><span class="text-purple-400">-${{ number_format($poCost, 2) }}</span></div>
        @endif
        @if($invoiceDeductions > 0)
        <div class="flex justify-between text-sm"><span class="text-slate-400">Invoice Deductions</span><span class="text-amber-400">-${{ number_format($invoiceDeductions, 2) }}</span></div>
        @endif
        @if($faultDeductions > 0)
        <div class="flex justify-between text-sm"><span class="text-slate-400">WO Fault Costs</span><span class="text-orange-400">-${{ number_format($faultDeductions, 2) }}</span></div>
        @endif
        <div class="flex justify-between text-base font-bold border-t border-slate-700 pt-2">
            <span class="{{ $grossProfit >= 0 ? 'text-green-400' : 'text-red-400' }}">Gross Profit</span>
            <span class="{{ $grossProfit >= 0 ? 'text-green-400' : 'text-red-400' }}">${{ number_format($grossProfit, 2) }}</span>
        </div>
        <div class="flex justify-between text-sm">
            <span class="text-slate-400">Gross Margin</span>
            <span class="{{ $margin >= 30 ? 'text-green-400' : ($margin >= 15 ? 'text-yellow-400' : 'text-red-400') }}">{{ $margin }}%</span>
        </div>
    </div>
</div>
@endsection
