<?php

namespace App\Filament\Resources\Manage\Contacts\Tables;

use App\Filament\Actions\Cheerful\DeleteAction;
use App\Filament\Actions\DefaultBulkActions;
use App\Filament\Columns\RowIndexColumn;
use App\Filament\Resources\Manage\Contacts\Actions\ReplyAction;
use App\Filament\Support\CheerfulNotification;
use App\Models\Contact;
use Filament\Actions\BulkActionGroup;
use Filament\Actions\ViewAction;
use Filament\Support\Icons\Heroicon;
use Filament\Tables\Columns\IconColumn;
use Filament\Tables\Columns\TextColumn;
use Filament\Tables\Filters\TernaryFilter;
use Filament\Tables\Table;

class ContactTable
{
    public static function configure(Table $table): Table
    {
        return $table
            ->columns([
                ...RowIndexColumn::make(),

                TextColumn::make('name')
                    ->label('Nama Pengirim')
                    ->searchable(),

                TextColumn::make('email')
                    ->label('Email')
                    ->searchable(),

                TextColumn::make('subject')
                    ->label('Subyek')
                    ->limit(30),

                IconColumn::make('replied_at')
                    ->label('Dibalas')
                    ->boolean()
                    ->getStateUsing(fn (Contact $record): bool => $record->replied_at !== null),

                TextColumn::make('repliedBy.name')
                    ->label('Dibalas Oleh')
                    ->badge()
                    ->placeholder('Belum dibalas')
                    ->sortable(),

                TextColumn::make('created_at')
                    ->label('Diterima pada')
                    ->date('l, d F Y H:i')
                    ->sortable(),
            ])
            ->filters([
                TernaryFilter::make('replied_at')
                    ->label('Status Balasan')
                    ->placeholder('Semua Pesan')
                    ->trueLabel('Sudah Dibalas')
                    ->falseLabel('Belum Dibalas')
                    ->queries(
                        true: fn ($query) => $query->whereNotNull('replied_at'),
                        false: fn ($query) => $query->whereNull('replied_at'),
                    )
                    ->native(false),
            ])
            ->recordActions([
                ViewAction::make()
                    ->label('Lihat'),

                ReplyAction::make(),

                DeleteAction::make()
                    ->label('Hapus'),
            ])
            ->toolbarActions([
                BulkActionGroup::make([
                    ...DefaultBulkActions::make('Kontak'),
                ]),
            ])
            ->emptyStateIcon(Heroicon::OutlinedEnvelope)
            ->emptyStateHeading(fn () => CheerfulNotification::getByKey('contact.empty_state_heading'))
            ->emptyStateDescription(fn () => CheerfulNotification::getByKey('contact.empty_state'))
            ->defaultSort('created_at', 'desc')
            ->deferFilters(false)
            ->paginated([25, 50, 100, 'all'])
            ->deferLoading();
    }
}
