<?php

namespace App\Filament\Resources\Assessments\RelationManagers;

use Filament\Actions\BulkActionGroup;
use Filament\Actions\CreateAction;
use Filament\Actions\DeleteAction;
use Filament\Actions\DeleteBulkAction;
use Filament\Actions\EditAction;
use Filament\Forms\Components\Repeater;
use Filament\Forms\Components\Select;
use Filament\Forms\Components\Textarea;
use Filament\Forms\Components\TextInput;
use Filament\Forms\Components\Toggle;
use Filament\Resources\RelationManagers\RelationManager;
use Filament\Schemas\Schema;
use Filament\Tables\Columns\IconColumn;
use Filament\Tables\Columns\TextColumn;
use Filament\Tables\Table;

class QuestionsRelationManager extends RelationManager
{
    protected static string $relationship = 'allQuestions';

    protected static ?string $title = 'Questions';

    public function form(Schema $schema): Schema
    {
        return $schema
            ->components([
                Textarea::make('prompt')
                    ->label('Question prompt')
                    ->required()
                    ->rows(2)
                    ->columnSpanFull(),
                TextInput::make('section')
                    ->label('Section label (shown as a chip)')
                    ->placeholder('e.g. Money Habits'),
                TextInput::make('dimension')
                    ->label('Scoring dimension (key)')
                    ->placeholder('e.g. spending, saving, debt')
                    ->required(),
                Select::make('type')
                    ->options([
                        'choice' => 'Multiple choice',
                        'emoji_scale' => 'Emoji scale',
                        'this_or_that' => 'This or that (2 options)',
                    ])
                    ->default('choice')
                    ->required(),
                TextInput::make('sort')
                    ->numeric()
                    ->default(0),
                Textarea::make('help')
                    ->label('Helper text (optional)')
                    ->rows(2)
                    ->columnSpanFull(),
                TextInput::make('section_feedback')
                    ->label('Interstitial feedback after this question (optional)')
                    ->placeholder('e.g. Halfway there, you\'re doing great')
                    ->columnSpanFull(),
                Repeater::make('options')
                    ->label('Answer options')
                    ->schema([
                        TextInput::make('key')
                            ->required()
                            ->maxLength(8)
                            ->placeholder('a'),
                        TextInput::make('emoji')
                            ->maxLength(16)
                            ->placeholder('😀'),
                        TextInput::make('label')
                            ->required()
                            ->columnSpan(2),
                        TextInput::make('points')
                            ->numeric()
                            ->required()
                            ->default(0),
                    ])
                    ->columns(5)
                    ->defaultItems(4)
                    ->minItems(2)
                    ->reorderable()
                    ->columnSpanFull(),
                Toggle::make('is_active')
                    ->default(true),
            ]);
    }

    public function table(Table $table): Table
    {
        return $table
            ->recordTitleAttribute('prompt')
            ->defaultSort('sort')
            ->reorderable('sort')
            ->columns([
                TextColumn::make('sort')
                    ->label('#')
                    ->sortable(),
                TextColumn::make('section')
                    ->badge()
                    ->color('info'),
                TextColumn::make('prompt')
                    ->searchable()
                    ->limit(70)
                    ->wrap(),
                TextColumn::make('dimension')
                    ->badge(),
                IconColumn::make('is_active')
                    ->boolean(),
            ])
            ->headerActions([
                CreateAction::make(),
            ])
            ->recordActions([
                EditAction::make(),
                DeleteAction::make(),
            ])
            ->toolbarActions([
                BulkActionGroup::make([
                    DeleteBulkAction::make(),
                ]),
            ]);
    }
}
