<?php

namespace App\Filament\Admin\Resources;

use App\Enums\IconEnum;
use App\Filament\Admin\Resources\TaxRateResource\Pages;
use App\Models\TaxRate;
use Filament\Forms\Components\Placeholder;
use Filament\Forms\Components\Section;
use Filament\Forms\Components\TextInput;
use Filament\Forms\Form;
use Filament\Resources\Resource;
use Filament\Support\Enums\FontWeight;
use Filament\Tables;
use Filament\Tables\Columns\TextColumn;
use Filament\Tables\Table;

class TaxRateResource extends Resource
{
    protected static ?string $model = TaxRate::class;

    protected static ?string $navigationIcon = IconEnum::TAX_RATE->value;

    protected static ?string $navigationGroup = 'Settings';

    protected static ?int $navigationSort = 11;

    public static function form(Form $form): Form
    {
        return $form
            ->schema([
                Section::make([
                    TextInput::make('name')
                        ->disabledOn('edit'),
                    TextInput::make('value')
                        ->label('Value in percentage')
                        ->numeric()
                        ->prefix('%'),
                ])
                    ->columns(1)
                    ->columnSpan(2),

                Section::make()
                    ->schema([
                        Placeholder::make('created_at')
                            ->label('Created')
                            ->content(fn (TaxRate $record): ?string => $record->created_at?->diffForHumans()),

                        Placeholder::make('updated_at')
                            ->label('Last modified')
                            ->content(fn (TaxRate $record): ?string => $record->updated_at?->diffForHumans()),
                    ])
                    ->columnSpan(1)
                    ->hidden(fn (?TaxRate $record) => $record === null)
                    ->extraAttributes(['class' => 'h-full']),
            ])->columns(3);
    }

    public static function table(Table $table): Table
    {
        return $table
            ->columns([
                TextColumn::make('name')
                    ->weight(FontWeight::Bold),
                TextColumn::make('value')
                    ->extraHeaderAttributes(['class' => 'w-full'])
                    ->suffix(' %'),
            ])
            ->filters([
                //
            ])
            ->actions([
                Tables\Actions\EditAction::make(),
            ])
            ->bulkActions([
                Tables\Actions\BulkActionGroup::make([
                    Tables\Actions\DeleteBulkAction::make(),
                ]),
            ]);
    }

    public static function getRelations(): array
    {
        return [
            //
        ];
    }

    public static function canAccess(): bool
    {
        return auth()->user()->is_admin ?? false;
    }

    public static function getPages(): array
    {
        return [
            'index' => Pages\ListTaxRates::route('/'),
            //            'create' => Pages\CreateTaxRate::route('/create'),
            //            'edit'   => Pages\EditTaxRate::route('/{record}/edit'),
        ];
    }
}
