<?php

namespace App\Filament\Resources;

use BackedEnum;
use UnitEnum;
use App\Filament\Resources\TicketResource\Pages\CreateTicket;
use App\Filament\Resources\TicketResource\Pages\EditTicket;
use App\Filament\Resources\TicketResource\Pages\ListTickets;
use App\Filament\Resources\TicketResource\RelationManagers\ResponsesRelationManager;
use App\Mail\TicketResponseNotification;
use App\Models\Ticket;
use App\Models\TicketResponse;
use Filament\Actions\Action;
use Filament\Actions\BulkAction;
use Filament\Actions\BulkActionGroup;
use Filament\Actions\DeleteAction;
use Filament\Actions\DeleteBulkAction;
use Filament\Actions\EditAction;
use Filament\Actions\ViewAction;
use Filament\Forms\Components\DatePicker;
use Filament\Forms\Components\Select;
use Filament\Forms\Components\Textarea;
use Filament\Forms\Components\TextInput;
use Filament\Notifications\Notification;
use Filament\Resources\Resource;
use Filament\Schemas\Schema;
use Filament\Tables\Columns\BadgeColumn;
use Filament\Tables\Columns\TextColumn;
use Filament\Tables\Filters\Filter;
use Filament\Tables\Filters\SelectFilter;
use Filament\Tables\Table;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Mail;
use Illuminate\Support\HtmlString;

class TicketResource extends Resource
{
    protected static ?string $model = Ticket::class;

    protected static string|BackedEnum|null $navigationIcon = 'heroicon-o-ticket';

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

    public static function form(Schema $schema): Schema
    {
        return $schema
            ->components([
                Select::make('user_id')
                    ->relationship('user', 'name')
                    ->searchable()
                    ->preload()
                    ->required(),

                Select::make('status')
                    ->label('Status')
                    ->options([
                        'open' => 'Open',
                        'pending' => 'In Progress',
                        'closed' => 'Closed',
                    ])
                    ->default('open')
                    ->required(),

                TextInput::make('subject')
                    ->label('Subject')
                    ->required()
                    ->maxLength(255)
                    ->columnSpanFull(),

                TextArea::make('message')
                    ->label('Message')
                    ->required()
                    ->rows(7)
                    ->maxLength(2000)
                    ->columnSpanFull(),
            ]);
    }

