@extends('layouts.app')
@section('title','Credit Score Portfolio')
@section('content')
<x-ui.page-toolbar title="Credit Score Portfolio" description="Current score coverage, risk distribution and active portfolio exposure by credit-score band." :breadcrumbs="[['label'=>'Dashboard','url'=>route('dashboard')],['label'=>'Loan & Branch Reports','url'=>route('loan-branch-reports.index')],['label'=>'Credit Score Portfolio']]">
    <x-slot:actions>
        <a class="mf-btn" href="{{ route('credit-score-reports.export', request()->query()) }}">Export CSV</a>
        <button type="button" class="mf-btn" onclick="window.print()">Print</button>
    </x-slot:actions>
</x-ui.page-toolbar>

<div class="mb-3 flex flex-wrap gap-2 print:hidden">
    <a class="mf-btn" href="{{ route('loan-branch-reports.index') }}">Loan & Branch Reports</a>
    <a class="mf-btn mf-btn-primary" href="{{ route('credit-score-reports.portfolio') }}">Credit Score Portfolio</a>
</div>

<form method="GET" action="{{ route('credit-score-reports.portfolio') }}" class="mf-table-toolbar mb-4 print:hidden" data-ui="table-toolbar">
    <div class="mf-table-toolbar-left">
        <div class="mf-table-quick-filters">
            <details class="mf-columns-menu">
                <summary class="mf-filter-control cursor-pointer min-w-[200px]" aria-label="Select branches">
                    {{ $selectedBranches->count() === 1 ? $selectedBranches->first()->name : $selectedBranches->count().' branches' }}
                </summary>
                <div class="mf-columns-popover">
                    <div class="mf-columns-title">Branches</div>
                    @foreach($branches as $branch)
                        <label>
                            <input type="checkbox" name="branch_ids[]" value="{{ $branch->id }}" @checked($selectedBranchIds->contains($branch->id))>
                            <span>{{ $branch->name }}</span>
                        </label>
                    @endforeach
                    <div class="px-3 pb-2 text-[11px] text-muted-foreground">Clear all selections to include every accessible branch.</div>
                </div>
            </details>
        </div>
        <button class="mf-btn mf-btn-sm" type="submit">Run Report</button>
    </div>
</form>

<div class="mb-4 flex flex-wrap items-start justify-between gap-2">
    <div>
        <div class="text-lg font-semibold">Current portfolio snapshot</div>
        <div class="text-xs text-muted-foreground">
            {{ $selectedBranches->pluck('name')->join(', ') }} · Score version {{ $data['score_version'] }} · Generated {{ $data['generated_at']->format('d M Y H:i') }}
        </div>
        <div class="mt-1 max-w-5xl text-xs text-muted-foreground">Arrears rate is current overdue divided by current outstanding. “Needs refresh” includes missing, stale or score-version-mismatched current profiles. Historical application score snapshots are not changed by this report.</div>
    </div>
</div>

<div class="mb-4 grid gap-3 sm:grid-cols-2 lg:grid-cols-3 xl:grid-cols-6">
    <div class="mf-card mf-card-body">
        <div class="text-xs text-muted-foreground">Score coverage</div>
        <div class="mt-1 text-lg font-semibold">{{ number_format($data['summary']['coverage_rate'],2) }}%</div>
        <div class="text-[11px] text-muted-foreground">{{ number_format($data['summary']['scored_customers']) }} / {{ number_format($data['summary']['total_customers']) }} customers</div>
    </div>
    <div class="mf-card mf-card-body">
        <div class="text-xs text-muted-foreground">Average score</div>
        <div class="mt-1 text-lg font-semibold">{{ $data['summary']['average_score'] === null ? '—' : number_format($data['summary']['average_score'],1) }}</div>
        <div class="text-[11px] text-muted-foreground">Current scored customers</div>
    </div>
    <div class="mf-card mf-card-body">
        <div class="text-xs text-muted-foreground">Needs refresh</div>
        <div class="mt-1 text-lg font-semibold">{{ number_format($data['summary']['needs_refresh']) }}</div>
        <div class="text-[11px] text-muted-foreground">Missing, stale or old version</div>
    </div>
    <div class="mf-card mf-card-body">
        <div class="text-xs text-muted-foreground">Manual review</div>
        <div class="mt-1 text-lg font-semibold">{{ number_format($data['summary']['manual_review']) }}</div>
        <div class="text-[11px] text-muted-foreground">Advisory review flags</div>
    </div>
    <div class="mf-card mf-card-body">
        <div class="text-xs text-muted-foreground">Outstanding</div>
        <div class="mt-1 text-lg font-semibold">{{ number_format($data['summary']['total_outstanding'],2) }}</div>
        <div class="text-[11px] text-muted-foreground">{{ number_format($data['summary']['active_loans']) }} active loans</div>
    </div>
    <div class="mf-card mf-card-body">
        <div class="text-xs text-muted-foreground">Portfolio arrears</div>
        <div class="mt-1 text-lg font-semibold">{{ number_format($data['summary']['arrears_rate'],2) }}%</div>
        <div class="text-[11px] text-muted-foreground">{{ number_format($data['summary']['total_overdue'],2) }} overdue</div>
    </div>
