<?php

declare(strict_types=1);

namespace App\Filament\Support;

use Filament\Actions\Action;
use Filament\Actions\BulkAction;
use Illuminate\Database\Eloquent\Collection;
use Maatwebsite\Excel\Facades\Excel;
use Symfony\Component\HttpFoundation\BinaryFileResponse;

class ExportExcelAction
{
    /**
     * @param  class-string  $exportClass
     */
    public static function make(string $name, string $exportClass, string $filename): Action
    {
        return Action::make($name)
            ->label(__('hr.actions.export'))
            ->icon('heroicon-o-arrow-down-tray')
            ->action(fn (): BinaryFileResponse => Excel::download(new $exportClass, $filename));
    }

    /**
     * @param  class-string  $exportClass
     */
    public static function bulk(string $name, string $exportClass, string $filename): BulkAction
    {
        return BulkAction::make($name)
            ->label(__('hr.actions.export_selected'))
            ->icon('heroicon-o-arrow-down-tray')
            ->action(function (Collection $records) use ($exportClass, $filename): BinaryFileResponse {
                $ids = $records->pluck('id')->all();

                return Excel::download(new $exportClass($ids), $filename);
            })
            ->deselectRecordsAfterCompletion();
    }
}
