<?php

declare(strict_types=1);

/**
 * 本文件属于 DBErp 进销存系统。
 *
 * 版权所有 © 2026 北京珑大钜商科技有限公司。保留所有权利。
 *
 * 未经官方授权，不得删除、篡改或隐藏本文件中的版权声明、产品标识与来源信息。
 * 商业授权、官方更新与技术支持服务，请以珑大官方支持中心记录为准。
 *
 * @link https://support.loongdom.cn
 * @see  https://www.dberp.com.cn
 */

namespace App\Filament\Resources\DberpAuditLogs\Pages;

use App\Filament\Resources\DberpAuditLogs\DberpAuditLogResource;
use App\Services\DberpAuditLogPruningService;
use App\Services\DberpPermissionService;
use App\Support\AuditLogDisplay\DberpAuditLogDisplayFormatter;
use Filament\Actions\Action;
use Filament\Notifications\Notification;
use Filament\Resources\Pages\ListRecords;
use Illuminate\Contracts\Pagination\AbstractPaginator;
use Illuminate\Contracts\Pagination\CursorPaginator;
use Illuminate\Contracts\View\View;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Auth;

class ListDberpAuditLogs extends ListRecords
{
    protected static string $resource = DberpAuditLogResource::class;

    public function render(): View
    {
        $records = $this->getTableRecords();
        $logs = match (true) {
            $records instanceof AbstractPaginator => collect($records->items()),
            $records instanceof CursorPaginator => collect($records->items()),
            $records instanceof Collection => $records,
            default => collect(),
        };

        if ($logs->isNotEmpty()) {
            app(DberpAuditLogDisplayFormatter::class)->preloadRelationsForLogs($logs);
        }

        return parent::render();
    }

    protected function getHeaderActions(): array
    {
        return [
            Action::make('purgeExpiredAuditLogs')
                ->label(__('dberp.audit.purge_expired_action'))
                ->color('danger')
                ->requiresConfirmation()
                ->modalHeading(__('dberp.audit.purge_expired_modal_heading'))
                ->modalDescription(fn (): string => (string) __('dberp.audit.purge_expired_modal_description', [
                    'days' => config('dberp.audit.retention_days', 7),
                ]))
                ->visible(fn (): bool => app(DberpPermissionService::class)->allows(Auth::user(), DberpPermissionService::PURGE_AUDIT_LOG))
                ->action(function (DberpAuditLogPruningService $pruning): void {
                    $days = (int) config('dberp.audit.retention_days', 7);
                    $deleted = $pruning->deleteOlderThan($days);

                    if ($deleted === 0) {
                        Notification::make()
                            ->title(__('dberp.audit.purge_nothing_title'))
                            ->body((string) __('dberp.audit.purge_nothing_body', ['days' => $days]))
                            ->info()
                            ->send();

                        return;
                    }

                    Notification::make()
                        ->title(__('dberp.audit.purge_completed_title'))
                        ->body((string) __('dberp.audit.purge_completed_body', ['count' => $deleted, 'days' => $days]))
                        ->success()
                        ->send();
                }),
        ];
    }
}