    public static function table(Table $table): Table
    {
        return $table
            ->defaultSort('created_at', 'desc')
            ->columns([
                TextColumn::make('ticket_id')
                    ->label('Ticket ID')
                    ->sortable(),
                TextColumn::make('user.name'),
                TextColumn::make('subject')
                    ->limit(50)
                    ->label('Subject')
                    ->searchable(),
                BadgeColumn::make('status')
                    ->colors([
                        'success' => fn ($state): bool => $state === 'open',
                        'warning' => fn ($state): bool => $state === 'pending',
                        'danger' => fn ($state): bool => $state === 'closed',
                    ])
                    ->sortable(),
                TextColumn::make('created_at')
                    ->label('Created At')
                    ->dateTime('F d, Y • h:i A')->sortable(),
                TextColumn::make('updated_at')
                    ->label('Last Response')
                    ->sortable()
                    ->formatStateUsing(fn ($state) => $state?->diffForHumans()),
            ])
            ->searchable()
            ->filters([
                SelectFilter::make('status')
                    ->label('Status')
                    ->options([
                        'open' => 'Open',
                        'pending' => 'In Progress',
                        'closed' => 'Closed',
                    ])
                    ->attribute('status'),
                Filter::make('created_at')
                    ->schema([
                        DatePicker::make('created_from')->label('Created From'),
                        DatePicker::make('created_until')->label('Created Until'),
                    ])
                    ->query(fn($query, array $data) => $query
                        ->when($data['created_from'], fn ($query, $date) => $query->whereDate('created_at', '>=', $date))
                        ->when($data['created_until'], fn ($query, $date) => $query->whereDate('created_at', '<=', $date))),
            ])
//            ->actions([
//                Tables\Actions\EditAction::make(),
//            ])
            ->recordActions([
                ViewAction::make(),
                EditAction::make(),
                DeleteAction::make(),
                Action::make('view')
                    ->label('View & Respond')
                    ->icon('heroicon-o-eye')
                    ->schema(fn(Ticket $ticket): array => [
                        TextArea::make('response')
                            ->label('Your Response')
                            ->required()
                            ->rows(7)
                            ->placeholder('Type your response here...'),

                        Select::make('status')
                            ->label('Ticket Status')
                            ->options([
                                'open' => 'Open',
                                'pending' => 'In Progress',
                                'closed' => 'Closed',
                            ])
                            ->default($ticket->status === 'open' ? 'pending' : $ticket->status)
                            ->required(),
                    ])
                    ->modalContent(function (Ticket $ticket): HtmlString {
                        $html = '<div class="space-y-3 mb-3 text-sm">';

                        // Ticket Subject & Message
                        $html .= '<div class="bg-gray-100 dark:bg-gray-900 border p-2 rounded-md">';
                        $html .= '<h6 class="xs font-semibold text-gray-800 dark:text-gray-100">Subject: '.e($ticket->subject).'</h6>';
                        $html .= '<p class="mt-1 text-gray-700 dark:text-gray-300">Message: '.nl2br(e($ticket->message)).'</p>';
                        $html .= '</div>';

                        // Responses Section
                        $html .= '<h6 class="text-xs font-bold text-gray-700 dark:text-gray-200 mt-2">Previous Responses</h6>';

                        foreach ($ticket->responses as $response) {
                            $html .= '<div class="rounded-md border p-2 bg-gray-50 dark:bg-gray-800">';
                            $html .= '<div class="text-xs text-gray-600 dark:text-gray-300">';
                            $html .= '<strong>'.e($response->user->name).'</strong>';
                            $html .= '<span class="ml-2 text-[11px] text-gray-500">'.e($response->created_at->diffForHumans()).'</span>';
                            $html .= '</div>';
                            $html .= '<p class="mt-1 text-gray-800 dark:text-gray-100">'.nl2br(e($response->response)).'</p>';
                            $html .= '</div>';
                        }

                        if ($ticket->responses->isEmpty()) {
                            $html .= '<p class="text-xs text-gray-500">No responses yet.</p>';
                        }

                        $html .= '</div>';

                        return new HtmlString($html);
                    })

                    ->action(function (array $data, Ticket $ticket): void {
                        TicketResponse::query()->create([
                            'ticket_id' => $ticket->id,
                            'user_id' => auth()->id(),
                            'response' => $data['response'],
                        ]);

                        // Update ticket status and last response time
                        $ticket->update([
                            'status' => $data['status'],
                            'last_response_at' => now(),
                        ]);

                        // Send success notification
                        Notification::make()
                            ->title('Response sent successfully!')
                            ->success()
                            ->send();
                    })
                    ->modalHeading('View & Respond to Ticket')
                    ->modalSubmitActionLabel('Send Reply'),
            ])
            ->recordActions([
                Action::make('close')
                    ->label('Close Ticket')
                    ->icon('heroicon-o-x-circle')
                    ->color('danger')
                    ->requiresConfirmation()
                    ->visible(fn (Ticket $ticket): bool => $ticket->status !== 'closed')
                    ->action(function (Ticket $ticket): void {
                        $ticket->update(['status' => 'closed']);
                    }),
                Action::make('reopen')
                    ->label('Reopen Ticket')
                    ->icon('heroicon-o-arrow-path')
                    ->color('success')
                    ->visible(fn (Ticket $ticket): bool => $ticket->status === 'closed')
                    ->action(function (Ticket $ticket): void {
                        $ticket->update(['status' => 'open']);
                    }),
            ])
            ->toolbarActions([
                BulkActionGroup::make([
                    DeleteBulkAction::make(),
                    BulkAction::make('notify_users')
                        ->label('Send Email Notification')
                        ->color('success')
                        ->icon('heroicon-o-envelope')
                        ->requiresConfirmation()
                        ->deselectRecordsAfterCompletion()
                        ->action(function (Collection $records): void {
                            foreach ($records as $ticket) {
                                $responses = $ticket->responses()
                                    ->with('user')
                                    ->orderBy('created_at', 'desc')
                                    ->get();

                                if ($ticket->user && $ticket->user->email) {
                                    Mail::to($ticket->user->email)
                                        ->send(new TicketResponseNotification($ticket, $responses));
                                }
                            }

                            Notification::make()
                                ->title('Email notifications sent successfully!')
                                ->success()
                                ->send();
                        }),
                ]),
            ]);
    }

    public static function getRelations(): array
    {
        return [
            ResponsesRelationManager::class,
        ];
    }

    public static function getPages(): array
    {
        return [
            'index' => ListTickets::route('/'),
            'create' => CreateTicket::route('/create'),
            'edit' => EditTicket::route('/{record}/edit'),
        ];
    }
}
