{{--
    Review-first AI data candidates for one product.
    Expects: $product (App\Product) + $candidates (Collection<PcDataCandidate>).
--}}
@php
    use Illuminate\Support\Str;

    $byField = $candidates->groupBy('field');
    $images       = $byField->get('image_url', collect());
    $descriptions = $byField->get('description', collect());
    $names        = $byField->get('name', collect());
    $brands       = $byField->get('brand', collect());
    $specsList    = $byField->get('specs', collect());
    $corrections  = $byField->get('correction', collect());

    $hasImage = ! empty($product->image);

    $badge = function ($c) {
        $conf = (int) $c->confidence;
        $cls = $conf >= 80 ? 'label-success' : ($conf >= 60 ? 'label-info' : 'label-warning');
        return '<span class="label label-default">' . e($c->source) . '</span> '
             . '<span class="label ' . $cls . '">' . $conf . '%</span>';
    };
@endphp

<hr>
<h4 style="margin-top:0;"><i class="fa fa-robot"></i> @lang('productcompat::lang.ai_suggestions')</h4>

{{-- ---------------------------------------------------------------- Images --}}
@if($images->count())
    <div style="margin-bottom:14px;">
        <strong>@lang('productcompat::lang.images')</strong>
        <div class="row" style="margin-top:6px;">
            @foreach($images as $c)
                <div class="col-sm-3 col-xs-6 pc-image-card" style="margin-bottom:10px;">
                    <img src="{{ $c->value }}" loading="lazy" referrerpolicy="no-referrer"
                         style="max-height:100px; max-width:100%; border:1px solid #ddd; padding:2px; display:block;"
                         alt="candidate image">
                    <div style="margin-top:4px;">{!! $badge($c) !!}</div>
                    @if($hasImage)
                        <label style="display:block; font-size:12px; margin:4px 0;">
                            <input type="checkbox" class="pc-image-replace"> @lang('productcompat::lang.replace_existing')
                        </label>
                    @endif
                    <button type="button" class="btn btn-xs btn-primary pc-attach-image" data-candidate="{{ $c->id }}">
                        <i class="fa fa-download"></i> @lang('productcompat::lang.download_attach')
                    </button>
                    <form method="POST" action="{{ route('productcompat.candidate.reject') }}" style="display:inline;">
                        @csrf <input type="hidden" name="id" value="{{ $c->id }}">
                        <button class="btn btn-xs btn-default"><i class="fa fa-times"></i> @lang('productcompat::lang.reject')</button>
                    </form>
                </div>
            @endforeach
        </div>
    </div>
@endif

{{-- ----------------------------------------------------------- Description --}}
@foreach($descriptions as $c)
    <div class="row" style="margin-bottom:14px;">
        <div class="col-md-12"><strong>@lang('productcompat::lang.description')</strong> {!! $badge($c) !!}</div>
        <div class="col-md-6">
            <small class="text-muted">@lang('productcompat::lang.current')</small>
            <div style="background:#f9f9f9; padding:6px; min-height:40px;">
                {{ Str::limit(trim(strip_tags((string) $product->product_description)), 300) ?: '—' }}
            </div>
        </div>
        <div class="col-md-6">
            <small class="text-muted">@lang('productcompat::lang.suggested')</small>
            <div style="background:#f0fff0; padding:6px; min-height:40px;">{{ $c->value }}</div>
            <div style="margin-top:4px;">
                @include('productcompat::review._candidate_actions', ['c' => $c])
            </div>
        </div>
    </div>
@endforeach

{{-- ------------------------------------------------------------ Name / Brand --}}
@foreach($names as $c)
    <div style="margin-bottom:10px;">
        <strong>@lang('productcompat::lang.name')</strong> {!! $badge($c) !!}<br>
        <span class="text-muted">{{ Str::limit($product->name, 80) ?: '—' }}</span>
        <i class="fa fa-arrow-right"></i>
        <strong>{{ Str::limit($c->value, 80) }}</strong>
        <div style="margin-top:4px;">@include('productcompat::review._candidate_actions', ['c' => $c])</div>
    </div>
@endforeach

@foreach($brands as $c)
    <div style="margin-bottom:10px;">
        <strong>@lang('productcompat::lang.brand')</strong> {!! $badge($c) !!}<br>
        <span class="text-muted">{{ optional($product->brand)->name ?: '—' }}</span>
        <i class="fa fa-arrow-right"></i>
        <strong>{{ Str::limit($c->value, 80) }}</strong>
        <div style="margin-top:4px;">@include('productcompat::review._candidate_actions', ['c' => $c])</div>
    </div>
@endforeach

{{-- ------------------------------------------------------------------- Specs --}}
@foreach($specsList as $c)
    @php $specRows = json_decode((string) $c->value, true); $specRows = is_array($specRows) ? $specRows : []; @endphp
    <div style="margin-bottom:14px;">
        <strong>@lang('productcompat::lang.specifications')</strong> {!! $badge($c) !!}
        @if(count($specRows))
            <table class="table table-condensed table-bordered" style="margin-top:6px; max-width:520px;">
                @foreach($specRows as $row)
                    <tr>
                        <th style="width:40%;">{{ (string) ($row['label'] ?? '') }}</th>
                        <td>{{ (string) ($row['value'] ?? '') }}</td>
                    </tr>
                @endforeach
            </table>
        @endif
        <div>@include('productcompat::review._candidate_actions', ['c' => $c])</div>
    </div>
@endforeach

{{-- ------------------------------------------------------------- Corrections --}}
@if($corrections->count())
    <div style="margin-bottom:8px;"><strong>@lang('productcompat::lang.corrections')</strong></div>
    @foreach($corrections as $c)
        @php $x = (array) $c->extra; @endphp
        <div class="alert alert-warning" style="padding:8px 12px;">
            <i class="fa fa-exclamation-triangle"></i>
            {{ __('productcompat::lang.correction_flag', [
                'current' => (string) ($x['current_value'] ?? ''),
                'problem' => (string) ($x['problem'] ?? ''),
            ]) }}
            @if(!empty($x['suggested_value']))
                <br><small class="text-muted">@lang('productcompat::lang.suggested'): {{ $x['suggested_value'] }}</small>
            @endif
            {!! $badge($c) !!}
            <div style="margin-top:6px;">
                <form method="POST" action="{{ route('productcompat.candidate.apply') }}" style="display:inline;"
                      onsubmit="return confirm('@lang('productcompat::lang.remove_flag_confirm')');">
                    @csrf <input type="hidden" name="candidate_id" value="{{ $c->id }}">
                    <button class="btn btn-xs btn-danger"><i class="fa fa-trash"></i> @lang('productcompat::lang.remove_flag')</button>
                </form>
                <form method="POST" action="{{ route('productcompat.candidate.reject') }}" style="display:inline;">
                    @csrf <input type="hidden" name="id" value="{{ $c->id }}">
                    <button class="btn btn-xs btn-default"><i class="fa fa-check"></i> @lang('productcompat::lang.keep')</button>
                </form>
            </div>
        </div>
    @endforeach
@endif
