<?php

namespace App\Http\Controllers\Api\Admin;

use App\Http\Controllers\Controller;
use App\Models\AuditLogs;
use Illuminate\Http\Request;

class AdminAuditLogController extends Controller
{
    public function index(Request $request)
    {
        $logs = AuditLogs::query()
            ->with([
                'actor:id,name,email',
                // Aktör danışmansa public profiline link verebilmek için slug'ı da taşınır.
                'actor.consultantProfile:id,user_id,slug',
                'company:id,name,slug',
            ])

            ->when(
                $request->filled('action'),
                fn ($query) => $query->where(
                    'action',
                    $request->query('action')
                )
            )

            ->when(
                $request->filled('actor_id'),
                fn ($query) => $query->where(
                    'actor_id',
                    $request->query('actor_id')
                )
            )

            ->when(
                $request->filled('company_id'),
                fn ($query) => $query->where(
                    'company_id',
                    $request->query('company_id')
                )
            )

            ->when(
                $request->filled('listing_id'),
                fn ($query) => $query
                    ->where('subject_type', 'listing')
                    ->where('subject_id', $request->query('listing_id'))
            )

            ->when(
                $request->filled('ip'),
                fn ($query) => $query->where(
                    'ip_address',
                    $request->query('ip')
                )
            )

            ->latest()
            ->paginate(20)
            ->withQueryString();

        return response()->json($logs);
    }
}
