<?php

namespace App\Filament\Resources;

use Filament\Forms;
use App\Models\User;
use Filament\Tables;
use Filament\Forms\Form;
use Filament\Tables\Table;
use Filament\Resources\Resource;
use Filament\Forms\Components\Card;
use Filament\Forms\Components\Select;
use Filament\Tables\Columns\TextColumn;
use Filament\Forms\Components\TextInput;
use Filament\Forms\Components\DatePicker;
use Illuminate\Database\Eloquent\Builder;
use App\Filament\Resources\UserResource\Pages;
use Illuminate\Database\Eloquent\SoftDeletingScope;
use App\Filament\Resources\UserResource\RelationManagers;

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

    protected static ?string $navigationIcon = 'heroicon-o-users';
    protected static ?string $navigationLabel = 'Pengguna';
    protected static ?string $pluralLabel = 'Pengguna';
    protected static ?string $modelLabel = 'Pengguna';
    protected static ?string $slug = 'pengguna';

    public static function form(Form $form): Form
    {
        return $form
            ->schema([
                Card::make()
                    ->schema([
                        TextInput::make('nik')
                            ->label('NIK')
                            ->required()
                            ->placeholder('Masukkan NIK, max : 16 karakter')
                            ->unique(ignoreRecord: true)
                            ->maxLength(16),

                        TextInput::make('name')
                            ->label('Nama Lengkap')
                            ->required()
                            ->placeholder('Masukkan nama lengkap, max : 50 karakter')
                            ->maxLength(50),

                        DatePicker::make('birth_date')
                            ->label('Tanggal Lahir')
                            ->required()
                            ->placeholder('Pilih tanggal lahir'),

                        TextInput::make('place_of_birth')
                            ->label('Tempat Lahir')
                            ->required()
                            ->placeholder('Masukkan tempat lahir, max : 50 karakter')
                            ->maxLength(50),

                        TextInput::make('email')
                            ->label('Email')
                            ->required()
                            ->email()
                            ->placeholder('Masukkan email, max : 50 karakter')
                            ->maxLength(50),

                        TextInput::make('password')
                            ->label('Password')
                            ->required(fn ($context) => $context === 'create')
                            ->password()
                            ->placeholder('Masukkan password, min : 8 karakter')
                            ->minLength(8)
                            ->maxLength(255)
                            ->dehydrateStateUsing(function ($state, $record) {
                                return $state ? bcrypt($state) : $record->password;
                            }),

                        Select::make('roles')
                            ->label('Hak Akses')
                            ->relationship('roles', 'name')
                            ->preload()
                            ->required()
                            ->placeholder('Pilih peran pengguna'),
                    ])
            ]);
    }

    public static function table(Table $table): Table
    {
        return $table
            ->columns([
                TextColumn::make('nik')
                    ->label('NIK')
                    ->searchable()
                    ->sortable(),

                TextColumn::make('name')
                    ->label('Nama Lengkap')
                    ->searchable()
                    ->sortable(),

                TextColumn::make('email')
                    ->label('Email')
                    ->searchable()
                    ->sortable(),

                TextColumn::make('roles.name')
                    ->label('Hak Akses')
                    ->badge()
                    ->colors(['success'])
                    ->searchable()
                    ->sortable(),
            ])
            ->filters([
                //
            ])
            ->actions([
                Tables\Actions\EditAction::make(),
                Tables\Actions\DeleteAction::make(),
            ])
            ->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'),
        ];
    }
}
