<?php

namespace App\Filament\Association\Resources;

use App\Filament\Association\Resources\ProfileFieldResource\Pages;
use App\Filament\Association\Resources\ProfileFieldResource\RelationManagers;
use App\Models\ProfileField;
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;

class ProfileFieldResource extends Resource
{
    protected static ?string $model = ProfileField::class;

    protected static ?string $navigationGroup = 'Member Settings';

    protected static ?string $tenantOwnershipRelationshipName = 'association';

    public static function form(Form $form): Form
    {
        return $form
            ->schema([
                Forms\Components\Section::make('Field Definition')
                    ->schema([
                        Forms\Components\Select::make('group_id')
                            ->relationship('group', 'name')
                            ->required()
                            ->preload(),
                        Forms\Components\TextInput::make('key')
                            ->required()
                            ->maxLength(255),
                        Forms\Components\TextInput::make('label')
                            ->required()
                            ->maxLength(255),
                        Forms\Components\Select::make('field_type')
                            ->options([
                                'text' => 'Short Text',
                                'textarea' => 'Long Text',
                                'number' => 'Number',
                                'date' => 'Date',
                                'select' => 'Dropdown',
                                'multiselect' => 'Multi-select',
                                'file' => 'File',
                                'image' => 'Image',
                            ])
                            ->required()
                            ->live(),
                        Forms\Components\TextInput::make('order')
                            ->numeric()
                            ->default(0),
                    ])->columns(2),

                Forms\Components\Section::make('Configuration')
                    ->schema([
                        Forms\Components\KeyValue::make('options')
                            ->visible(fn (Forms\Get $get) => in_array($get('field_type'), ['select', 'multiselect'])),
                        Forms\Components\TextInput::make('default_value'),
                        Forms\Components\Grid::make(3)
                            ->schema([
                                Forms\Components\Toggle::make('is_required'),
                                Forms\Components\Toggle::make('is_searchable'),
                                Forms\Components\Toggle::make('is_visible')
                                    ->default(true),
                            ]),
                    ]),
            ]);
    }

    public static function table(Table $table): Table
    {
        return $table
            ->columns([
                Tables\Columns\TextColumn::make('label')
                    ->searchable()
                    ->sortable(),
                Tables\Columns\TextColumn::make('group.name')
                    ->sortable(),
                Tables\Columns\TextColumn::make('field_type')
                    ->badge(),
                Tables\Columns\IconColumn::make('is_required')
                    ->boolean()
                    ->label('Req'),
                Tables\Columns\IconColumn::make('is_visible')
                    ->boolean()
                    ->label('Vis'),
                Tables\Columns\TextColumn::make('order')
                    ->numeric()
                    ->sortable(),
            ])
            ->defaultSort('order')
            ->reorderable('order')
            ->filters([
                Tables\Filters\SelectFilter::make('group_id')
                    ->relationship('group', 'name')
                    ->label('Group'),
                Tables\Filters\SelectFilter::make('field_type'),
            ])
            ->actions([
                Tables\Actions\EditAction::make(),
            ])
            ->bulkActions([
                Tables\Actions\BulkActionGroup::make([
                    Tables\Actions\DeleteBulkAction::make(),
                ]),
            ]);
    }

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

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