<?php

declare(strict_types=1);

namespace App\Filament\Resources\Partners\Tables;

use App\Enums\OwnershipModel;
use App\Enums\PartnerStatus;
use App\Models\Partner;
use Filament\Actions\DeleteAction;
use Filament\Actions\EditAction;
use Filament\Actions\ViewAction;
use Filament\Tables\Columns\TextColumn;
use Filament\Tables\Filters\SelectFilter;
use Filament\Tables\Table;

class PartnersTable
{
    public static function configure(Table $table): Table
    {
        return $table
            ->columns([
                TextColumn::make('name')
                    ->label('Nombre')
                    ->searchable()
                    ->sortable(),

                TextColumn::make('document_number')
                    ->label('Documento')
                    ->searchable()
                    ->toggleable(),

                TextColumn::make('net_capital')
                    ->label('Capital neto')
                    ->getStateUsing(fn (Partner $record) => $record->getNetCapital()->value())
                    ->money('COP', locale: 'es_CO')
                    ->sortable(false)
                    ->color('primary'),

                TextColumn::make('total_profits')
                    ->label('Utilidades')
                    ->getStateUsing(fn (Partner $record) => $record->getTotalProfitsReceived()->value())
                    ->money('COP', locale: 'es_CO')
                    ->sortable(false)
                    ->color('success')
                    ->toggleable(),

                TextColumn::make('ownership_model')
                    ->label('Modelo')
                    ->badge()
                    ->formatStateUsing(fn ($state) => $state instanceof OwnershipModel
                        ? $state->getLabel()
                        : (is_string($state) ? (OwnershipModel::tryFrom($state)?->getLabel() ?? $state) : '-')
                    )
                    ->color(fn ($state) => $state instanceof OwnershipModel
                        ? $state->getColor()
                        : 'gray'
                    )
                    ->toggleable(),

                TextColumn::make('status')
                    ->label('Estado')
                    ->badge()
                    ->formatStateUsing(fn ($state) => $state instanceof PartnerStatus
                        ? $state->getLabel()
                        : (is_string($state) ? (PartnerStatus::tryFrom($state)?->getLabel() ?? $state) : '-')
                    )
                    ->color(fn ($state) => $state instanceof PartnerStatus
                        ? $state->getColor()
                        : 'gray'
                    ),

                TextColumn::make('joined_at')
                    ->label('Ingreso')
                    ->date('d/m/Y')
                    ->sortable()
                    ->toggleable(),

                TextColumn::make('contributions_count')
                    ->label('Aportes')
                    ->counts('contributions')
                    ->sortable()
                    ->toggleable(isToggledHiddenByDefault: true),
            ])
            ->filters([
                SelectFilter::make('status')
                    ->label('Estado')
                    ->options([
                        'active' => 'Activo',
                        'inactive' => 'Inactivo',
                        'suspended' => 'Suspendido',
                    ]),

                SelectFilter::make('ownership_model')
                    ->label('Modelo')
                    ->options([
                        'fixed' => 'Porcentaje fijo',
                        'dynamic' => 'Basado en capital',
                    ]),
            ])
            ->actions([
                ViewAction::make(),
                EditAction::make(),
                DeleteAction::make()
                    ->visible(fn (Partner $record) => $record->contributions()->count() === 0),
            ])
            ->defaultSort('name')
            ->emptyStateHeading('Sin socios registrados')
            ->emptyStateDescription('Registra el primer socio o inversionista.')
            ->emptyStateIcon('heroicon-o-user-group');
    }
}
