<?php

namespace App\Filament\Clusters\Settings\CardTypes;

use AbdulmajeedJamaan\FilamentTranslatableTabs\TranslatableTabs;
use App\Filament\Clusters\Settings\CardTypes\Pages\ManageCardTypes;
use App\Filament\Clusters\Settings\SettingsCluster;
use App\Modules\CardOrder\Models\CardType;
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\Components\Section;
use Filament\Schemas\Schema;
use Filament\Support\Icons\Heroicon;
use Filament\Tables\Columns\IconColumn;
use Filament\Tables\Columns\TextColumn;
use Filament\Tables\Table;

class CardTypeResource extends Resource
{
    protected static ?string $model = CardType::class;

    protected static ?string $cluster = SettingsCluster::class;

    protected static ?int $navigationSort = 2;

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

    protected static ?string $recordTitleAttribute = 'name';

    public static function getNavigationGroup(): ?string
    {
        return __('Cards');
    }

    public static function getModelLabel(): string
    {
        return __('Card type');
    }

    public static function getPluralModelLabel(): string
    {
        return __('Card types');
    }

    public static function form(Schema $schema): Schema
    {
        return $schema
            ->components([
                TranslatableTabs::make('name')
                    ->schema([
                        TextInput::make('name')
                            ->label(__('Card state'))
                            ->columnSpan(1)
                            ->required(),
                    ]),

                Section::make(__('General'))
                    ->columns(6)
                    ->schema([
                        TextInput::make('price')
                            ->label(__('Price'))
                            ->columnSpan(4)
                            ->required(),

                        Toggle::make('active')
                            ->label(__('Active'))
                            ->default(true)
                            ->inline(false)
                            ->columnSpan(2)
                            ->required(),

                        TextInput::make('notes')
                            ->label(__('Notes'))
                            ->columnSpanFull()
                            ->default(null),
                    ]),
            ]);
    }

    public static function table(Table $table): Table
    {
        return $table
            ->columns([
                TextColumn::make('name')
                    ->searchable(),

                TextColumn::make('price')
                    ->searchable(),

                TextColumn::make('notes'),

                IconColumn::make('active')
                    ->boolean(),

                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' => ManageCardTypes::route('/'),
        ];
    }
}
