<?php

namespace App\Filament\Admin\Resources\Academies;

use App\Filament\Admin\Resources\Academies\Pages\ManageAcademies;
use App\Models\Academy;
use BackedEnum;
use Filament\Actions\BulkActionGroup;
use Filament\Actions\DeleteAction;
use Filament\Actions\DeleteBulkAction;
use Filament\Actions\EditAction;
use Filament\Forms\Components\TextInput;
use Filament\Forms\Components\Toggle;
use Filament\Resources\Resource;
use Filament\Schemas\Schema;
use Filament\Support\Icons\Heroicon;
use Filament\Tables\Columns\IconColumn;
use Filament\Tables\Columns\TextColumn;
use Filament\Tables\Table;
use Filament\Schemas\Components\Utilities\Get;
use Filament\Forms\Components\Select;

class AcademyResource extends Resource
{
    protected static ?string $model = Academy::class;

    protected static string|BackedEnum|null $navigationIcon = Heroicon::OutlinedRectangleStack;

    protected static ?string $recordTitleAttribute = 'Academy';

    public static function form(Schema $schema): Schema
    {
        return $schema
            ->components([
                TextInput::make('name')
                    ->label(__('academy.name'))
                    ->required(),
                Toggle::make('is_sub_academy')
                    ->required()->inline(false)->live()->label(__('academy.is_sub_academy')),
                Select::make('academy_id')
                    ->label(__('academy.parent_name'))
                    ->options(Academy::where('is_sub_academy', false)->get()->pluck('name', 'id'))
                    ->visible(fn(Get $get) => $get('is_sub_academy'))
                    ->required(fn(Get $get) => $get('is_sub_academy')),
            ]);
    }

    public static function table(Table $table): Table
    {
        return $table
            ->recordTitleAttribute('Academy')
            ->columns([
                TextColumn::make('name')
                    ->searchable()->label(__('academy.name')),
                IconColumn::make('is_sub_academy')
                    ->boolean()->label(__('academy.is_sub_academy')),
                TextColumn::make('parent.name')
                    ->sortable()
                    ->label(__('academy.parent_name')),
                TextColumn::make('created_at')
                    ->dateTime()
                    ->sortable()
                    ->toggleable(isToggledHiddenByDefault: true),
                TextColumn::make('updated_at')
                    ->dateTime()
                    ->sortable()
                    ->toggleable(isToggledHiddenByDefault: true),
            ])
            ->filters([
                //
            ])
            ->recordActions([
                EditAction::make(),
                DeleteAction::make(),
            ])
            ->toolbarActions([
                BulkActionGroup::make([
                    DeleteBulkAction::make(),
                ]),
            ]);
    }

    public static function getPages(): array
    {
        return [
            'index' => ManageAcademies::route('/'),
        ];
    }
}
