<?php

namespace App\Filament\Resources\ApiProviders;

use App\Filament\Resources\ApiProviders\Pages;
use App\Filament\Resources\ApiProviders\RelationManagers;
use App\Models\ApiProvider;
use Filament\Schemas;
use Filament\Schemas\Schema;
use Filament\Resources\Resource;
use Filament\Tables;
use Filament\Actions\BulkActionGroup;
use Filament\Actions\CreateAction;
use Filament\Actions\DeleteBulkAction;
use Filament\Actions\EditAction;
use Filament\Tables\Table;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\SoftDeletingScope;
use Filament\Forms\Components\TextInput;
use Filament\Tables\Columns\TextColumn;
use Filament\Forms\Components\Select;
use Illuminate\Support\Facades\File;
use App\Api\Plugins\ApiPluginInterface;
use Filament\Forms\Components\Toggle;
use Filament\Tables\Columns\IconColumn;
use BackedEnum;

class ApiProviderResource extends Resource
{
    protected static ?string $model = ApiProvider::class;

    protected static BackedEnum|string|null $navigationIcon = 'heroicon-o-rectangle-stack';

    public static function form(Schema $schema): Schema
    {
        $plugins = self::getAvailablePlugins();
        return $schema
            ->components([
                TextInput::make('name')
                    ->required()
                    ->maxLength(255),
                Toggle::make('enabled')
                    ->default(true),
                TextInput::make('api_url')
                    ->required()
                    ->url()
                    ->maxLength(255),
                TextInput::make('username')
                    ->nullable()
                    ->maxLength(255),
                TextInput::make('password')
                    ->password()
                    ->nullable()
                    ->maxLength(255),
                TextInput::make('token')
                    ->nullable()
                    ->maxLength(255),
                Select::make('plugin')
                    ->options($plugins)
                    ->nullable(),
            ]);
    }

    public static function table(Table $table): Table
    {
        return $table
            ->columns([
                TextColumn::make('name')->searchable()->sortable(),
                IconColumn::make('enabled')
                    ->boolean(),
                TextColumn::make('api_url')->searchable(),
                TextColumn::make('plugin')->searchable()->sortable(),
            ])
            ->filters([
                //
            ])
            ->actions([
                EditAction::make(),
            ])
            ->bulkActions([
                BulkActionGroup::make([
                    DeleteBulkAction::make(),
                ]),
            ])
            ->emptyStateActions([
                CreateAction::make(),
            ]);
    }
    
    public static function getRelations(): array
    {
        return [
            //
        ];
    }
    
    public static function getPages(): array
    {
        return [
            'index' => Pages\ListApiProviders::route('/'),
            'create' => Pages\CreateApiProvider::route('/create'),
            'edit' => Pages\EditApiProvider::route('/{record}/edit'),
        ];
    }
    
    protected static function getAvailablePlugins(): array
    {
        $plugins = [];
        $path = app_path('Api/Plugins');
        $files = File::files($path);
        
        foreach ($files as $file) {
            $filename = $file->getFilenameWithoutExtension();
            if (in_array($filename, ['ApiPluginInterface', 'PluginLoader'])) {
                continue;
            }
            $class = "App\Api\Plugins\\" . $filename;
            if (class_exists($class) && in_array(ApiPluginInterface::class, class_implements($class))) {
                $plugins[$filename] = $filename;
            }
        }
        return $plugins;
    }
}
