<?php

namespace App\Filament\Resources\DeductionTypes;

use App\Filament\Resources\DeductionTypes\Pages\CreateDeductionType;
use App\Filament\Resources\DeductionTypes\Pages\EditDeductionType;
use App\Filament\Resources\DeductionTypes\Pages\ListDeductionTypes;
use App\Models\DeductionType;
use BackedEnum;
use Filament\Resources\Resource;
use Filament\Schemas\Schema;
use Filament\Support\Icons\Heroicon;
use Filament\Tables\Table;
use Filament\Forms\Components\TextInput;
use Filament\Forms\Components\Select;
use Filament\Forms\Components\Toggle;
use Filament\Schemas\Components\Section;
use Filament\Tables\Columns\TextColumn;
use Filament\Tables\Columns\IconColumn;
use Filament\Actions\EditAction;
use Filament\Actions\DeleteAction;
use Filament\Actions\BulkActionGroup;
use Filament\Actions\DeleteBulkAction;

class DeductionTypeResource extends Resource
{
    protected static ?string $model = DeductionType::class;

    protected static string|BackedEnum|null $navigationIcon = Heroicon::OutlinedCalculator;
    protected static ?int $navigationSort = 2;
    protected static ?string $navigationLabel = 'أنواع الخصومات';

    public static function getNavigationGroup(): string|null
    {
        return 'العقود والتعاقدات';
    }

    public static function getNavigationBadge(): ?string
    {
        return static::getModel()::count();
    }

    public static function form(Schema $schema): Schema
    {
        return $schema
            ->components([
                Section::make('معلومات نوع الخصم')
                    ->columnSpanFull()
                    ->schema([
                        TextInput::make('name')
                            ->label('اسم نوع الخصم')
                            ->required()
                            ->maxLength(255)
                            ->placeholder('مثال: دفعة مقدمة، ضمان أعمال، سولار'),

                        Select::make('calc_method')
                            ->label('طريقة الحساب')
                            ->options([
                                'percent' => 'نسبة مئوية %',
                                'fixed' => 'قيمة ثابتة',
                            ])
                            ->required()
                            ->default('percent')
                            ->preload(),

                        TextInput::make('default_value')
                            ->label('القيمة الافتراضية')
                            ->numeric()
                            ->default(0)
                            ->minValue(0),

                        TextInput::make('sequence')
                            ->label('ترتيب العرض')
                            ->numeric()
                            ->default(1)
                            ->minValue(1),

                        Toggle::make('is_active')
                            ->label('نشط')
                            ->default(true),
                    ])->columns(2),
            ]);
    }

    public static function table(Table $table): Table
    {
        return $table
            ->columns([
                TextColumn::make('name')
                    ->label('اسم نوع الخصم')
                    ->searchable()
                    ->sortable(),

                TextColumn::make('calc_method')
                    ->label('طريقة الحساب')
                    ->formatStateUsing(fn (string $state): string => match ($state) {
                        'percent' => 'نسبة مئوية',
                        'fixed' => 'قيمة ثابتة',
                        default => $state
                    }),

                TextColumn::make('default_value')
                    ->label('القيمة الافتراضية')
                    ->formatStateUsing(fn ($record) => $record->calc_method === 'percent' 
                        ? $record->default_value . '%' 
                        : number_format($record->default_value, 2)),

                TextColumn::make('sequence')
                    ->label('الترتيب')
                    ->sortable(),

                IconColumn::make('is_active')
                    ->label('نشط')
                    ->boolean(),

                TextColumn::make('created_at')
                    ->label('تاريخ الإنشاء')
                    ->dateTime('d/m/Y')
                    ->toggleable(isToggledHiddenByDefault: true),
            ])
            ->defaultSort('sequence')
            ->actions([
                EditAction::make(),
                DeleteAction::make(),
            ])
            ->bulkActions([
                BulkActionGroup::make([
                    DeleteBulkAction::make(),
                ]),
            ]);
    }

    public static function getPages(): array
    {
        return [
            'index' => ListDeductionTypes::route('/'),
            'create' => CreateDeductionType::route('/create'),
            'edit' => EditDeductionType::route('/{record}/edit'),
        ];
    }
}
