<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Case Status Report</title>
    <style>
        body { font-family: Arial, sans-serif; font-size: 11px; margin: 16px; color: #111; }
        h3, h4 { margin: 0; }
        .line { border-top: 1px solid #888; margin: 7px 0; }
        .section { margin-top: 12px; }
        table { width: 100%; border-collapse: collapse; margin-top: 6px; }
        th, td { border: 1px solid #ddd; padding: 5px; vertical-align: top; }
        th { background: #f2f4f7; text-align: left; }
        .text-right { text-align: right; }
        .text-center { text-align: center; }
        ul { margin-top: 4px; margin-bottom: 8px; }
    </style>
</head>
<body>
    @include('modules.reports.partials.pdf-company-details')
    <div class="text-center">
        <h3>MATTER CASE STATUS REPORT</h3>
    </div>
    <div class="line"></div>
    <div><strong>As Of:</strong> {{ $reportData['as_of'] }}</div>
    <div><strong>Matter:</strong> {{ $reportData['matter_label'] ?? 'All Matters' }}</div>
    <div><strong>Total Active Cases:</strong> {{ $reportData['total_active_cases'] }}</div>

    <div class="section">
        <strong>SUMMARY</strong>
        <table>
            <thead>
                <tr><th>Status</th><th class="text-right">Count</th></tr>
            </thead>
            <tbody>
                @forelse($reportData['summary_by_status'] as $status => $count)
                    <tr><td>{{ $status }}</td><td class="text-right">{{ $count }}</td></tr>
                @empty
                    <tr><td colspan="2" class="text-center">No status data available.</td></tr>
                @endforelse
            </tbody>
        </table>
    </div>

    <div class="section">
        <strong>ACTIVE CASES</strong>
        <table>
            <thead>
                <tr>
                    <th>Case #</th>
                    <th>Client</th>
                    <th>Type</th>
                    <th>Status</th>
                    <th>Court Appearance Date</th>
                    <th>Attorney</th>
                </tr>
            </thead>
            <tbody>
                @forelse($reportData['active_cases'] as $row)
                    <tr>
                        <td>{{ $row['case_number'] }}</td>
                        <td>{{ $row['client'] }}</td>
                        <td>{{ $row['type'] }}</td>
                        <td>{{ $row['status'] }}</td>
                        <td>{{ $row['court_appearance_date'] ? $row['court_appearance_date']->format('M j, Y') : 'No appearance date' }}</td>
                        <td>{{ $row['attorney'] }}</td>
                    </tr>
                @empty
                    <tr><td colspan="6" class="text-center">No active cases found.</td></tr>
                @endforelse
            </tbody>
        </table>
    </div>

    <div class="section">
        <strong>UPCOMING COURT APPEARANCES (NEXT 30 DAYS)</strong>
        <table>
            <thead>
                <tr>
                    <th>Date</th>
                    <th>Case #</th>
                    <th>Client</th>
                    <th>Court Appearance</th>
                    <th>Attorney</th>
                </tr>
            </thead>
            <tbody>
                @forelse($reportData['upcoming_court_appearances'] as $row)
                    <tr>
                        <td>{{ $row['court_appearance_date'] ? $row['court_appearance_date']->format('M j') : 'N/A' }}</td>
                        <td>{{ $row['case_number'] }}</td>
                        <td>{{ $row['client'] }}</td>
                        <td>{{ $row['notes'] ?: 'Court appearance' }}</td>
                        <td>{{ $row['attorney'] }}</td>
                    </tr>
                @empty
                    <tr><td colspan="5" class="text-center">No upcoming court appearances.</td></tr>
                @endforelse
            </tbody>
        </table>
    </div>

    <div class="section">
        <strong>CASES BY ATTORNEY</strong>
        @forelse($reportData['cases_by_attorney'] as $group)
            <div style="margin-top:8px;"><strong>{{ $group['attorney'] }} ({{ $group['count'] }} cases)</strong></div>
            <ul>
                @foreach($group['cases'] as $caseRow)
                    <li>
                        {{ $caseRow['case_number'] }} {{ $caseRow['client'] }} ({{ $caseRow['status'] }} -
                        {{ $caseRow['court_appearance_date'] ? $caseRow['court_appearance_date']->format('M j') : 'none' }})
                    </li>
                @endforeach
            </ul>
        @empty
            <div>No attorney grouping data available.</div>
        @endforelse
    </div>

    <div class="section">
        <strong>CASES WITH NO ACTIVITY (&gt;14 DAYS)</strong>
        <table>
            <thead>
                <tr>
                    <th class="text-right">Days</th>
                    <th>Case #</th>
                    <th>Client</th>
                    <th>Last Action</th>
                    <th>Attorney</th>
                </tr>
            </thead>
            <tbody>
                @forelse($reportData['no_activity_cases'] as $row)
                    <tr>
                        <td class="text-right">{{ $row['days_no_activity'] ?? 0 }}</td>
                        <td>{{ $row['case_number'] }}</td>
                        <td>{{ $row['client'] }}</td>
                        <td>{{ $row['last_action'] }}</td>
                        <td>{{ $row['attorney'] }}</td>
                    </tr>
                @empty
                    <tr><td colspan="5" class="text-center">No cases with inactivity above 14 days.</td></tr>
                @endforelse
            </tbody>
        </table>
    </div>
</body>
</html>
