<?php

declare(strict_types=1);

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\AuditLog;
use App\Models\OrderRiskAssessment;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Inertia\Inertia;
use Inertia\Response;

class FraudQueueController extends Controller
{
    public function index(Request $request): Response
    {
        $query = OrderRiskAssessment::query()
            ->with(['user:id,name,email', 'order:id,order_number,total,status'])
            ->whereNull('reviewed_at');

        if ($request->filled('risk_level') && $request->input('risk_level') !== 'all') {
            $query->where('risk_level', $request->input('risk_level'));
        }

        if ($request->filled('action') && $request->input('action') !== 'all') {
            $query->where('auto_action', $request->input('action'));
        }

        $assessments = $query->latest()->paginate(25);

        return Inertia::render('Admin/FraudQueue/Index', [
            'assessments' => $assessments,
            'filters' => [
                'risk_level' => $request->input('risk_level', 'all'),
                'action' => $request->input('action', 'all'),
            ],
        ]);
    }

    public function show(OrderRiskAssessment $assessment): Response
    {
        $assessment->load([
            'user:id,name,email,status,created_at',
            'order:id,order_number,total,status,currency,created_at',
            'reviewer:id,name',
        ]);

        return Inertia::render('Admin/FraudQueue/Show', [
            'assessment' => $assessment,
        ]);
    }

    public function approve(Request $request, OrderRiskAssessment $assessment): RedirectResponse
    {
        $assessment->update([
            'reviewed_by' => $request->user()->id,
            'reviewed_at' => now(),
            'auto_action' => 'approve',
        ]);

        if ($assessment->order) {
            $assessment->order->update(['status' => 'processing']);
        }

        AuditLog::create([
            'user_id' => $assessment->user_id,
            'admin_id' => $request->user()->id,
            'action' => 'fraud_assessment_approved',
            'resource_type' => 'OrderRiskAssessment',
            'resource_id' => $assessment->id,
            'ip_address' => $request->ip(),
            'user_agent' => $request->userAgent(),
            'changes' => ['risk_score' => $assessment->risk_score, 'risk_level' => $assessment->risk_level],
        ]);

        return back()->with('success', 'Order approved and released from fraud queue.');
    }

    public function reject(Request $request, OrderRiskAssessment $assessment): RedirectResponse
    {
        $assessment->update([
            'reviewed_by' => $request->user()->id,
            'reviewed_at' => now(),
            'auto_action' => 'reject',
        ]);

        if ($assessment->order) {
            $assessment->order->update([
                'status' => 'cancelled',
                'cancelled_at' => now(),
            ]);
        }

        AuditLog::create([
            'user_id' => $assessment->user_id,
            'admin_id' => $request->user()->id,
            'action' => 'fraud_assessment_rejected',
            'resource_type' => 'OrderRiskAssessment',
            'resource_id' => $assessment->id,
            'ip_address' => $request->ip(),
            'user_agent' => $request->userAgent(),
            'changes' => ['risk_score' => $assessment->risk_score, 'risk_level' => $assessment->risk_level],
        ]);

        return back()->with('success', 'Order rejected and cancelled.');
    }
}
