<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>Client Statement of Account — Ledger</title>
    <style>
        body {
            font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif;
            color: #334155;
            font-size: 12px;
            line-height: 1.5;
        }
        .header {
            margin-bottom: 20px;
            border-bottom: 2px solid #f97316;
            padding-bottom: 15px;
        }
        .logo-text {
            font-size: 22px;
            font-weight: 800;
            color: #f97316;
            letter-spacing: -0.5px;
        }
        .title {
            font-size: 16px;
            font-weight: bold;
            text-transform: uppercase;
            text-align: right;
            color: #475569;
        }
        .meta-table {
            width: 100%;
            margin-bottom: 20px;
            border-collapse: collapse;
        }
        .meta-table td {
            vertical-align: top;
            width: 50%;
            padding: 0;
        }
        .summary-box {
            background-color: #fffbeb;
            border: 1px solid #fde68a;
            border-radius: 8px;
            padding: 12px 16px;
            margin-bottom: 25px;
        }
        .summary-title {
            font-weight: bold;
            color: #78350f;
            margin-bottom: 8px;
            font-size: 13px;
            text-transform: uppercase;
            letter-spacing: 0.5px;
        }
        .summary-grid {
            width: 100%;
            border-collapse: collapse;
        }
        .summary-grid td {
            padding: 4px 0;
            font-size: 12px;
        }
        .summary-value {
            font-weight: bold;
            text-align: right;
            font-family: monospace;
            font-size: 13px;
        }
        .section-title {
            font-size: 13px;
            font-weight: bold;
            color: #f97316;
            margin-top: 20px;
            margin-bottom: 10px;
            border-left: 3px solid #f97316;
            padding-left: 8px;
            text-transform: uppercase;
        }
        .ledger-table {
            width: 100%;
            border-collapse: collapse;
            margin-bottom: 20px;
        }
        .ledger-table th {
            background-color: #f8fafc;
            color: #475569;
            font-weight: bold;
            text-align: left;
            padding: 6px 8px;
            border-bottom: 1px solid #cbd5e1;
            border-top: 1px solid #cbd5e1;
            font-size: 10px;
            text-transform: uppercase;
        }
        .ledger-table td {
            padding: 6px 8px;
            border-bottom: 1px solid #f1f5f9;
            font-size: 11px;
            color: #334155;
            vertical-align: middle;
        }
        .amount-col {
            text-align: right;
            font-family: monospace;
        }
        .footer {
            margin-top: 40px;
            font-size: 9px;
            color: #94a3b8;
            text-align: center;
            border-top: 1px solid #f1f5f9;
            padding-top: 8px;
        }
    </style>
