<?php

namespace App\Http\Controllers\SuperAdmin;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Inertia\Inertia;
use Inertia\Response;

class AuditLogController extends Controller
{
    public function index(Request $request): Response
    {
        $schoolFilter = $request->integer('school_id') ?: null;

        $logs = DB::table('audit_logs')
            ->leftJoin('users', 'users.id', '=', 'audit_logs.user_id')
            ->leftJoin('schools', 'schools.id', '=', 'audit_logs.school_id')
            ->when($schoolFilter, fn ($q) => $q->where('audit_logs.school_id', $schoolFilter))
            ->orderByDesc('audit_logs.created_at')
            ->limit(200)
            ->get([
                'audit_logs.id',
                'audit_logs.action',
                'audit_logs.entity_type',
                'audit_logs.entity_id',
                'audit_logs.description',
                'audit_logs.ip_address',
                'audit_logs.created_at',
                'users.username',
                'users.first_name',
                'users.last_name',
                'users.role_key',
                'schools.name_fa as school_name',
            ]);

        $loginLogs = DB::table('login_logs')
            ->leftJoin('schools', 'schools.id', '=', 'login_logs.school_id')
            ->when($schoolFilter, fn ($q) => $q->where('login_logs.school_id', $schoolFilter))
            ->orderByDesc('login_logs.created_at')
            ->limit(100)
            ->get([
                'login_logs.id',
                'login_logs.username',
                'login_logs.status',
                'login_logs.ip_address',
                'login_logs.created_at',
                'schools.name_fa as school_name',
            ]);

        $schools = DB::table('schools')->orderBy('name_fa')->get(['id', 'name_fa']);

        return Inertia::render('SuperAdmin/AuditLogs', [
            'schoolFilter' => $schoolFilter,
            'schools' => $schools->map(fn ($s): array => ['id' => (int) $s->id, 'name' => $s->name_fa])->values(),
            'logs' => $logs->map(fn ($log): array => [
                'id' => (int) $log->id,
                'action' => $log->action,
                'entityType' => $log->entity_type ?? '—',
                'entityId' => $log->entity_id,
                'description' => $log->description,
                'ipAddress' => $log->ip_address ?? '—',
                'createdAt' => $this->jalaliDate($log->created_at, true),
                'user' => trim(($log->first_name ?? '').' '.($log->last_name ?? '')) ?: ($log->username ?? 'سیستم'),
                'role' => $log->role_key ?? '—',
                'school' => $log->school_name ?? 'سیستم',
            ])->values(),
            'loginLogs' => $loginLogs->map(fn ($log): array => [
                'id' => (int) $log->id,
                'username' => $log->username,
                'status' => $log->status,
                'ipAddress' => $log->ip_address ?? '—',
                'createdAt' => $this->jalaliDate($log->created_at, true),
                'school' => $log->school_name ?? '—',
            ])->values(),
        ]);
    }
}
