<?php

namespace App\Components\Client;

use App\Jobs\Synapse\ExtractEvidenceFromAttachmentJob;
use App\Jobs\Synapse\ExtractEvidenceFromDocumentJob;
use App\Models\Client\Client;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Livewire\Attributes\On;
use Livewire\Component;

class ClientEvidence extends Component
{
    use AuthorizesRequests;

    public Client $client;

    public function mount(): void
    {
        $this->authorize('dsars.view');
    }

    public function runAttachmentExtraction(int $attachmentId, int $dsarId): void
    {
        $this->authorize('synapse.use');
        ExtractEvidenceFromAttachmentJob::dispatch($attachmentId, $dsarId);
    }

    public function runDocumentExtraction(int $documentId): void
    {
        $this->authorize('synapse.use');
        ExtractEvidenceFromDocumentJob::dispatch($documentId);
    }

    #[On('evidenceItemUpdated')]
    public function refresh(): void {}

    public function render()
    {
        // DSAR email-attachment evidence rows
        $dsarRows = collect();
        if ($this->client->claim) {
            foreach ($this->client->claim->dsars as $dsar) {
                foreach ($dsar->evidenceAttachments() as $attachment) {
                    $items = $attachment->evidenceItems;
                    $dsarRows->push([
                        'dsar'       => $dsar,
                        'attachment' => $attachment,
                        'items'      => $items,
                        'confirmed'  => $items->filter->isConfirmed()->count(),
                        'unreviewed' => $items->filter->isUnreviewed()->count(),
                        'dismissed'  => $items->filter->isDismissed()->count(),
                    ]);
                }
            }
        }

        // Document Manager evidence rows
        $documentRows = $this->client->documents()
            ->with(['evidenceItems', 'type'])
            ->orderByDesc('created_at')
            ->get()
            ->map(function ($document) {
                $items = $document->evidenceItems;
                return [
                    'document'   => $document,
                    'items'      => $items,
                    'confirmed'  => $items->filter->isConfirmed()->count(),
                    'unreviewed' => $items->filter->isUnreviewed()->count(),
                    'dismissed'  => $items->filter->isDismissed()->count(),
                ];
            });

        // Grand totals
        $allItems    = $dsarRows->flatMap(fn ($r) => $r['items'])
                           ->merge($documentRows->flatMap(fn ($r) => $r['items']));
        $totalItems  = $allItems->count();
        $confirmed   = $allItems->filter->isConfirmed()->count();
        $unreviewed  = $allItems->filter->isUnreviewed()->count();
        $dismissed   = $allItems->filter->isDismissed()->count();

        return view('components.client.client-evidence', compact(
            'dsarRows', 'documentRows', 'totalItems', 'confirmed', 'unreviewed', 'dismissed'
        ));
    }
}
