<?php

namespace App\Filament\Resources;

use App\Filament\Resources\LoanProductResource\Pages;
use App\Models\LoanProduct;
use Filament\Forms;
use Filament\Forms\Form;
use Filament\Resources\Resource;
use Filament\Tables;
use Filament\Tables\Table;

class LoanProductResource extends Resource
{
    protected static ?string $model = LoanProduct::class;

    protected static ?string $navigationIcon = 'heroicon-o-banknotes';

    protected static ?string $navigationGroup = 'Loan Management';

    protected static ?int $navigationSort = 1;

    protected static ?string $modelLabel = 'Loan Product';

    public static function form(Form $form): Form
    {
        return $form
            ->schema([
                Forms\Components\Section::make('Product Details')
                    ->schema([
                        Forms\Components\TextInput::make('name')
                            ->required()
                            ->maxLength(255)
                            ->columnSpanFull(),
                        Forms\Components\Textarea::make('description')
                            ->rows(3)
                            ->columnSpanFull(),
                    ]),

                Forms\Components\Section::make('Amount Range')
                    ->schema([
                        Forms\Components\TextInput::make('min_amount')
                            ->required()
                            ->numeric()
                            ->prefix('₦')
                            ->minValue(1000),
                        Forms\Components\TextInput::make('max_amount')
                            ->required()
                            ->numeric()
                            ->prefix('₦')
                            ->minValue(1000),
                    ])->columns(2),

                Forms\Components\Section::make('Interest & Fees')
                    ->schema([
                        Forms\Components\TextInput::make('interest_rate')
                            ->required()
                            ->numeric()
                            ->suffix('%')
                            ->helperText('Interest rate in percentage'),
                        Forms\Components\Select::make('interest_type')
                            ->options([
                                'flat' => 'Flat Rate',
                                'reducing' => 'Reducing Balance',
                            ])
                            ->required()
                            ->default('flat'),
                        Forms\Components\TextInput::make('processing_fee')
                            ->numeric()
                            ->default(0),
                        Forms\Components\Select::make('processing_fee_type')
                            ->options([
                                'percentage' => 'Percentage',
                                'fixed' => 'Fixed Amount',
                            ])
                            ->default('percentage'),
                        Forms\Components\TextInput::make('late_fee')
                            ->numeric()
                            ->default(0),
                        Forms\Components\Select::make('late_fee_type')
                            ->options([
                                'percentage' => 'Percentage',
                                'fixed' => 'Fixed Amount',
                            ])
                            ->default('percentage'),
                    ])->columns(2),

                Forms\Components\Section::make('Tenure')
                    ->schema([
                        Forms\Components\TextInput::make('min_tenure_days')
                            ->required()
                            ->numeric()
                            ->suffix('days')
                            ->minValue(1),
                        Forms\Components\TextInput::make('max_tenure_days')
                            ->required()
                            ->numeric()
                            ->suffix('days')
                            ->minValue(1),
                    ])->columns(2),

                Forms\Components\Toggle::make('is_active')
                    ->label('Active')
                    ->default(true),
            ]);
    }

    public static function table(Table $table): Table
    {
        return $table
            ->columns([
                Tables\Columns\TextColumn::make('name')->searchable()->sortable(),
                Tables\Columns\TextColumn::make('min_amount')->money('NGN'),
                Tables\Columns\TextColumn::make('max_amount')->money('NGN'),
                Tables\Columns\TextColumn::make('interest_rate')->suffix('%'),
                Tables\Columns\TextColumn::make('interest_type')->badge(),
                Tables\Columns\TextColumn::make('min_tenure_days')->suffix(' days'),
                Tables\Columns\TextColumn::make('max_tenure_days')->suffix(' days'),
                Tables\Columns\IconColumn::make('is_active')->boolean(),
                Tables\Columns\TextColumn::make('created_at')->dateTime()->sortable()->toggleable(isToggledHiddenByDefault: true),
            ])
            ->filters([
                Tables\Filters\TernaryFilter::make('is_active')->label('Active'),
            ])
            ->actions([
                Tables\Actions\EditAction::make(),
                Tables\Actions\DeleteAction::make(),
            ])
            ->bulkActions([
                Tables\Actions\BulkActionGroup::make([
                    Tables\Actions\DeleteBulkAction::make(),
                ]),
            ]);
    }

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