<?php $__env->startSection('content'); ?>
    <section class="page">
        <div class="section-header">
            <div>
                <h1>Problematic replies</h1>
                <p class="muted" style="margin: 0;">Review conversations where the AI struggled to answer or fell back to human agents.</p>
            </div>
        </div>

        <?php if(session('status')): ?>
            <div class="flash"><?php echo e(session('status')); ?></div>
        <?php endif; ?>

        <section class="panel">
            <div class="panel-header">
                <div>
                    <h2>Training queue</h2>
                    <span class="muted"><?php echo e($unknownQuestions->where('status', 'open')->count()); ?> open items</span>
                </div>
                <a class="button secondary" href="<?php echo e(route('conversations.index')); ?>">Open inbox</a>
            </div>
            <div class="panel-body">
                <?php if($unknownQuestions->isEmpty()): ?>
                    <div class="empty-state">No unknown questions yet.</div>
                <?php else: ?>
                    <div class="table-wrap">
                        <table class="table">
                            <thead>
                                <tr>
                                    <th>Question</th>
                                    <th>Contact</th>
                                    <th>Status</th>
                                    <th>Created</th>
                                    <th></th>
                                </tr>
                            </thead>
                            <tbody>
                                <?php $__currentLoopData = $unknownQuestions; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $unknownQuestion): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                    <tr>
                                        <td>
                                            <?php if($unknownQuestion->conversation): ?>
                                                <a href="<?php echo e(route('conversations.show', $unknownQuestion->conversation)); ?>">
                                                    <strong><?php echo e($unknownQuestion->body); ?></strong>
                                                </a>
                                            <?php else: ?>
                                                <strong class="text-muted" title="Conversation has been deleted"><?php echo e($unknownQuestion->body); ?></strong>
                                            <?php endif; ?>
                                        </td>
                                        <td><?php echo e($unknownQuestion->contact->name ?? $unknownQuestion->contact->phone ?? 'Unknown contact'); ?></td>
                                        <td>
                                            <span class="<?php echo \Illuminate\Support\Arr::toCssClasses(['badge', 'danger' => $unknownQuestion->status === 'open']); ?>">
                                                <?php echo e(ucfirst($unknownQuestion->status)); ?>

                                            </span>
                                        </td>
                                        <td class="muted"><?php echo e($unknownQuestion->created_at->diffForHumans()); ?></td>
                                        <td>
                                            <?php if($unknownQuestion->status === 'open'): ?>
                                                <form method="POST" action="<?php echo e(route('unknown-questions.resolve', $unknownQuestion)); ?>">
                                                    <?php echo csrf_field(); ?>
                                                    <button class="button secondary" type="submit">Resolve</button>
                                                </form>
                                            <?php else: ?>
                                                <span class="muted"><?php echo e($unknownQuestion->resolved_at?->diffForHumans()); ?></span>
                                            <?php endif; ?>
                                        </td>
                                    </tr>
                                <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                            </tbody>
                        </table>
                    </div>
                <?php endif; ?>
            </div>
        </section>
        <section class="panel" style="margin-top: 24px;">
            <div class="panel-header">
                <div>
                    <h2>User Corrections</h2>
                    <span class="muted"><?php echo e($reportedCorrections->where('status', 'pending')->count()); ?> pending</span>
                </div>
            </div>
            <div class="panel-body">
                <?php if($reportedCorrections->isEmpty()): ?>
                    <div class="empty-state">No user corrections yet.</div>
                <?php else: ?>
                    <div class="table-wrap">
                        <table class="table">
                            <thead>
                                <tr>
                                    <th>Customer's Original Message</th>
                                    <th>Suggested Correction</th>
                                    <th>Status</th>
                                    <th>Created</th>
                                    <th></th>
                                </tr>
                            </thead>
                            <tbody>
                                <?php $__currentLoopData = $reportedCorrections; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $correction): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                    <?php
                                        // Retrieve the preceding customer message
                                        $customerMessage = \App\Models\Message::where('conversation_id', $correction->conversation_id)
                                            ->where('id', '<', $correction->message_id)
                                            ->where('sender_type', 'customer')
                                            ->latest('id')
                                            ->first();
                                    ?>
                                    <tr>
                                        <td>
                                            <?php if($correction->conversation): ?>
                                                <a href="<?php echo e(route('conversations.show', $correction->conversation)); ?>">
                                                    <strong><?php echo e($customerMessage->body ?? '(Unknown Message)'); ?></strong>
                                                </a>
                                            <?php else: ?>
                                                <strong class="text-muted" title="Conversation has been deleted"><?php echo e($customerMessage->body ?? '(Unknown Message)'); ?></strong>
                                            <?php endif; ?>
                                        </td>
                                        <td><?php echo e($correction->suggested_reply); ?></td>
                                        <td>
                                            <span class="<?php echo \Illuminate\Support\Arr::toCssClasses(['badge', 'danger' => $correction->status === 'pending']); ?>">
                                                <?php echo e(ucfirst($correction->status)); ?>

                                            </span>
                                        </td>
                                        <td class="muted"><?php echo e($correction->created_at->diffForHumans()); ?></td>
                                        <td>
                                            <?php if($correction->status === 'pending'): ?>
                                                <div style="display: flex; gap: 8px;">
                                                    <form method="POST" action="<?php echo e(route('reports.corrections.approve', $correction)); ?>">
                                                        <?php echo csrf_field(); ?>
                                                        <button class="button secondary" type="submit" style="color: #38d66b; border-color: #38d66b;">Approve</button>
                                                    </form>
                                                    <form method="POST" action="<?php echo e(route('reports.corrections.reject', $correction)); ?>">
                                                        <?php echo csrf_field(); ?>
                                                        <button class="button secondary" type="submit" style="color: #ff3158; border-color: #ff3158;">Reject</button>
                                                    </form>
                                                </div>
                                            <?php else: ?>
                                                <span class="muted"><?php echo e($correction->resolved_at?->diffForHumans()); ?></span>
                                            <?php endif; ?>
                                        </td>
                                    </tr>
                                <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                            </tbody>
                        </table>
                    </div>
                <?php endif; ?>
            </div>
        </section>
    </section>
<?php $__env->stopSection(); ?>

<?php echo $__env->make('layouts.app', [
    'title' => 'Reports | VejalNai',
    'topbarTitle' => 'Reports',
    'topbarSubtitle' => $organization?->name ?? 'No organization assigned',
], array_diff_key(get_defined_vars(), ['__data' => 1, '__path' => 1]))->render(); ?><?php /**PATH /home/ecom123/public_html/chat.shunnointernational.com/resources/views/unknown-questions/index.blade.php ENDPATH**/ ?>