<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\VerificationLog;
use Illuminate\Http\Request;
use Yajra\DataTables\Facades\DataTables;

class VerificationController extends Controller
{
    public function index(Request $request)
    {
        $type = $request->query('type');

        $prefixes = [
            'aadhaar_otp_send' => 'Aadhar OTP Send',
            'aadhaar_otp_submit' => 'Aadhar OTP Submit',
            'pan' => 'Paan Card Verification',
            'aadhaar_pan_link' => 'Aadhar-PAN Verification',
            'gst' => 'GST Verification',
            'bank' => 'Bank Verification',
            'obf' => 'OBF Validation',
            'user_creation' => 'User Creation',
        ];

        $typesCounts = [
            'all' => VerificationLog::count(),
        ];

        foreach (array_keys($prefixes) as $prefix) {
            $typesCounts[$prefix] = VerificationLog::where('verification_type', 'like', $prefix . '%')->count();
        }

        return view('admin.verifications', [
            'types' => $prefixes,
            'typesCounts' => $typesCounts,
            'currentType' => $type,
            'prefixes' => $prefixes,
        ]);
    }

    public function data(Request $request)
    {
        $type = $request->query('type');

        $query = VerificationLog::query()->with('apiClient')->orderBy('id', 'desc');
        if ($type) {
            $query->where('verification_type', 'like', $type . '%');
        }

        return DataTables::eloquent($query)
            ->addIndexColumn()
            ->addColumn('customer', function (VerificationLog $log) {
                return $log->apiClient?->name ?? $log->api_client_id;
            })
            ->addColumn('document', function (VerificationLog $log) {
                $inputs = $log->input_parameters ?? [];
                if (!empty($inputs['aadhaar_number'])) {
                    return $inputs['aadhaar_number'];
                }
                if (!empty($inputs['pan_number'])) {
                    return $inputs['pan_number'];
                }
                if (!empty($inputs['gstin'])) {
                    return $inputs['gstin'];
                }
                if (!empty($inputs['account_number'])) {
                    return $inputs['account_number'] . ' | ' . ($inputs['ifsc'] ?? '');
                }
                return $inputs['client_id'] ?? '-';
            })
            ->editColumn('status', function (VerificationLog $log) {
                $color = strtolower($log->status) === 'success' ? 'text-green-600' : 'text-red-600';
                return "<span class='{$color} font-semibold'>{$log->status}</span>";
            })
            ->addColumn('action', function (VerificationLog $log) {
                return '<a class="text-blue-600 hover:underline" href="' . route('admin.logs.show', $log->id) . '">View</a>';
            })
            ->rawColumns(['status', 'action'])
            ->toJson();
    }

    public function show($id)
    {
        $log = VerificationLog::with(['apiClient', 'aadhaarVerification'])->findOrFail($id);

        $processedCount = VerificationLog::where('verification_type', $log->verification_type)->count();

        return view('admin.show', ['log' => $log, 'processedCount' => $processedCount]);
    }
}
