@extends('layouts.app')
@section('title', 'Daily Report '.$report->closing_number)

@section('content')
<x-ui.page-toolbar
    :title="$report->closing_number"
    :description="'Archived daily branch report for '.$report->branch->name.' · '.$report->report_date->format('d M Y')"
    :breadcrumbs="[['label'=>'Dashboard','url'=>route('dashboard')],['label'=>'Daily Branch Reports','url'=>route('admin.daily-reports.index')],['label'=>$report->closing_number]]"
>
    <x-slot:actions>
        <a class="mf-btn" href="{{ route('admin.daily-reports.index') }}">Back</a>
        <form method="POST" action="{{ route('admin.daily-reports.send',$report) }}">
            @csrf
            <button class="mf-btn mf-btn-primary" type="submit">Send / retry enabled channels</button>
        </form>
    </x-slot:actions>
</x-ui.page-toolbar>

@php($snapshot=$report->snapshot)
<div class="space-y-6">
    <div class="grid gap-3 md:grid-cols-2 xl:grid-cols-4">
        <div class="mf-card mf-card-body"><div class="text-xs text-muted-foreground">Branch</div><div class="mt-1 font-semibold">{{ $snapshot['branch_name'] }}</div></div>
        <div class="mf-card mf-card-body"><div class="text-xs text-muted-foreground">Closing funds</div><div class="mt-1 text-xl font-semibold">{{ number_format((float)$snapshot['closing_funds'],2) }}</div></div>
        <div class="mf-card mf-card-body"><div class="text-xs text-muted-foreground">Actual reconciled balance</div><div class="mt-1 text-xl font-semibold">{{ number_format((float)$snapshot['actual_balance'],2) }}</div></div>
        <div class="mf-card mf-card-body"><div class="text-xs text-muted-foreground">Variance</div><div class="mt-1 text-xl font-semibold {{ (float)$snapshot['variance']!==0.0?'text-destructive':'' }}">{{ number_format((float)$snapshot['variance'],2) }}</div></div>
    </div>

    <div class="grid gap-6 xl:grid-cols-[minmax(0,1.15fr)_minmax(320px,.85fr)]">
        <section class="mf-card min-w-0">
            <div class="mf-card-body border-b">
                <h2 class="text-sm font-semibold">Stored report copy</h2>
                <p class="mt-1 text-xs text-muted-foreground">This content was frozen when the confirmed closing was archived. Checksum: <span class="font-mono">{{ $report->checksum }}</span></p>
            </div>
            <div class="mf-card-body">
                <div class="whitespace-pre-wrap text-sm leading-6">{{ $report->body_text }}</div>
            </div>
        </section>

        <section class="mf-card self-start">
            <div class="mf-card-body border-b">
                <h2 class="text-sm font-semibold">Audit metadata</h2>
                <p class="mt-1 text-xs text-muted-foreground">Generation and confirmation details.</p>
            </div>
            <div class="mf-card-body">
                <dl class="grid grid-cols-[130px_minmax(0,1fr)] gap-x-3 gap-y-2 text-sm">
                    <dt class="text-muted-foreground">Report date</dt><dd>{{ $report->report_date->format('d M Y') }}</dd>
                    <dt class="text-muted-foreground">Generated</dt><dd>{{ $report->generated_at->format('d M Y H:i:s') }}</dd>
                    <dt class="text-muted-foreground">Confirmed by</dt><dd>{{ $snapshot['confirmed_by'] ?: '—' }}</dd>
                    <dt class="text-muted-foreground">Confirmed at</dt><dd>{{ $snapshot['confirmed_at'] ?: '—' }}</dd>
                    <dt class="text-muted-foreground">Fingerprint</dt><dd class="break-all font-mono text-xs">{{ $snapshot['snapshot_fingerprint'] }}</dd>
                </dl>
                @if($snapshot['manager_comments'])
                    <div class="mt-4 border-t pt-3">
                        <div class="text-xs text-muted-foreground">Manager comments</div>
                        <div class="mt-1 text-sm">{{ $snapshot['manager_comments'] }}</div>
                    </div>
                @endif
            </div>
        </section>
    </div>

    <section class="mf-card">
        <div class="mf-card-body border-b">
            <h2 class="text-sm font-semibold">Delivery audit trail</h2>
            <p class="mt-1 text-xs text-muted-foreground">Every destination keeps its current status, attempt count, provider reference and failure details.</p>
        </div>
        <div class="mf-table-frame border-0">
            <table class="mf-table" data-standard-table>
                <thead>
                    <tr>
                        <th>Channel</th>
                        <th>Recipient</th>
                        <th>Destination</th>
                        <th>Status</th>
                        <th>Attempts</th>
                        <th>Last attempt</th>
                        <th>Provider ID</th>
                        <th>Result</th>
                        <th></th>
                    </tr>
                </thead>
                <tbody>
                @forelse($report->deliveries as $delivery)
                    <tr>
                        <td>{{ $delivery->channel === 'whatsapp' ? 'WhatsApp' : 'Email' }}</td>
                        <td>{{ $delivery->recipient_name ?: '—' }}</td>
                        <td>{{ $delivery->recipient }}</td>
                        <td><span class="mf-badge {{ $delivery->status==='sent'?'mf-badge-success':($delivery->status==='failed'?'mf-badge-danger':'mf-badge-warning') }}">{{ ucfirst($delivery->status) }}</span></td>
                        <td>{{ $delivery->attempts }}</td>
                        <td>{{ $delivery->last_attempt_at?->format('d M Y H:i:s') ?? '—' }}</td>
                        <td class="max-w-[180px] break-all text-xs">{{ $delivery->provider_message_id ?: '—' }}</td>
                        <td class="max-w-[320px] text-xs">
                            @if($delivery->error_message)
                                <span class="text-destructive">{{ $delivery->error_message }}</span>
                            @elseif($delivery->provider_response)
                                <span class="text-muted-foreground">{{ $delivery->provider_response }}</span>
                            @else
                                <span class="text-muted-foreground">—</span>
                            @endif
                        </td>
                        <td class="text-right">
                            @if($delivery->status!=='sent')
                                <form method="POST" action="{{ route('admin.daily-reports.deliveries.retry',$delivery) }}">
                                    @csrf
                                    <button class="mf-btn mf-btn-sm" type="submit">Retry</button>
                                </form>
                            @endif
                        </td>
                    </tr>
                @empty
                    <tr><td colspan="9" class="text-center text-muted-foreground">No delivery rows exist. Enable a channel and add recipients, then use Send / retry enabled channels.</td></tr>
                @endforelse
                </tbody>
            </table>
        </div>
    </section>
</div>
@endsection
