<?php

namespace App\Filament\Resources\Courses\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\TextInput;
use Filament\Forms\Components\Textarea;
use Filament\Forms\Components\Toggle;
use Filament\Resources\RelationManagers\RelationManager;
use Filament\Schemas\Schema;
use Filament\Tables\Columns\TextColumn;
use Filament\Tables\Table;
use Illuminate\Support\Str;

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

    public function form(Schema $schema): Schema
    {
        return $schema
            ->components([
                TextInput::make('title')
                    ->required()
                    ->maxLength(255),
                TextInput::make('sort')
                    ->numeric()
                    ->default(0),
                Textarea::make('summary')
                    ->rows(2)
                    ->columnSpanFull(),
                Toggle::make('promotion_enabled')
                    ->label('Show product card after this module')
                    ->live(),
                TextInput::make('promotion_icon')
                    ->label('Product card icon')
                    ->helperText('Icon name, for example: calculator, book-open, target, or message-circle.')
                    ->visible(fn (callable $get): bool => (bool) $get('promotion_enabled')),
                TextInput::make('promotion_eyebrow')
                    ->label('Product card eyebrow')
                    ->visible(fn (callable $get): bool => (bool) $get('promotion_enabled')),
                TextInput::make('promotion_title')
                    ->label('Product card title')
                    ->visible(fn (callable $get): bool => (bool) $get('promotion_enabled'))
                    ->columnSpan(2),
                Textarea::make('promotion_body')
                    ->label('Product card description')
                    ->visible(fn (callable $get): bool => (bool) $get('promotion_enabled'))
                    ->rows(2)
                    ->columnSpanFull(),
                TextInput::make('promotion_label')
                    ->label('Product card button label')
                    ->visible(fn (callable $get): bool => (bool) $get('promotion_enabled')),
                TextInput::make('promotion_url')
                    ->label('Product card link')
                    ->helperText('Use a site path such as /tools/budget or a full https:// URL.')
                    ->visible(fn (callable $get): bool => (bool) $get('promotion_enabled')),
                Repeater::make('lessons')
                    ->relationship()
                    ->orderColumn('sort')
                    ->schema([
                        TextInput::make('title')
                            ->required()
                            ->live(onBlur: true)
                            ->afterStateUpdated(fn ($state, callable $set) => $set('slug', Str::slug($state ?? '')))
                            ->columnSpan(2),
                        TextInput::make('slug')
                            ->required(),
                        TextInput::make('video_url')
                            ->label('YouTube URL')
                            ->url()
                            ->columnSpan(2),
                        Textarea::make('summary')
                            ->rows(2)
                            ->columnSpanFull(),
                        TextInput::make('duration_minutes')
                            ->numeric()
                            ->default(0),
                        Toggle::make('is_free_preview')
                            ->label('Free preview'),
                    ])
                    ->columns(3)
                    ->reorderable()
                    ->collapsible()
                    ->itemLabel(fn (array $state): ?string => $state['title'] ?? null)
                    ->columnSpanFull(),
            ]);
    }

    public function table(Table $table): Table
    {
        return $table
            ->recordTitleAttribute('title')
            ->defaultSort('sort')
            ->reorderable('sort')
            ->columns([
                TextColumn::make('sort')
                    ->label('#'),
                TextColumn::make('title')
                    ->searchable(),
                TextColumn::make('lessons_count')
                    ->counts('lessons')
                    ->label('Lessons'),
            ])
            ->headerActions([
                CreateAction::make(),
            ])
            ->recordActions([
                EditAction::make(),
                DeleteAction::make(),
            ])
            ->toolbarActions([
                BulkActionGroup::make([
                    DeleteBulkAction::make(),
                ]),
            ]);
    }
}
