<?php

namespace App\Filament\Resources\Annonces\Tables;

use Filament\Actions\BulkActionGroup;
use Filament\Actions\DeleteBulkAction;
use Filament\Actions\EditAction;
use Filament\Tables\Columns\TextColumn;
use Filament\Tables\Table;
use App\Enums\StatutAnnonce;
use App\Enums\TypeAnnonce;
use App\Models\Annonce;
use Filament\Actions\Action;
use Filament\Forms\Components\TextInput;
use Filament\Tables\Filters\SelectFilter;

class AnnoncesTable
{
    public static function configure(Table $table): Table
    {
        return $table
            ->columns([
                    TextColumn::make('titre')
    ->searchable()
    ->limit(40),

TextColumn::make('type')
    ->badge(),

TextColumn::make('statut')
    ->badge(),

TextColumn::make('user.name')
    ->label('Auteur')
    ->searchable(),

TextColumn::make('frais')
    ->label('Frais')
    ->money('HTG'),

TextColumn::make('publiee_le')
    ->label('Publiée le')
    ->dateTime('d/m/Y')
    ->placeholder('—'),
            ])

            ->filters([
                SelectFilter::make('type')
    ->options(TypeAnnonce::class),

SelectFilter::make('statut')
    ->options(StatutAnnonce::class),
                //
            ])
            ->recordActions([
                EditAction::make(),

                Action::make('confirmerPaiement')
    ->label('Confirmer le paiement')
    ->icon('heroicon-o-banknotes')
    ->color('warning')
    ->visible(fn (Annonce $record) => $record->statut === StatutAnnonce::EnAttentePaiement)
    ->schema([
        TextInput::make('paiement_reference')
            ->label('Référence NatCash / MonCash')
            ->required(),
    ])
    ->action(function (array $data, Annonce $record) {
        $record->update([
            'paiement_reference' => $data['paiement_reference'],
            'paiement_confirme_le' => now(),
            'statut' => StatutAnnonce::EnValidation,
        ]);
    }),

Action::make('publier')
    ->label('Valider & publier')
    ->icon('heroicon-o-check-circle')
    ->color('success')
    ->requiresConfirmation()
    ->visible(fn (Annonce $record) => $record->statut === StatutAnnonce::EnValidation)
    ->action(function (Annonce $record) {
        $record->update([
            'statut' => StatutAnnonce::Publiee,
            'publiee_le' => now(),
        ]);
    }),

Action::make('rejeter')
    ->label('Rejeter')
    ->icon('heroicon-o-x-circle')
    ->color('danger')
    ->requiresConfirmation()
    ->visible(fn (Annonce $record) => in_array($record->statut, [
        StatutAnnonce::EnAttentePaiement,
        StatutAnnonce::EnValidation,
    ]))
    ->action(fn (Annonce $record) => $record->update([
        'statut' => StatutAnnonce::Rejetee,
    ])),

            ])
            ->toolbarActions([
                BulkActionGroup::make([
                    DeleteBulkAction::make(),
                ]),
            ]);
    }
}
