<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Hash;

class UserController extends Controller
{
    public function index(Request $request)
    {
        $size = $request->get('size', 20);
        $role = $request->get('role');
        $status = $request->get('status');
        $search = $request->get('search');

        $query = DB::table('users')->orderByDesc('id');
        if ($role) $query->whereRaw('LOWER(role) = LOWER(?)', [$role]);
        if ($status) $query->where('status', $status);
        if ($search) $query->where('name', 'like', "%$search%")->orWhere('phone', 'like', "%$search%");

        $users = $query->paginate($size)->withQueryString();

        $roleStats = DB::table('users')->selectRaw('role, count(*) as cnt')->groupBy('role')->pluck('cnt', 'role');
        $statusStats = DB::table('users')->selectRaw('status, count(*) as cnt')->groupBy('status')->pluck('cnt', 'status');
        $user = auth()->user();
        $message = session('message');

        return view('user.index', compact('users', 'roleStats', 'statusStats', 'user', 'message', 'size'));
    }

    public function create(Request $request)
    {
        DB::table('users')->insert([
            'name' => $request->name,
            'phone' => $request->phone,
            'password' => Hash::make($request->password ?? '12345678'),
            'avatar' => '/assets/images/users/avatar-5.jpg',
            'status' => 'active',
            'role' => $request->role ?? 'user',
            'balance' => 0,
            'created_at' => now(),
            'updated_at' => now(),
        ]);
        return redirect()->route('user.index')->with('message', 'User created');
    }

    public function show($id)
    {
        $user = DB::table('users')->find($id);
        return view('user.show', compact('user'));
    }

    public function edit()
    {
        $user = auth()->user();
        return view('user.edit', compact('user'));
    }

    public function update(Request $request)
    {
        return redirect()->back();
    }

    public function favourite($id)
    {
        return redirect()->back();
    }

    public function updatePassword(Request $request)
    {
        return redirect()->back();
    }

    public function activate($id)
    {
        DB::table('users')->where('id', $id)->update(['status' => 'active']);
        return redirect()->back();
    }

    public function block($id)
    {
        DB::table('users')->where('id', $id)->update(['status' => 'inactive']);
        return redirect()->back();
    }

    public function appraisers(Request $request)
    {
        $size = $request->get('size', 20);
        $users = DB::table('users')->whereRaw('LOWER(role) = ?', ['appraiser'])->paginate($size);
        $roleStats = DB::table('users')->selectRaw('role, count(*) as cnt')->groupBy('role')->pluck('cnt', 'role');
        $statusStats = DB::table('users')->selectRaw('status, count(*) as cnt')->groupBy('status')->pluck('cnt', 'status');
        $user = auth()->user();
        return view('user.appraisers', compact('users', 'roleStats', 'statusStats', 'user', 'size'));
    }

    public function appraisersBonus()
    {
        $users = DB::table('users')->whereRaw('LOWER(role) = ?', ['appraiser'])->get();
        return view('user.appraisers-bonus', compact('users'));
    }

    public function destroy($id)
    {
        return redirect()->route('user.index');
    }
}
