<?php

namespace App\Filament\Resources\QuestionSets\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\TextInput;
use Filament\Resources\RelationManagers\RelationManager;
use Filament\Schemas\Components\Utilities\Get as SchemaGet;
use Filament\Schemas\Schema;
use Filament\Tables\Columns\TextColumn;
use Filament\Tables\Table;

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

    public function form(Schema $schema): Schema
    {
        return $schema
            ->components([
                \Filament\Forms\Components\Textarea::make('context')
                    ->label('Context / Preamble')
                    ->helperText('Extra text displayed before the question (e.g., "Read the following passage...")')
                    ->columnSpanFull(),
                \Filament\Forms\Components\Textarea::make('question_text')
                    ->required()
                    ->columnSpanFull(),
                \Filament\Forms\Components\Repeater::make('options')
                    ->schema([
                        TextInput::make('id')
                            ->required()
                            ->label('Option ID')
                            ->helperText('Use unique numbers or labels (e.g., 1, 2, A, B)'),
                        TextInput::make('text')
                            ->label('Option Text')
                            ->required(),
                    ])
                    ->default([
                        ['id' => '1', 'text' => ''],
                        ['id' => '2', 'text' => ''],
                        ['id' => '3', 'text' => ''],
                        ['id' => '4', 'text' => ''],
                    ])
                    ->addable()
                    ->deletable()
                    ->reorderable()
                    ->live()
                    ->columns(2),
                \Filament\Forms\Components\Select::make('correct_option_id')
                    ->label('Correct Option')
                    ->options(function (SchemaGet $get) {
                        $options = $get('options') ?? [];

                        return collect($options)
                            ->filter(fn ($item) => filled($item['id']))
                            ->mapWithKeys(fn ($item) => [$item['id'] => ($item['text'] ?: "Option {$item['id']}")])
                            ->toArray();
                    })
                    ->required()
                    ->live(),
                TextInput::make('marks')
                    ->numeric()
                    ->default(1),
            ]);
    }

    public function table(Table $table): Table
    {
        return $table
            ->recordTitleAttribute('question_text')
            ->columns([
                TextColumn::make('id')->sortable(),
                TextColumn::make('question_text')
                    ->limit(50)
                    ->searchable(),
                TextColumn::make('marks'),
            ])
            ->filters([
                //
            ])
            ->headerActions([
                CreateAction::make(),
            ])
            ->recordActions([
                EditAction::make(),
                DeleteAction::make(),
            ])
            ->toolbarActions([
                BulkActionGroup::make([
                    DeleteBulkAction::make(),
                ]),
            ]);
    }
}
