<?php

namespace App\Http\Controllers\Web\Bo;

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

class DisputeModerationController extends Controller
{
    public function resolve(
        Request $request,
        MissionDispute $missionDispute,
        AdminAuditLogger $auditLogger,
        InAppNotificationService $notificationService,
    ): RedirectResponse
    {
        $admin = $request->user();
        $data = $request->validate([
            'resolution_notes' => ['nullable', 'string', 'max:2000'],
        ]);

        $missionDispute->update([
            'status' => 'resolved',
            'resolution_notes' => $data['resolution_notes'] ?? null,
            'resolved_by' => $admin?->id,
            'resolved_at' => now(),
        ]);

        if ($admin) {
            $auditLogger->log($admin, 'dispute.resolve', $missionDispute, [
                'resolution_notes' => $data['resolution_notes'] ?? null,
            ]);
        }

        $this->notifyDisputeReviewed($missionDispute, 'resolved', $notificationService);

        return back()->with('status', 'Litige marque comme resolu.');
    }

    public function reject(
        Request $request,
        MissionDispute $missionDispute,
        AdminAuditLogger $auditLogger,
        InAppNotificationService $notificationService,
    ): RedirectResponse
    {
        $admin = $request->user();
        $data = $request->validate([
            'resolution_notes' => ['nullable', 'string', 'max:2000'],
        ]);

        $missionDispute->update([
            'status' => 'rejected',
            'resolution_notes' => $data['resolution_notes'] ?? null,
            'resolved_by' => $admin?->id,
            'resolved_at' => now(),
        ]);

        if ($admin) {
            $auditLogger->log($admin, 'dispute.reject', $missionDispute, [
                'resolution_notes' => $data['resolution_notes'] ?? null,
            ]);
        }

        $this->notifyDisputeReviewed($missionDispute, 'rejected', $notificationService);

        return back()->with('status', 'Litige rejete.');
    }

    private function notifyDisputeReviewed(
        MissionDispute $missionDispute,
        string $status,
        InAppNotificationService $notificationService,
    ): void {
        $dispute = $missionDispute->fresh(['mission:id,title']);
        $missionTitle = $dispute->mission?->title ?? 'mission';

        $recipientIds = collect([
            $dispute->reporter_id,
            $dispute->against_user_id,
        ])->filter()->unique();

        foreach ($recipientIds as $recipientId) {
            $notificationService->notifyUser(
                (int) $recipientId,
                'dispute.reviewed',
                'Mise a jour litige',
                sprintf('Le litige de "%s" est maintenant: %s.', $missionTitle, $status),
                [
                    'mission_id' => $dispute->mission_id,
                    'dispute_id' => $dispute->id,
                    'status' => $status,
                ],
            );
        }
    }
}
