<?php

namespace App\Filament\Resources\Customers\Tables;

use Filament\Tables\Columns\TextColumn;
use Filament\Actions\EditAction;
use Filament\Actions\Action;
use Filament\Tables\Table;
use Filament\Notifications\Notification;
use Filament\Forms\Components\TextInput;
use Filament\Forms\Components\RichEditor;

class CustomersTable
{
    public static function configure(Table $table): Table
    {
        return $table
            ->columns([
                TextColumn::make('name')
                    ->label('Name')
                    ->searchable()
                    ->sortable(),
                TextColumn::make('email')
                    ->label('Email address')
                    ->searchable()
                    ->sortable()
                    ->toggleable(),
                TextColumn::make('addresses.country')
                    ->label('Address Type')
                    ->default('-')
                    ->toggleable(isToggledHiddenByDefault: true),
                TextColumn::make('phone')
                    ->label('Phone')
                    ->searchable()
                    ->sortable()
                    ->default('-')
                    ->toggleable(),
            ])
            ->actions([
                Action::make('send_email')
                    ->label('Send email')
                    ->icon('heroicon-m-envelope')
                    ->color('primary')
                    ->modalWidth('2xl')
                    ->form([
                        TextInput::make('to')
                            ->label('To')
                            ->disabled()
                            ->dehydrated(false)
                            ->default(fn ($record) => $record?->email),
                        TextInput::make('subject')
                            ->label('Subject')
                            ->required(),
                        RichEditor::make('body')
                            ->label('Body')
                            ->required(),
                    ])
                    ->action(function ($record, array $data) {
                        Notification::make()
                            ->title('Email Sent')
                            ->body('Email sent to ' . $record->email . ' successfully!')
                            ->success()
                            ->send();
                    }),
                EditAction::make()
                    ->label('Edit'),
            ]);
    }
}
