@extends('layouts.app')
@section('title','Credit score history')
@section('content')
@php
    $risk = $customer->riskProfile;
    $bandLabel = data_get($risk?->factors, 'band_label') ?: str($risk?->score_band ?? 'unrated')->replace('_',' ')->title();
@endphp

<x-ui.page-toolbar
    title="Credit score history"
    :description="$customer->full_name.' · '.$customer->customer_number.' · '.$customer->branch->name"
    :breadcrumbs="[['label'=>'Customers','url'=>route('customers.index')],['label'=>$customer->full_name,'url'=>route('customers.show',$customer)],['label'=>'Credit score history']]"
>
    <x-slot:actions>
        <a class="mf-btn" href="{{ route('customers.show',$customer) }}">Back to customer</a>
        @if(auth()->user()->can('credit-score.recalculate'))
            <form method="POST" action="{{ route('customers.credit-score.recalculate',$customer) }}" class="inline">@csrf
                <button class="mf-btn mf-btn-primary">Recalculate</button>
            </form>
        @endif
    </x-slot:actions>
</x-ui.page-toolbar>

<div class="mf-stat-grid mb-4">
    <div class="mf-card mf-card-body"><div class="text-xs text-muted-foreground">Current score</div><div class="mt-1 text-xl font-semibold">{{ $risk?->score !== null ? (int) round((float)$risk->score) : '—' }}</div></div>
    <div class="mf-card mf-card-body"><div class="text-xs text-muted-foreground">Band</div><div class="mt-1 font-medium">{{ $bandLabel }}</div></div>
    <div class="mf-card mf-card-body"><div class="text-xs text-muted-foreground">Confidence</div><div class="mt-1 font-medium">{{ $risk?->confidence !== null ? number_format((float)$risk->confidence,0).'%' : '—' }}</div></div>
    <div class="mf-card mf-card-body"><div class="text-xs text-muted-foreground">Last calculated</div><div class="mt-1 font-medium">{{ $risk?->calculated_at?->format('d M Y H:i') ?? '—' }}</div></div>
</div>

<div class="mf-card">
    <div class="border-b p-4">
        <h2 class="font-semibold">Score snapshots</h2>
        <p class="mt-1 text-xs text-muted-foreground">Immutable history retained for audit, trend review and score-version comparison.</p>
    </div>
    <div class="mf-table-frame border-0">
        <table class="mf-table">
            <thead><tr><th>Calculated</th><th>Score</th><th>Band / risk</th><th>Confidence</th><th>Flags</th><th>Trigger</th><th>Explanation</th></tr></thead>
            <tbody>
            @forelse($histories as $item)
                <tr>
                    <td class="whitespace-nowrap">{{ $item->calculated_at?->format('d M Y H:i') ?? $item->created_at->format('d M Y H:i') }}<div class="text-xs text-muted-foreground">{{ $item->creator?->name ?? 'System' }}</div></td>
                    <td><span class="text-base font-semibold">{{ (int) round((float)$item->score) }}</span><div class="text-xs text-muted-foreground">{{ strtoupper($item->score_version) }}</div></td>
                    <td>{{ str($item->score_band)->replace('_',' ')->title() }}<div class="text-xs text-muted-foreground">{{ str($item->risk_level)->replace('_',' ')->title() }} risk</div></td>
                    <td>{{ number_format((float)$item->confidence,0) }}%</td>
                    <td>
                        <div class="flex flex-wrap gap-1">
                            @if($item->thin_file)<span class="mf-badge mf-badge-muted">Thin file</span>@endif
                            @if($item->manual_review_required)<span class="mf-badge">Manual review</span>@endif
                            @if(!$item->thin_file && !$item->manual_review_required)<span class="text-xs text-muted-foreground">—</span>@endif
                        </div>
                    </td>
                    <td>{{ str($item->trigger)->replace('_',' ')->title() }}</td>
                    <td class="max-w-sm text-sm text-muted-foreground">{{ $item->explanation ?: '—' }}</td>
                </tr>
            @empty
                <tr><td colspan="7" class="text-center text-muted-foreground">No immutable score history has been recorded yet.</td></tr>
            @endforelse
            </tbody>
        </table>
    </div>
    @if($histories->hasPages())<div class="border-t p-4">{{ $histories->links() }}</div>@endif
</div>
@endsection
