<?php

namespace App\Filament\Clusters\Cards\Cards;

use App\Filament\Clusters\Cards\Cards\Pages\ManageCards;
use App\Filament\Clusters\Cards\CardsCluster;
use App\Modules\AppHelpers\Repositories\DateHelper;
use App\Modules\Card\Filament\Actions\CheckCardBalanceAction;
use App\Modules\Card\Filament\Actions\DownloadCardRequisteAction;
use App\Modules\Card\Filament\Actions\DownloadCardTransactionAction;
use App\Modules\Card\Models\Card;
use BackedEnum;
use Filament\Actions\BulkActionGroup;
use Filament\Actions\DeleteAction;
use Filament\Actions\DeleteBulkAction;
use Filament\Actions\EditAction;
use Filament\Forms\Components\Hidden;
use Filament\Forms\Components\Select;
use Filament\Forms\Components\TextInput;
use Filament\Resources\Resource;
use Filament\Schemas\Schema;
use Filament\Support\Icons\Heroicon;
use Filament\Tables\Columns\TextColumn;
use Filament\Tables\Table;
use Illuminate\Database\Eloquent\Builder;

class CardResource extends Resource
{
    protected static ?int $navigationSort = 1;

    protected static ?string $model = Card::class;

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

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

    public static function getNavigationLabel(): string
    {
        return __('My cards');
    }

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

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

    public static function form(Schema $schema): Schema
    {
        return $schema
            ->columns(3)
            ->components([
                Hidden::make('user_id')
                    ->default(fn () => user()->id),

                TextInput::make('number')
                    ->label(__('Card number'))
                    ->mask('9999 9999 9999 9999')
                    ->dehydrateStateUsing(fn ($state) => str_replace(' ', '', $state))
                    ->columnSpan(1)
                    ->required(),

                Select::make('month')
                    ->label(__('Card month'))
                    ->options(DateHelper::staticNumberMonths())
                    ->native(false)
                    ->columnSpan(1)
                    ->required(),

                Select::make('year')
                    ->label(__('Card year'))
                    ->options(DateHelper::staticNumberYears())
                    ->native(false)
                    ->columnSpan(1)
                    ->required(),

                TextInput::make('name')
                    ->label(__('Card name'))
                    ->maxLength(255)
                    ->columnSpanFull()
                    ->required(),
            ]);
    }

    public static function table(Table $table): Table
    {
        return $table
            ->modifyQueryUsing(function (Builder $query) {
                if (user()->isAdmin()) {
                    return;
                }

                $query->where('user_id', user()->id);
            })
            ->columns([
                TextColumn::make('number')
                    ->label(__('Card number'))
                    ->formatStateUsing(fn ($state) => trim(chunk_split($state, 4, ' ')))
                    ->searchable(),

                TextColumn::make('name')
                    ->label(__('Card name'))
                    ->searchable(),

                TextColumn::make('month')
                    ->label(__('Card month'))
                    ->searchable(),

                TextColumn::make('year')
                    ->label(__('Card year'))
                    ->searchable(),

                TextColumn::make('created_at')
                    ->dateTime()
                    ->sortable()
                    ->toggleable(isToggledHiddenByDefault: true),
                TextColumn::make('updated_at')
                    ->dateTime()
                    ->sortable()
                    ->toggleable(isToggledHiddenByDefault: true),
            ])
            ->filters([
                //
            ])
            ->recordActions([
                CheckCardBalanceAction::make(),
                DownloadCardTransactionAction::make(),
                DownloadCardRequisteAction::make(),

                EditAction::make()
                    ->label(''),
                DeleteAction::make()
                    ->label(''),
            ])
            ->toolbarActions([
                BulkActionGroup::make([
                    DeleteBulkAction::make(),
                ]),
            ]);
    }

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