<?php

namespace App\Http\Controllers\Web\Bo;

use App\Http\Controllers\Controller;
use App\Models\MissionProof;
use App\Services\AdminAuditLogger;
use App\Services\InAppNotificationService;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;

class ProofModerationController extends Controller
{
    public function approve(
        Request $request,
        MissionProof $missionProof,
        AdminAuditLogger $auditLogger,
        InAppNotificationService $notificationService,
    ): RedirectResponse
    {
        $admin = $request->user();

        $missionProof->update([
            'status' => 'approved',
            'reviewed_by' => $admin?->id,
            'reviewed_at' => now(),
        ]);

        if ($admin) {
            $auditLogger->log($admin, 'proof.approve', $missionProof);
        }

        $this->notifyProofReviewed($missionProof, 'approved', $notificationService);

        return back()->with('status', 'Preuve mission approuvee.');
    }

    public function reject(
        Request $request,
        MissionProof $missionProof,
        AdminAuditLogger $auditLogger,
        InAppNotificationService $notificationService,
    ): RedirectResponse
    {
        $admin = $request->user();
        $data = $request->validate([
            'review_notes' => ['nullable', 'string', 'max:1000'],
        ]);

        $missionProof->update([
            'status' => 'rejected',
            'review_notes' => $data['review_notes'] ?? null,
            'reviewed_by' => $admin?->id,
            'reviewed_at' => now(),
        ]);

        if ($admin) {
            $auditLogger->log($admin, 'proof.reject', $missionProof, [
                'review_notes' => $data['review_notes'] ?? null,
            ]);
        }

        $this->notifyProofReviewed($missionProof, 'rejected', $notificationService);

        return back()->with('status', 'Preuve mission rejetee.');
    }

    private function notifyProofReviewed(
        MissionProof $missionProof,
        string $status,
        InAppNotificationService $notificationService,
    ): void {
        $proof = $missionProof->fresh(['mission:id,title,client_id']);
        $missionTitle = $proof->mission?->title ?? 'mission';

        $notificationService->notifyUser(
            $proof->liner_id,
            'proof.reviewed',
            'Preuve moderee',
            sprintf('Votre preuve pour "%s" est: %s.', $missionTitle, $status),
            [
                'mission_id' => $proof->mission_id,
                'proof_id' => $proof->id,
                'status' => $status,
            ],
        );

        if ($proof->mission?->client_id) {
            $notificationService->notifyUser(
                $proof->mission->client_id,
                'proof.reviewed',
                'Preuve moderee',
                sprintf('La preuve de "%s" est: %s.', $missionTitle, $status),
                [
                    'mission_id' => $proof->mission_id,
                    'proof_id' => $proof->id,
                    'status' => $status,
                ],
            );
        }
    }
}
