<?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\TextInput;
use Filament\Forms\Components\Textarea;
use Filament\Resources\RelationManagers\RelationManager;
use Filament\Schemas\Schema;
use Filament\Tables\Columns\TextColumn;
use Filament\Tables\Table;

class BandsRelationManager extends RelationManager
{
    protected static string $relationship = 'bands';

    protected static ?string $title = 'Result bands';

    public function form(Schema $schema): Schema
    {
        return $schema
            ->components([
                TextInput::make('label')
                    ->label('Band label (the persona)')
                    ->placeholder('e.g. The Wealth Builder')
                    ->required(),
                TextInput::make('emoji')
                    ->maxLength(16),
                TextInput::make('min_percent')
                    ->numeric()
                    ->minValue(0)
                    ->maxValue(100)
                    ->required(),
                TextInput::make('max_percent')
                    ->numeric()
                    ->minValue(0)
                    ->maxValue(100)
                    ->required(),
                TextInput::make('color')
                    ->placeholder('#58b9e6'),
                TextInput::make('sort')
                    ->numeric()
                    ->default(0),
                TextInput::make('headline')
                    ->label('Result headline')
                    ->columnSpanFull(),
                Textarea::make('message')
                    ->label('What this means (paragraph)')
                    ->rows(4)
                    ->columnSpanFull(),
                Repeater::make('advice')
                    ->label('Next 3 moves')
                    ->simple(
                        TextInput::make('tip')->required(),
                    )
                    ->defaultItems(3)
                    ->columnSpanFull(),
            ]);
    }

    public function table(Table $table): Table
    {
        return $table
            ->recordTitleAttribute('label')
            ->defaultSort('sort')
            ->columns([
                TextColumn::make('emoji'),
                TextColumn::make('label')
                    ->searchable(),
                TextColumn::make('min_percent')
                    ->suffix('%'),
                TextColumn::make('max_percent')
                    ->suffix('%'),
                TextColumn::make('headline')
                    ->limit(50)
                    ->wrap(),
            ])
            ->headerActions([
                CreateAction::make(),
            ])
            ->recordActions([
                EditAction::make(),
                DeleteAction::make(),
            ])
            ->toolbarActions([
                BulkActionGroup::make([
                    DeleteBulkAction::make(),
                ]),
            ]);
    }
}