</div>

<div class="mb-5">
    <div class="mb-2">
        <div class="text-sm font-semibold">Distribution by score band</div>
        <div class="text-xs text-muted-foreground">Customer coverage and active exposure grouped by each customer’s latest current score.</div>
    </div>
    <div class="mf-card">
        <div class="mf-table-frame border-0">
            <table class="mf-table">
                <thead>
                    <tr>
                        <th>Band</th><th class="text-right">Customers</th><th class="text-right">Avg score</th><th class="text-right">Confidence</th>
                        <th class="text-right">Manual review</th><th class="text-right">Thin file</th><th class="text-right">Active loans</th>
                        <th class="text-right">Outstanding</th><th class="text-right">Overdue</th><th class="text-right">Arrears rate</th>
                    </tr>
                </thead>
                <tbody>
                    @foreach($data['bands'] as $row)
                        <tr>
                            <td class="font-medium">{{ $row['label'] }}</td>
                            <td class="text-right">{{ number_format($row['customers']) }}</td>
                            <td class="text-right">{{ $row['average_score'] === null ? '—' : number_format($row['average_score'],1) }}</td>
                            <td class="text-right">{{ $row['average_confidence'] === null ? '—' : number_format($row['average_confidence'],1).'%' }}</td>
                            <td class="text-right">{{ number_format($row['manual_review']) }}</td>
                            <td class="text-right">{{ number_format($row['thin_file']) }}</td>
                            <td class="text-right">{{ number_format($row['active_loans']) }}</td>
                            <td class="text-right">{{ number_format($row['outstanding'],2) }}</td>
                            <td class="text-right">{{ number_format($row['overdue'],2) }}</td>
                            <td class="text-right">{{ number_format($row['arrears_rate'],2) }}%</td>
                        </tr>
                    @endforeach
                </tbody>
            </table>
        </div>
    </div>
</div>

<div>
    <div class="mb-2">
        <div class="text-sm font-semibold">Branch score and exposure summary</div>
        <div class="text-xs text-muted-foreground">Only branches available to the signed-in user are included.</div>
    </div>
    <div class="mf-card">
        <div class="mf-table-frame border-0">
            <table class="mf-table">
                <thead>
                    <tr>
                        <th>Branch</th><th class="text-right">Customers</th><th class="text-right">Scored</th><th class="text-right">Avg score</th>
                        <th class="text-right">Manual review</th><th class="text-right">Active loans</th><th class="text-right">Outstanding</th>
                        <th class="text-right">Overdue</th><th class="text-right">Arrears rate</th>
                    </tr>
                </thead>
                <tbody>
                    @forelse($data['branches'] as $row)
                        <tr>
                            <td class="font-medium">{{ $row['branch_name'] }}</td>
                            <td class="text-right">{{ number_format($row['customers']) }}</td>
                            <td class="text-right">{{ number_format($row['scored_customers']) }}</td>
                            <td class="text-right">{{ $row['average_score'] === null ? '—' : number_format($row['average_score'],1) }}</td>
                            <td class="text-right">{{ number_format($row['manual_review']) }}</td>
                            <td class="text-right">{{ number_format($row['active_loans']) }}</td>
                            <td class="text-right">{{ number_format($row['outstanding'],2) }}</td>
                            <td class="text-right">{{ number_format($row['overdue'],2) }}</td>
                            <td class="text-right">{{ number_format($row['arrears_rate'],2) }}%</td>
                        </tr>
                    @empty
                        <tr><td colspan="9" class="text-center text-muted-foreground">No branch data is available for the selected scope.</td></tr>
                    @endforelse
                </tbody>
            </table>
        </div>
    </div>
</div>
@endsection

@push('styles')
<style>@media print{.mf-sidebar,.mf-topbar,.print\:hidden{display:none!important}.mf-content{margin:0!important}.mf-page{padding:0!important}.mf-card{box-shadow:none!important}.mf-table{font-size:10px!important}}</style>
@endpush
