<?php

namespace App\Filament\Resources\Images\Pages;

use App\Filament\Resources\Images\ImageResource;
use Filament\Actions;
use Filament\Resources\Pages\ListRecords;
use Illuminate\Contracts\Pagination\Paginator;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Support\Facades\Storage;
use Filament\Notifications\Notification;

class ListImages extends ListRecords
{
    protected static string $resource = ImageResource::class;

    protected function getHeaderActions(): array
    {
        return [
            Actions\Action::make('delete_all')
                ->label('Delete All Images')
                ->icon('heroicon-o-trash')
                ->color('danger')
                ->requiresConfirmation()
                ->action(function () {
                    // Delete all images from storage
                    Storage::disk('public')->deleteDirectory('images');
                    Storage::disk('public')->makeDirectory('images');
                    
                    // Show success notification
                    Notification::make()
                        ->title('All images deleted successfully')
                        ->success()
                        ->send();
                    
                    // Refresh the page
                    $this->redirect(static::getUrl());
                }),
        ];
    }

    protected function shouldPersistTableFiltersInSession(): bool
    {
        return true;
    }

    protected function shouldPersistTableSortInSession(): bool
    {
        return true;
    }

    protected function shouldPersistTableSearchInSession(): bool
    {
        return true;
    }

    protected function paginateTableQuery(Builder $query): Paginator
    {
        $paginator = parent::paginateTableQuery($query);
        return $paginator;
    }

    public function updatedTablePage($page)
    {
        $this->dispatch('table-pagination-updated', ['tableId' => $this->id, 'page' => $page]);
    }

    protected function getTableQueryStringIdentifier(): ?string
    {
        return 'images-table';
    }
}
