<?php
namespace App\Http\Controllers\API\Admin;

use App\Http\Controllers\Controller;
use App\Models\Vendor;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;

class AdminVendorController extends Controller
{
    public function index(Request $request): JsonResponse
    {
        $vendors = Vendor::with('user:id,name,email,status')
            ->when($request->search, fn($q) => $q->where('store_name', 'like', "%{$request->search}%"))
            ->when($request->status, fn($q) => $q->where('status', $request->status))
            ->when($request->plan, fn($q) => $q->where('subscription_plan', $request->plan))
            ->withCount(['products', 'orders'])
            ->latest()
            ->paginate(15);

        return response()->json($vendors);
    }

    public function show(int $id): JsonResponse
    {
        $vendor = Vendor::with(['user', 'products' => fn($q) => $q->latest()->limit(10), 'orders' => fn($q) => $q->latest()->limit(10)])
            ->withCount(['products', 'orders', 'reviews'])
            ->findOrFail($id);

        return response()->json(['vendor' => $vendor]);
    }

    public function update(Request $request, int $id): JsonResponse
    {
        $vendor = Vendor::findOrFail($id);
        $data = $request->validate([
            'commission_rate'      => 'sometimes|numeric|min:0|max:100',
            'subscription_plan'    => 'sometimes|in:free,pro,agency',
            'subscription_expires_at' => 'sometimes|nullable|date',
        ]);
        $vendor->update($data);
        return response()->json(['message' => 'Vendor updated.', 'vendor' => $vendor]);
    }

    public function destroy(int $id): JsonResponse
    {
        $vendor = Vendor::findOrFail($id);
        $vendor->delete();
        return response()->json(['message' => 'Vendor deleted.']);
    }

    public function store(Request $request): JsonResponse
    {
        return response()->json(['message' => 'Use registration endpoint.'], 405);
    }

    public function approve(int $id): JsonResponse
    {
        $vendor = Vendor::findOrFail($id);
        $vendor->update(['status' => 'approved', 'rejection_reason' => null]);
        $vendor->user->notify(new \App\Notifications\VendorStatusNotification('approved'));
        return response()->json(['message' => 'Vendor approved.']);
    }

    public function reject(Request $request, int $id): JsonResponse
    {
        $vendor = Vendor::findOrFail($id);
        $request->validate(['reason' => 'required|string|max:1000']);
        $vendor->update(['status' => 'rejected', 'rejection_reason' => $request->reason]);
        $vendor->user->notify(new \App\Notifications\VendorStatusNotification('rejected', $request->reason));
        return response()->json(['message' => 'Vendor rejected.']);
    }

    public function suspend(int $id): JsonResponse
    {
        $vendor = Vendor::findOrFail($id);
        $vendor->update(['status' => 'suspended']);
        return response()->json(['message' => 'Vendor suspended.']);
    }
}
