<?php

namespace App\Http\Controllers\Admin;

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

class ClientLogController extends Controller
{
    /**
     * Client log list query
     * @param Request $request
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function index(Request $request)
    {
        $logType = $request->input('log_type', '');
        $logDate = $request->input('log_date', '');
        $channel = $request->input('Channel', '');
        $page = $request->input('page', 1);

        $query = DB::table(ClientLog::TABLE);

        // Filter by log_type
        if (!empty($logType)) {
            $query->where('log_type', $logType);
        }

        // Filter by date
        if (!empty($logDate)) {
            $query->where('log_date', $logDate);
        }

        // Filter by channel
        if (!empty($channel)) {
            $query->where('Channel', $channel);
        }

        // Order by created_at desc
        $query->orderBy('created_at', 'desc');

        // Pagination
        $perPage = 20;
        $list = $query->paginate($perPage, ['*'], 'page', $page);

        return view('admin.client_log.index', [
            'list' => $list,
            'log_type' => $logType,
            'log_date' => $logDate,
            'Channel' => $channel,
        ]);
    }

    /**
     * API: Query client log
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function query(Request $request)
    {
        try {
            $logType = $request->input('log_type', '');
            $logDate = $request->input('log_date', '');
            $userID = $request->input('userID', '');
            $channel = $request->input('Channel', '');
            $page = $request->input('page', 1);
            $perPage = $request->input('per_page', 20);

            $query = DB::table(ClientLog::TABLE);

            // Filter by log_type
            if (!empty($logType)) {
                $query->where('log_type', $logType);
            }

            // Filter by date
            if (!empty($logDate)) {
                $query->where('log_date', $logDate);
            }

            // Filter by userID
            if (!empty($userID)) {
                $query->where('userID', $userID);
            }

            // Filter by channel
            if (!empty($channel)) {
                $query->where('Channel', $channel);
            }

            // Order by created_at desc
            $query->orderBy('created_at', 'desc');

            // Pagination
            $list = $query->paginate($perPage, ['*'], 'page', $page);

            return response()->json([
                'code' => 0,
                'data' => $list->items(),
                'total' => $list->total(),
                'per_page' => $list->perPage(),
                'current_page' => $list->currentPage(),
                'last_page' => $list->lastPage(),
            ]);
        } catch (\Exception $e) {
            \Log::error('Client log query error: ' . $e->getMessage(), [
                'request' => $request->all(),
                'trace' => $e->getTraceAsString()
            ]);
            return response()->json([
                'code' => 1,
                'message' => 'Query failed: ' . $e->getMessage()
            ]);
        }
    }
}
