<?php

namespace App\Filament\Resources\Programs;

use App\Models\Program;
use BackedEnum;
use Filament\Actions\BulkActionGroup;
use Filament\Actions\DeleteAction;
use Filament\Actions\DeleteBulkAction;
use Filament\Actions\EditAction;
use Filament\Actions\ViewAction;
use Filament\Forms\Components\DatePicker;
use Filament\Forms\Components\Repeater;
use Filament\Forms\Components\Select;
use Filament\Forms\Components\TextInput;
use Filament\Resources\Resource;
use Filament\Schemas\Schema;
use Filament\Tables\Columns\TextColumn;
use Filament\Tables\Table;

class ProgramResource extends Resource
{
    protected static ?string $model = Program::class;

    protected static ?string $navigationLabel = 'Programmalar';

    protected static ?string $pluralLabel = 'Programmalar';

    protected static ?string $recordTitleAttribute = 'name';

    protected static ?int $navigationSort = 7;

    protected static string|BackedEnum|null $navigationIcon = 'heroicon-o-calendar-days';

    public static function form(Schema $schema): Schema
    {
        return $schema
            ->components([
                TextInput::make('name')
                    ->label('Programmanyň ady')
                    ->required()
                    ->default('Adaty')
                    ->columnSpanFull(),

                Select::make('group_id')
                    ->relationship('group', 'id')
                    ->label('Topar')
                    ->required()
                    ->columnSpanFull(),

                Repeater::make('days')
                    ->label('Günler')
                    ->schema([
                        DatePicker::make('day')
                            ->label('Gün (dd.mm.yyyy)')
                            ->native(false)
                            ->required(),
                        Repeater::make('activities')
                            ->label('Işler')
                            ->schema([
                                TextInput::make('time')->label('Wagty'),
                                TextInput::make('title')->label('Ady')->required(),
                                TextInput::make('description')->label('Düşündirişi'),

                                Select::make('iconSet')->label('Ikon toplumy')->options([
                                    'FontAwesome' => 'FontAwesome',
                                    'FontAwesome5' => 'FontAwesome5',
                                ])->default('FontAwesome5')->required(),

                                TextInput::make('iconName')->label('Ikon ady')->required(),
                                Select::make('transport')->label('Ulag')->options([
                                    'bus' => 'Bus',
                                    'car' => 'Car',
                                    'train' => 'Train',
                                    'plane' => 'Plane',
                                ])->default('bus')->required(),
                            ])
                            ->columns(2),
                    ])
                    ->columnSpanFull(),
            ]);
    }

    public static function table(Table $table): Table
    {
        return $table
            ->columns([
                TextColumn::make('name')->label('Programmanyň ady')->searchable(),
                TextColumn::make('group.name')->label('Topar')->searchable(),
            ])
            ->filters([
                //
            ])
            ->recordActions([
                EditAction::make(),
                ViewAction::make(),
                DeleteAction::make(),
            ])
            ->toolbarActions([
                BulkActionGroup::make([
                    DeleteBulkAction::make(),
                ]),
            ]);
    }

    public static function getPages(): array
    {
        return [
            'index' => Pages\ListPrograms::route('/'),
            'create' => Pages\CreateProgram::route('/create'),
            'edit' => Pages\EditProgram::route('/{record}/edit'),
            'view' => Pages\ViewProgram::route('/{record}'),
        ];
    }

    public static function mutateFormDataBeforeFill(array $data): array
    {
        if (! is_array($data['days'])) {
            $data['days'] = [];
        } else {
            $days = [];
            foreach ($data['days'] as $date => $activities) {
                $days[] = [
                    'day' => $date,
                    'activities' => $activities,
                ];
            }
            $data['days'] = $days;
        }

        return $data;
    }

    public static function mutateFormDataBeforeSave(array $data): array
    {
        $days = [];
        if (is_array($data['days'])) {
            foreach ($data['days'] as $day) {
                if (isset($day['day']) && isset($day['activities'])) {
                    $days[date('d.m.Y', strtotime($day['day']))] = $day['activities'];
                }
            }
        }
        $data['days'] = $days;

        return $data;
    }
}
