<?php

namespace App\Filament\Resources\Manage\Contacts;

use App\Filament\Actions\Cheerful\DeleteAction;
use App\Filament\Actions\DefaultBulkActions;
use App\Filament\Resources\Manage\Contacts\Actions\ReplyAction;
use App\Filament\Resources\Manage\Contacts\Pages\ManageContacts;
use App\Models\Contact;
use BackedEnum;
use Filament\Actions\BulkActionGroup;
use Filament\Actions\ViewAction;
use Filament\Infolists\Components\TextEntry;
use Filament\Resources\Resource;
use Filament\Schemas\Components\Grid;
use Filament\Schemas\Components\Section;
use Filament\Schemas\Schema;
use Filament\Support\Icons\Heroicon;
use Filament\Tables\Columns\IconColumn;
use Filament\Tables\Columns\TextColumn;
use Filament\Tables\Filters\TernaryFilter;
use Filament\Tables\Table;
use UnitEnum;

class ContactResource extends Resource
{
    protected static ?string $model = Contact::class;

    protected static ?string $modelLabel = 'Kontak';

    protected static string|UnitEnum|null $navigationGroup = 'Kelola';

    protected static string|BackedEnum|null $navigationIcon = Heroicon::Envelope;

    protected static ?string $navigationLabel = 'Kontak';

    protected static ?int $navigationSort = 9;

    protected static ?string $recordTitleAttribute = 'subject';

    protected static ?string $slug = 'manage/contacts';

    public static function getNavigationBadge(): ?string
    {
        return static::getModel()::where('replied_at', null)->count();
    }

    public static function infolist(Schema $schema): Schema
    {
        return $schema
            ->schema([
                Section::make('Informasi Pengirim')
                    ->schema([
                        Grid::make(4)
                            ->schema([
                                TextEntry::make('name')
                                    ->label('Nama'),

                                TextEntry::make('email')
                                    ->label('Email'),

                                TextEntry::make('subject')
                                    ->label('Subyek'),

                                TextEntry::make('created_at')
                                    ->label('Diterima pada')
                                    ->date('l, d F Y H:i'),
                            ]),
                    ])
                    ->columnSpanFull(),

                Section::make('Pesan')
                    ->schema([
                        TextEntry::make('message')
                            ->label('Isi Pesan')
                            ->columnSpanFull(),
                    ])
                    ->columnSpanFull(),

                Section::make('Balasan')
                    ->schema([
                        TextEntry::make('repliedBy.name')
                            ->label('Dibalas Oleh')
                            ->placeholder('Belum dibalas'),

                        TextEntry::make('replied_at')
                            ->label('Dibalas pada')
                            ->date('l, d F Y H:i')
                            ->placeholder('Belum dibalas'),

                        TextEntry::make('reply_message')
                            ->label('Pesan Balasan')
                            ->placeholder('Belum dibalas'),
                    ])
                    ->columnSpanFull()
                    ->visible(fn (Contact $record): bool => $record->replied_at !== null),
            ])
            ->columns(2);
    }

    public static function table(Table $table): Table
    {
        return $table
            ->columns([
                TextColumn::make('name')
                    ->label('Nama')
                    ->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('reply'),

                DeleteAction::make()
                    ->label('Hapus'),
            ])
            ->toolbarActions([
                BulkActionGroup::make([
                    ...DefaultBulkActions::make('Kontak'),
                ]),
            ])
            ->emptyStateIcon(Heroicon::Envelope)
            ->emptyStateDescription('Belum ada kontak atau pesan masuk.')
            ->defaultSort('created_at', 'desc')
            ->deferFilters(false);
    }

    public static function getPages(): array
    {
        return [
            'index' => ManageContacts::route('/'),
        ];
    }
}
