<?php

namespace App\Filament\Resources;

use App\Enums\UserStatus;
use App\Filament\Resources\UserResource\Pages;
use App\Filament\Resources\UserResource\Pages\CreateUser;
use App\Filament\Resources\UserResource\Pages\EditUser;
use App\Filament\Resources\UserResource\RelationManagers;
use App\Models\User;
use Filament\Forms;
use Filament\Forms\Form;
use Filament\Resources\Resource;
use Filament\Tables;
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\Section;
use Illuminate\Testing\Fluent\Concerns\Has;
use Illuminate\Validation\Rules\Password;
use Livewire\Attributes\Rule;
use function Livewire\Volt\rules;
use Illuminate\Support\Facades\Hash;
use Spatie\Permission\Models\Role;
use Filament\Forms\Components\Select;
use Livewire\Component;
use Filament\Notifications\Notification;

class UserResource extends Resource
{
    protected static ?string $model = User::class;

    protected static ?string $navigationIcon = 'heroicon-o-user-group';

    public static function form(Form $form): Form
    {
        return $form
            ->schema([
            Section::make('User Details')->schema([
                  TextInput::make('name')
                  
                  ->required(),
               TextInput::make('email')
               ->required()->unique(ignoreRecord:true),
                TextInput::make('password')
                ->required()
                ->password()
                ->dehydrateStateUsing(fn($state)=>Hash::make($state))
                ->visible(fn($livewire)=>$livewire  instanceof CreateUser)
                ->rule(Password::default())
                 ->hiddenOn('edit'),   
            ]),
             Select::make('status')
            ->options(UserStatus::class),

                Section::make('User New Password')->schema([ 
                TextInput::make('new_password')
                ->nullable()
                ->password()
                ->visibleOn('edit')
                ->visible(fn($livewire)=>$livewire  instanceof EditUser)
                ->rule(Password::default()),
                TextInput::make('new_password_confirmation')->password()
                ->same('new_password')
                ->requiredWith('new_password')
                 ->visibleOn('edit'),
           
            ]) ->visible(fn ($livewire) => $livewire instanceof EditUser),
    Select::make('roles')
    ->relationship('roles', 'name')
    ->preload()
    ->searchable(),
            ]);
           
    }

    public static function table(Table $table): Table
    {
        return $table
            ->columns([
             TextColumn::make('name')->searchable()->sortable(),
             TextColumn::make('email'),
             TextColumn::make('status')
             ->badge()
            
            ])
            ->filters([
                //
            ])
            ->actions([
                Tables\Actions\EditAction::make(),
                 Tables\Actions\ViewAction::make(),
                 Tables\Actions\DeleteAction::make()
                       ->successNotification(
                        Notification::make()
    ->success()
    ->title('Deleted User')
    ->body('The User Deleted SuccessFully')
                       )
            ])
            ->bulkActions([
                Tables\Actions\BulkActionGroup::make([
                    Tables\Actions\DeleteBulkAction::make(),
                ]),
            ]);
    }

    public static function getRelations(): array
    {
        return [
            //
        ];
    }

    public static function getPages(): array
    {
        return [
            'index' => Pages\ListUsers::route('/'),
            'create' => Pages\CreateUser::route('/create'),
            'edit' => Pages\EditUser::route('/{record}/edit'),
        ];
    }
}
