<?php

namespace App\Http\Controllers\Admin\System;

use App\Http\Controllers\Controller;
use App\Models\OperationLog;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;

class OperationLogController extends Controller
{
    public function index(Request $request): JsonResponse
    {
        $query = OperationLog::query();

        $user = auth()->user();
        if (!$user->is_super) {
            $query->where('store_id', $user->store_id);
        }

        if ($request->keyword) {
            $query->where(function ($q) use ($request) {
                $q->where('user_name', 'like', "%{$request->keyword}%")
                  ->orWhere('target', 'like', "%{$request->keyword}%");
            });
        }
        if ($request->module) {
            $query->where('module', $request->module);
        }
        if ($request->start_date && $request->end_date) {
            $query->whereBetween('created_at', [$request->start_date, $request->end_date . ' 23:59:59']);
        }

        $logs = $query->orderByDesc('created_at')
            ->paginate($request->input('page_size', 20));

        return $this->paginate($logs);
    }
}
