<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\LeadershipClaim;
use Illuminate\Http\Request;

class LeadershipClaimController extends Controller
{
    public function index(Request $request)
    {
        $q = LeadershipClaim::with(['rank', 'user']);

        if ($request->filled('search')) {
            $s = trim($request->search);
            $q->whereHas('user', function($u) use ($s){
                $u->where('name','like',"%$s%")
                  ->orWhere('email','like',"%$s%")
                  ->orWhere('referral_code','like',"%$s%");
            });
        }

        if ($request->filled('rank_id')) {
            $q->where('rank_id', (int)$request->rank_id);
        }

        $claims = $q->latest('claimed_at')->paginate(50)->withQueryString();

        // simple rank list for filter dropdown
        $ranks = \App\Models\LeadershipRank::orderBy('sort_order')->get();

        return view('admin.leadership.claims.index', compact('claims','ranks'));
    }
}