<?php

namespace App\Http\Controllers\Legacy;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class MonthlyCommentatorsController extends Controller
{
    public function index(Request $request)
    {
        $hits = 30;
        $page = max(1, (int) $request->query('page', 1));

        $query = DB::table('artwork_comments as t1')
            ->leftJoin('users as t2', 't1.user_id', '=', 't2.user_id')
            ->leftJoin('country as c', 't2.country', '=', 'c.id')
            ->where('t1.user_id', '>', 0)
            ->whereRaw("DATE_SUB(CURDATE(), INTERVAL 30 DAY) <= t1.date")
            ->select(
                't2.user_id',
                't2.uname',
                't2.user_type',
                't2.country',
                'c.name as country_name',
                'c.flag as country_flag',
                DB::raw('COUNT(*) as num_comments')
            )
            ->groupBy('t1.user_id')
            ->orderByDesc('num_comments');

        $rows = $query->paginate($hits)->withQueryString();

        $page_title = 'Monthly Top Commentators';

        return view('legacy::monthly-commentators', compact('page_title', 'rows'));
    }
}