</head>
<body>

    <!-- Header -->
    <table class="meta-table">
        <tr>
            <td>
                <span class="logo-text">{{ $businessName }}</span><br>
                <span style="color: #64748b; font-size: 10px;">Corporate Account Statement</span>
            </td>
            <td style="text-align: right;">
                <span class="title">Statement of Account</span><br>
                <span style="color: #64748b; font-size: 10px;">Generated: {{ date('d-m-Y') }}</span>
            </td>
        </tr>
    </table>

    <div class="header"></div>

    <!-- Client Info -->
    <table class="meta-table" style="margin-bottom: 15px;">
        <tr>
            <td>
                <strong style="color: #1e293b; font-size: 13px;">Client Profile</strong><br>
                {{ $client->full_name }}<br>
                Email: {{ $client->email ?? 'N/A' }}<br>
                Phone: {{ $client->phone ?? 'N/A' }}
            </td>
            <td style="text-align: right;">
                @if($client->company_name)
                    <strong style="color: #1e293b; font-size: 13px;">Billing Entity</strong><br>
                    {{ $client->company_name }}<br>
                    VAT No: {{ $client->company_vat ?? 'N/A' }}<br>
                @endif
                {{ $client->address_line1 }} {{ $client->address_line2 }}<br>
                {{ $client->city }}, {{ $client->state }} {{ $client->postcode }}
            </td>
        </tr>
    </table>

    <!-- Financial Summary Box -->
    <div class="summary-box">
        <div class="summary-title">Ledger Financial Summary</div>
        <table class="summary-grid">
            <tr>
                <td>Opening Balance Owed</td>
                <td class="summary-value" style="color: #475569;">{{ $currencySymbol }}{{ number_format($openingBalance, 2) }}</td>
            </tr>
            <tr>
                <td>Total Machinery Deals / Contracts (+)</td>
                <td class="summary-value" style="color: #b91c1c;">{{ $currencySymbol }}{{ number_format($totalDeals, 2) }}</td>
            </tr>
            <tr>
                <td>Total Project Cost Shares (+)</td>
                <td class="summary-value" style="color: #b91c1c;">{{ $currencySymbol }}{{ number_format($totalProjectCostShares, 2) }}</td>
            </tr>
            <tr>
                <td>Total Payments Received (-)</td>
                <td class="summary-value" style="color: #15803d;">{{ $currencySymbol }}{{ number_format($totalCashIn, 2) }}</td>
            </tr>
            <tr>
                <td>Total Refunds / Cash Outs (+)</td>
                <td class="summary-value" style="color: #b91c1c;">{{ $currencySymbol }}{{ number_format($totalCashOut, 2) }}</td>
            </tr>
            <tr style="border-top: 1.5px solid #fde68a;">
                <td style="font-weight: bold; padding-top: 6px; color: #78350f;">Receivable Balance (Owed to Us)</td>
                <td class="summary-value" style="font-weight: bold; color: #ea580c; padding-top: 6px; font-size: 14px;">
                    {{ $currencySymbol }}{{ number_format($balance, 2) }}
                </td>
            </tr>
        </table>
    </div>

    <!-- Running Ledger Section -->
    <div class="section-title">Account Activity Running Ledger</div>
    <table class="ledger-table">
        <thead>
            <tr>
                <th style="width: 12%;">Date</th>
                <th style="width: 38%;">Description / Details</th>
                <th style="width: 15%;">Project</th>
                <th style="width: 11%; text-align: right;">Debit (+)</th>
                <th style="width: 11%; text-align: right;">Credit (-)</th>
                <th style="width: 13%; text-align: right;">Balance</th>
            </tr>
        </thead>
        <tbody>
            <!-- Opening Balance Row -->
            <tr>
                <td>{{ $client->created_at ? $client->created_at->format('d-m-Y') : date('d-m-Y') }}</td>
                <td><strong>Opening Balance</strong></td>
                <td>-</td>
                <td class="amount-col" style="color: #b91c1c;">{{ $openingBalance >= 0 ? $currencySymbol . number_format($openingBalance, 2) : '-' }}</td>
                <td class="amount-col" style="color: #15803d;">{{ $openingBalance < 0 ? $currencySymbol . number_format(abs($openingBalance), 2) : '-' }}</td>
                <td class="amount-col" style="font-weight: bold;">{{ $currencySymbol }}{{ number_format($openingBalance, 2) }}</td>
            </tr>

            @php
                $running = $openingBalance;
                $txns = collect();
                
                foreach($client->deals as $deal) {
                    $txns->push([
                        'date' => $deal->contract_date ?? $deal->created_at,
                        'desc' => 'Machinery Deal: ' . $deal->deal_name . ($deal->machinery_details ? ' (' . $deal->machinery_details . ')' : ''),
                        'project' => $deal->consignment ? $deal->consignment->title : '-',
                        'debit' => $deal->deal_value,
                        'credit' => 0,
                    ]);
                }

                foreach($client->consignments as $consignment) {
                    if ($consignment->investor_share_type === 'fixed') {
                        $costShare = $consignment->investor_share_value;
                        $shareLabel = 'Fixed Share';
                    } else {
                        $projectOutflows = $consignment->cashOuts->where('payment_status', 'paid')->sum('amount') 
                                           + $consignment->expenses->sum('amount');
                        $costShare = $projectOutflows * ($consignment->investor_share_value / 100);
                        $shareLabel = number_format($consignment->investor_share_value, 1) . '% Cost Share';
                    }
                    $txns->push([
                        'date' => $consignment->created_at,
                        'desc' => 'Consignment Project Share: ' . $consignment->title . ' (' . $shareLabel . ')',
                        'project' => $consignment->title,
                        'debit' => $costShare,
                        'credit' => 0,
                    ]);
                }
                
                foreach($client->cashIns->where('payment_status', 'paid') as $in) {
                    $desc = ($in->paymentType ? $in->paymentType->name : 'Payment') . ' Received';
                    if ($in->third_party_payer) {
                        $desc .= ' (via ' . $in->third_party_payer . ')';
                    }
                    if ($in->sender_bank_name) {
                        $desc .= ' - ' . $in->sender_bank_name;
                    }
                    $txns->push([
                        'date' => $in->date,
                        'desc' => $desc,
                        'project' => $in->consignment ? $in->consignment->title : '-',
                        'debit' => 0,
                        'credit' => $in->amount,
                    ]);
                }
                
                foreach($client->cashOuts->where('payment_status', 'paid') as $out) {
                    $desc = ($out->paymentType ? $out->paymentType->name : 'Payout');
                    if ($out->guest_bank_name) {
                        $desc .= ' - ' . $out->guest_bank_name;
                    }
                    $txns->push([
                        'date' => $out->date,
                        'desc' => $desc,
                        'project' => $out->consignment ? $out->consignment->title : '-',
                        'debit' => $out->amount,
                        'credit' => 0,
                    ]);
                }
                
                $txns = $txns->sortBy('date');
            @endphp
            
            @foreach($txns as $t)
                @php
                    $running += ($t['debit'] - $t['credit']);
                @endphp
                <tr>
                    <td>{{ $t['date']->format('d-m-Y') }}</td>
                    <td>{{ $t['desc'] }}</td>
                    <td>{{ $t['project'] }}</td>
                    <td class="amount-col" style="color: #b91c1c;">{{ $t['debit'] > 0 ? $currencySymbol . number_format($t['debit'], 2) : '-' }}</td>
                    <td class="amount-col" style="color: #15803d;">{{ $t['credit'] > 0 ? $currencySymbol . number_format($t['credit'], 2) : '-' }}</td>
                    <td class="amount-col" style="font-weight: bold;">{{ $currencySymbol }}{{ number_format($running, 2) }}</td>
                </tr>
            @endforeach
        </tbody>
    </table>

    <div class="footer">
        Statement of account issued on behalf of {{ $businessName }}. Confirmed ledger activity.
    </div>

</body>
</html>
