<?php

namespace MoneyTransfer\AuditLog\Http\Controllers;

use App\Http\Controllers\Controller;
use MoneyTransfer\AuditLog\Models\AuditHistory;
use Illuminate\Http\Request;

class AuditLogController extends Controller
{
    public function index(Request $request)
    {
        $limit = $request->integer('paginate', 10);
        $limit = $limit > 0 ? $limit : 10;

        $histories = AuditHistory::query()
            ->select('id', 'user_id', 'module', 'action', 'ip_address', 'reference_user', 'reference_id', 'reference_name', 'type', 'created_at', 'updated_at')
            ->with('user')
            ->orderByDesc('created_at')
            ->paginate($this->recordsLimit());

        return response()->json([
            'rows' => $histories
        ]);
    }

    public function deleteAll()
    {
        AuditHistory::query()->truncate();

        return $this
            ->httpResponse()
            ->withDeletedSuccessMessage();
    }
}
