<?php

namespace App\Filament\Resources\Collections\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\FileUpload;
use Filament\Forms\Components\TextInput;
use Filament\Forms\Components\Toggle;
use Filament\Resources\RelationManagers\RelationManager;
use Filament\Schemas\Schema;
use Filament\Tables\Columns\ImageColumn;
use Filament\Tables\Columns\TextColumn;
use Filament\Tables\Columns\ToggleColumn;
use Filament\Tables\Table;

class CollectionItemsRelationManager extends RelationManager
{
    protected static string $relationship = 'collectionItems';

    protected static ?string $title = 'Kolleksiýa elementleri';

    public function form(Schema $schema): Schema
    {
        return $schema
            ->columns(4)
            ->components([
                TextInput::make('title')
                    ->label('Sözbaşy')
                    ->required(),
                TextInput::make('subtitle')
                    ->label('Sözbaşy asty'),
                TextInput::make('order')
                    ->label('Tertibi')
                    ->required()
                    ->numeric()
                    ->default(0),
                Toggle::make('is_active')
                    ->label('Işjeňmi')
                    ->default(true)
                    ->required(),
                FileUpload::make('image')
                    ->label('Suraty')
                    ->image()
                    ->visibility('public')
                    ->columnSpan(2),
            ]);
    }

    public function table(Table $table): Table
    {
        return $table
            ->recordTitleAttribute('title')
            ->columns([
                ImageColumn::make('image')
                    ->label('Suraty'),
                TextColumn::make('title')
                    ->label('Sözbaşy')
                    ->searchable(),
                TextColumn::make('subtitle')
                    ->label('Sözbaşy asty')
                    ->searchable(),
                TextColumn::make('order')
                    ->label('Tertibi')
                    ->sortable(),
                ToggleColumn::make('is_active')
                    ->label('Işjeňmi'),
            ])
            ->defaultSort('order')
            ->reorderable('order')
            ->filters([
                //
            ])
            ->headerActions([
                CreateAction::make(),
            ])
            ->recordActions([
                EditAction::make(),
                DeleteAction::make(),
            ])
            ->toolbarActions([
                BulkActionGroup::make([
                    DeleteBulkAction::make(),
                ]),
            ]);
    }
}
