<?php

namespace App\Filament\Pages\AdminVerification\Actions\Concerns;

use App\Enums\DecisionAdmin;
use App\Enums\VerificationStatus;
use App\Filament\Pages\Verification;
use App\Filament\Support\CheerfulNotification;
use App\Models\VerificationRequest;
use App\Models\VerificationReview;
use App\Notifications\BroadcastNotification;
use Filament\Actions\Action;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;

trait CanProcessReview
{
    protected function processReview(int $requestId, DecisionAdmin $decision, ?string $note): void
    {
        $verificationRequest = VerificationRequest::with(['company.partnerMedia.journalists'])->find($requestId);

        if (! $verificationRequest) {
            CheerfulNotification::danger(
                CheerfulNotification::getByKey('verification.admin.process.not_found.title'),
                CheerfulNotification::getByKey('verification.admin.process.not_found.body')
            )
                ->send();

            return;
        }

        try {
            DB::beginTransaction();

            // Create review record
            VerificationReview::create([
                'verification_request_id' => $verificationRequest->id,
                'reviewer_id' => auth()->id(),
                'decision' => $decision,
                'note' => $note,
            ]);

            // Update verification request status
            $newStatus = match ($decision) {
                DecisionAdmin::APPROVED => VerificationStatus::APPROVED,
                DecisionAdmin::NEED_REVISION => VerificationStatus::NEED_REVISION,
                DecisionAdmin::REJECTED => VerificationStatus::REJECTED,
            };

            $verificationRequest->update([
                'status' => $newStatus,
            ]);

            DB::commit();

            $actionLabel = match ($decision) {
                DecisionAdmin::APPROVED => 'disetujui',
                DecisionAdmin::NEED_REVISION => 'diminta revisi',
                DecisionAdmin::REJECTED => 'ditolak',
            };

            CheerfulNotification::success(
                CheerfulNotification::getByKey('verification.admin.process.success.title'),
                CheerfulNotification::getByKey('verification.admin.process.success.body', ['action' => $actionLabel])
            )
                ->send();

            // Notify User
            $partnerUser = $verificationRequest->company?->user;
            if ($partnerUser) {
                $statusTitle = match ($decision) {
                    DecisionAdmin::APPROVED => CheerfulNotification::getByKey('verification.admin.notif_user.approved.title'),
                    DecisionAdmin::NEED_REVISION => CheerfulNotification::getByKey('verification.admin.notif_user.revision.title'),
                    DecisionAdmin::REJECTED => CheerfulNotification::getByKey('verification.admin.notif_user.rejected.title'),
                };

                $body = match ($decision) {
                    DecisionAdmin::APPROVED => CheerfulNotification::getByKey('verification.admin.notif_user.approved.body'),
                    DecisionAdmin::NEED_REVISION => CheerfulNotification::getByKey('verification.admin.notif_user.revision.body'),
                    DecisionAdmin::REJECTED => CheerfulNotification::getByKey('verification.admin.notif_user.rejected.body'),
                };

                if ($note) {
                    $body .= "\n\n".CheerfulNotification::getByKey('verification.admin.notif_user.note', ['note' => $note]);
                }

                $partnerUser->notify(new BroadcastNotification([
                    'title' => $statusTitle,
                    'body' => $body,
                    'action' => [
                        Action::make('view')
                            ->label('Lihat')
                            ->url(Verification::getUrl()),
                    ],
                ]));
            }
        } catch (\Throwable $e) {
            DB::rollBack();
            Log::error('Verification processing failed', [
                'error' => $e->getMessage(),
                'trace' => $e->getTraceAsString(),
                'request_id' => $requestId,
                'decision' => $decision->value,
            ]);

            CheerfulNotification::danger(
                CheerfulNotification::getByKey('verification.admin.process.error.title'),
                CheerfulNotification::getByKey('verification.admin.process.error.body')
            )
                ->send();
        }
    }
}
