<?php

namespace App\Filament\Resources\Courses\RelationManagers;

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

class ModulesRelationManager extends RelationManager
{
    protected static string $relationship = 'modules';

    public function form(Schema $schema): Schema
    {
        return $schema
            ->components([
                TextInput::make('title')
                    ->required(),
                TextInput::make('sort_order')
                    ->numeric()
                    ->default(0),

                Repeater::make('lessons')
                    ->relationship()
                    ->schema([
                        TextInput::make('title')->required(),
                        Select::make('type')
                            ->options([
                                'video' => 'Video',
                                'pdf' => 'PDF',
                                'ppt' => 'PPT',
                                'text' => 'Text',
                            ])
                            ->required()
                            ->reactive(),
                        FileUpload::make('content_url')
                            ->label('Content File')
                            ->disk('local')
                            ->directory('course-content')
                            ->hidden(fn ($get) => $get('type') === 'text' || $get('type') === null)
                            ->acceptedFileTypes(['video/mp4', 'application/pdf', 'application/vnd.ms-powerpoint', 'application/vnd.openxmlformats-officedocument.presentationml.presentation'])
                            ->preserveFilenames()
                            ->maxSize(307200)
                            ->visibility('private'),
                        TextInput::make('content_url_text')
                            ->label('Or External URL (YouTube etc)')
                            ->hidden(fn ($get) => $get('type') !== 'video' && $get('type') !== 'text'),
                        RichEditor::make('content_body')
                            ->hidden(fn ($get) => $get('type') !== 'text'),
                        TextInput::make('sort_order')->numeric()->default(0),
                    ])
                    ->orderColumn('sort_order')
                    ->defaultItems(0)
                    ->columnSpanFull(),
            ]);
    }

    public function table(Table $table): Table
    {
        return $table
            ->recordTitleAttribute('title')
            ->columns([
                TextColumn::make('title')
                    ->searchable()
                    ->sortable(),
                TextColumn::make('lessons_count')
                    ->counts('lessons')
                    ->label('Lessons'),
                TextColumn::make('sort_order')
                    ->sortable(),
            ])
            ->filters([
                //
            ])
            ->headerActions([
                CreateAction::make(),
                // Removed AssociateAction as Modules belong to Course, usually created directly
            ])
            ->actions([
                EditAction::make(),
                DeleteAction::make(),
            ])
            ->bulkActions([
                BulkActionGroup::make([
                    DeleteBulkAction::make(),
                ]),
            ]);
    }
}
