<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\SmartLeadershipReward;
use App\Models\User;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class SmartLeadershipAdminController extends Controller
{
    public function index(Request $request)
    {
        $q = trim((string) $request->query('q', ''));
        $dateFrom = $request->query('date_from');
        $dateTo = $request->query('date_to');

        $base = SmartLeadershipReward::query()->with('user:id,name,referral_code,mobile');
        $this->applyFilters($base, $q, $dateFrom, $dateTo);

        $summary = (clone $base)
            ->selectRaw('COUNT(*) as total_claims, COALESCE(SUM(amount), 0) as total_amount')
            ->first();

        $rewards = (clone $base)
            ->select('user_id')
            ->selectRaw('COUNT(*) as times')
            ->selectRaw('COALESCE(SUM(amount), 0) as total_amount')
            ->selectRaw('MAX(claimed_at) as last_claimed')
            ->groupBy('user_id')
            ->orderByDesc(DB::raw('MAX(claimed_at)'))
            ->paginate(30)
            ->withQueryString();

        return view('admin.smart-leadership.index', compact(
            'rewards',
            'summary',
            'q',
            'dateFrom',
            'dateTo'
        ));
    }

    public function show(Request $request, User $user)
    {
        $dateFrom = $request->query('date_from');
        $dateTo = $request->query('date_to');

        $claimsBase = SmartLeadershipReward::query()
            ->where('user_id', $user->id)
            ->when($dateFrom, fn ($q) => $q->whereDate('claimed_at', '>=', $dateFrom))
            ->when($dateTo, fn ($q) => $q->whereDate('claimed_at', '<=', $dateTo));

        $totals = (clone $claimsBase)
            ->selectRaw('COUNT(*) as times')
            ->selectRaw('COALESCE(SUM(amount), 0) as total_amount')
            ->selectRaw('MAX(claimed_at) as last_claimed')
            ->first();

        $claims = (clone $claimsBase)
            ->select('level', 'amount', 'claimed_at')
            ->orderByDesc('claimed_at')
            ->paginate(30)
            ->withQueryString();

        return view('admin.smart-leadership.show', compact(
            'user',
            'claims',
            'totals',
            'dateFrom',
            'dateTo'
        ));
    }

    private function applyFilters(Builder $query, string $q, ?string $dateFrom, ?string $dateTo): void
    {
        if ($q !== '') {
            $query->whereHas('user', function ($u) use ($q) {
                $u->where('name', 'like', '%' . $q . '%')
                    ->orWhere('referral_code', 'like', '%' . $q . '%')
                    ->orWhere('mobile', 'like', '%' . $q . '%');
            });
        }

        if (!empty($dateFrom)) {
            $query->whereDate('claimed_at', '>=', $dateFrom);
        }

        if (!empty($dateTo)) {
            $query->whereDate('claimed_at', '<=', $dateTo);
        }
    }
}
