<?php

namespace App\Filament\Resources\InvestmentPartners;

use App\Filament\Resources\InvestmentPartners\Pages\CreateInvestmentPartner;
use App\Filament\Resources\InvestmentPartners\Pages\EditInvestmentPartner;
use App\Filament\Resources\InvestmentPartners\Pages\ListInvestmentPartners;
use App\Models\InvestmentPartner;
use BackedEnum;
use Filament\Forms\Components\KeyValue;
use Filament\Forms\Components\Repeater;
use Filament\Forms\Components\Textarea;
use Filament\Forms\Components\TextInput;
use Filament\Forms\Components\Toggle;
use Filament\Resources\Resource;
use Filament\Schemas\Schema;
use Filament\Tables\Columns\IconColumn;
use Filament\Tables\Columns\TextColumn;
use Filament\Tables\Table;

class InvestmentPartnerResource extends Resource
{
    protected static ?string $model = InvestmentPartner::class;
    protected static string|BackedEnum|null $navigationIcon = 'heroicon-o-building-library';

    protected static string|\UnitEnum|null $navigationGroup = 'Money';

    protected static ?string $navigationLabel = 'Investment options';

    protected static ?int $navigationSort = 2;

    public static function form(Schema $schema): Schema
    {
        return $schema->components([
            TextInput::make('name')->required(),
            TextInput::make('slug')->required()->unique(ignoreRecord: true),
            Textarea::make('description')->columnSpanFull(),
            TextInput::make('minimum_amount')->numeric()->prefix('KES'),
            TextInput::make('currency')->default('KES')->maxLength(3),
            KeyValue::make('requirements')
                ->keyLabel('Item')
                ->valueLabel('Requirement')
                ->reorderable()
                ->columnSpanFull(),
            TextInput::make('application_url')
                ->url()
                ->required()
                ->helperText('The official agency link. Used when no account types are defined below.')
                ->columnSpanFull(),
            TextInput::make('website_url')
                ->url()
                ->label('Provider info page (optional)')
                ->helperText('A "learn about this fund" link on the provider\'s own website.')
                ->columnSpanFull(),
            Repeater::make('links')
                ->label('Account types (optional)')
                ->helperText('For providers with several sign-up links, e.g. CIC individual/joint/junior/student. Leave empty for a single link.')
                ->schema([
                    TextInput::make('key')->required()->helperText('Short id, e.g. individual'),
                    TextInput::make('label')->required(),
                    TextInput::make('hint')->helperText('Tiny caption, e.g. "For your child"'),
                    TextInput::make('url')->url()->required()->columnSpanFull(),
                ])
                ->columns(3)
                ->reorderable()
                ->default([])
                ->columnSpanFull(),
            Textarea::make('disclosure')->columnSpanFull(),
            TextInput::make('sort')->numeric()->default(0),
            Toggle::make('is_active')->helperText('Publish only after links and wording have been compliance-checked.'),
        ]);
    }

    public static function table(Table $table): Table
    {
        return $table->columns([
            TextColumn::make('name')->searchable()->weight('bold'),
            TextColumn::make('minimum_amount')->money(fn ($record) => $record->currency)->sortable(),
            TextColumn::make('applications_count')->counts('applications')->label('Applications')->badge(),
            IconColumn::make('is_active')->boolean()->label('Published'),
        ])->recordActions([\Filament\Actions\EditAction::make()]);
    }

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