<?php

namespace App\Filament\Resources\Applications;

use App\Filament\Resources\Applications\Pages\ListApplications;
use App\Filament\Resources\Applications\Pages\ViewApplication;
use App\Models\Application;
use BackedEnum;
use Filament\Actions\ViewAction;
use Filament\Infolists\Components\TextEntry;
use Filament\Resources\Resource;
use Filament\Schemas\Components\Section;
use Filament\Schemas\Schema;
use Filament\Support\Icons\Heroicon;
use Filament\Tables\Columns\TextColumn;
use Filament\Tables\Table;

class ApplicationResource extends Resource
{
    protected static ?string $model = Application::class;

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

    protected static ?string $navigationLabel = 'Applications';

    protected static string|\UnitEnum|null $navigationGroup = 'Jobs';

    protected static ?int $navigationSort = 30;

    public static function form(Schema $schema): Schema
    {
        return $schema;
    }

    public static function table(Table $table): Table
    {
        return $table
            ->columns([
                TextColumn::make('user.email')
                    ->label('User')
                    ->searchable()
                    ->sortable(),

                TextColumn::make('importedJob.title')
                    ->label('Job')
                    ->searchable()
                    ->sortable(),

                TextColumn::make('importedJob.company')
                    ->label('Company')
                    ->searchable(),

                TextColumn::make('source.name')
                    ->label('Source')
                    ->sortable(),

                TextColumn::make('source_attribution')
                    ->limit(40),

                TextColumn::make('status')
                    ->badge()
                    ->sortable(),

                TextColumn::make('ip_address')
                    ->label('IP'),

                TextColumn::make('applied_at')
                    ->dateTime()
                    ->sortable(),
            ])
            ->recordActions([
                ViewAction::make(),
            ])
            ->recordUrl(fn (Application $record): string => static::getUrl('view', ['record' => $record]))
            ->defaultSort('applied_at', 'desc');
    }

    public static function infolist(Schema $schema): Schema
    {
        return $schema
            ->components([
                Section::make('Applicant')
                    ->schema([
                        TextEntry::make('user.name')
                            ->label('Name')
                            ->placeholder('Not available'),

                        TextEntry::make('user.email')
                            ->label('Email')
                            ->copyable()
                            ->placeholder('Not available'),

                        TextEntry::make('user.profile.headline')
                            ->label('Profile headline')
                            ->placeholder('No profile headline'),

                        TextEntry::make('user.resumeReport.ats_score')
                            ->label('ATS score')
                            ->placeholder('Not analyzed'),
                    ])
                    ->columns(2),

                Section::make('Job')
                    ->schema([
                        TextEntry::make('importedJob.title')
                            ->label('Job title'),

                        TextEntry::make('importedJob.company')
                            ->label('Company'),

                        TextEntry::make('importedJob.category')
                            ->label('Category')
                            ->badge(),

                        TextEntry::make('importedJob.location_type')
                            ->label('Location type')
                            ->badge()
                            ->placeholder('Remote'),

                        TextEntry::make('importedJob.salary_min')
                            ->label('Salary min')
                            ->money(fn (Application $record): string => $record->importedJob?->salary_currency ?: 'USD')
                            ->placeholder('Not provided'),

                        TextEntry::make('importedJob.salary_max')
                            ->label('Salary max')
                            ->money(fn (Application $record): string => $record->importedJob?->salary_currency ?: 'USD')
                            ->placeholder('Not provided'),

                        TextEntry::make('importedJob.skills')
                            ->label('Skills')
                            ->listWithLineBreaks()
                            ->badge()
                            ->placeholder('No skills')
                            ->columnSpanFull(),
                    ])
                    ->columns(3),

                Section::make('Application tracking')
                    ->schema([
                        TextEntry::make('status')
                            ->badge(),

                        TextEntry::make('applied_at')
                            ->dateTime(),

                        TextEntry::make('follow_up_at')
                            ->date()
                            ->placeholder('No follow-up scheduled'),

                        TextEntry::make('ip_address')
                            ->label('IP address')
                            ->placeholder('Not captured'),

                        TextEntry::make('notes')
                            ->placeholder('No notes')
                            ->columnSpanFull(),
                    ])
                    ->columns(4),

                Section::make('Source and redirect audit')
                    ->schema([
                        TextEntry::make('source.name')
                            ->label('Source')
                            ->placeholder('Not available'),

                        TextEntry::make('source_attribution')
                            ->label('Attribution')
                            ->placeholder('No attribution saved')
                            ->columnSpanFull(),

                        TextEntry::make('source_url')
                            ->label('Source URL')
                            ->copyable()
                            ->placeholder('Not saved')
                            ->columnSpanFull(),

                        TextEntry::make('apply_url')
                            ->label('Apply URL')
                            ->copyable()
                            ->placeholder('Not saved')
                            ->columnSpanFull(),

                        TextEntry::make('user_agent')
                            ->label('User agent')
                            ->placeholder('Not captured')
                            ->columnSpanFull(),
                    ])
                    ->columns(2),
            ]);
    }

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