<?php

namespace App\Filament\Resources\ImportedJobs;

use App\Filament\Resources\ImportedJobs\Pages\EditImportedJob;
use App\Filament\Resources\ImportedJobs\Pages\ListImportedJobs;
use App\Models\ImportedJob;
use BackedEnum;
use Filament\Actions\DeleteAction;
use Filament\Actions\EditAction;
use Filament\Forms\Components\DateTimePicker;
use Filament\Forms\Components\Select;
use Filament\Forms\Components\TagsInput;
use Filament\Forms\Components\Textarea;
use Filament\Forms\Components\TextInput;
use Filament\Forms\Components\Toggle;
use Filament\Resources\Resource;
use Filament\Schemas\Schema;
use Filament\Support\Icons\Heroicon;
use Filament\Tables\Columns\IconColumn;
use Filament\Tables\Columns\TextColumn;
use Filament\Tables\Filters\SelectFilter;
use Filament\Tables\Table;

class ImportedJobResource extends Resource
{
    protected static ?string $model = ImportedJob::class;

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

    protected static ?string $navigationLabel = 'Imported Jobs';

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

    protected static ?int $navigationSort = 20;

    public static function form(Schema $schema): Schema
    {
        return $schema
            ->components([
                Select::make('job_source_id')
                    ->relationship('source', 'name')
                    ->required(),

                TextInput::make('external_id')
                    ->maxLength(255),

                TextInput::make('title')
                    ->required()
                    ->maxLength(255),

                TextInput::make('company')
                    ->required()
                    ->maxLength(255),

                Select::make('category')
                    ->required()
                    ->options([
                        'tech' => 'Tech',
                        'design' => 'Design',
                        'marketing' => 'Marketing',
                        'sales' => 'Sales',
                        'writing' => 'Writing',
                        'support' => 'Support',
                        'product' => 'Product',
                        'operations' => 'Operations',
                        'virtual-assistant' => 'Virtual assistant',
                        'finance' => 'Finance',
                        'hr' => 'HR',
                        'data' => 'Data',
                        'ai' => 'AI',
                        'general' => 'General',
                    ]),

                Select::make('employment_type')
                    ->options([
                        'full_time' => 'Full-time',
                        'contract' => 'Contract',
                        'part_time' => 'Part-time',
                        'freelance' => 'Freelance',
                    ]),

                Select::make('experience_level')
                    ->options([
                        'entry' => 'Entry',
                        'junior' => 'Junior',
                        'mid' => 'Mid',
                        'senior' => 'Senior',
                        'lead' => 'Lead',
                        'manager' => 'Manager',
                        'executive' => 'Executive',
                    ]),

                TextInput::make('location_type')
                    ->default('remote')
                    ->maxLength(80),

                TextInput::make('country_code')
                    ->maxLength(2),

                TextInput::make('timezone')
                    ->maxLength(255),

                TextInput::make('salary_currency')
                    ->maxLength(3),

                TextInput::make('salary_min')
                    ->numeric(),

                TextInput::make('salary_max')
                    ->numeric(),

                Textarea::make('summary')
                    ->maxLength(2000),

                Textarea::make('description'),

                TagsInput::make('skills'),

                TagsInput::make('tags'),

                TextInput::make('source_url')
                    ->url()
                    ->required()
                    ->maxLength(2048),

                TextInput::make('apply_url')
                    ->url()
                    ->required()
                    ->maxLength(2048),

                Textarea::make('source_attribution')
                    ->maxLength(1000),

                Select::make('status')
                    ->required()
                    ->options([
                        ImportedJob::STATUS_ACTIVE => 'Active',
                        ImportedJob::STATUS_INACTIVE => 'Inactive',
                        ImportedJob::STATUS_EXPIRED => 'Expired',
                    ]),

                Toggle::make('is_featured'),

                DateTimePicker::make('posted_at'),

                DateTimePicker::make('expires_at'),

                DateTimePicker::make('last_seen_at'),
            ])
            ->columns(2);
    }

    public static function table(Table $table): Table
    {
        return $table
            ->columns([
                TextColumn::make('title')
                    ->searchable()
                    ->sortable(),

                TextColumn::make('company')
                    ->searchable()
                    ->sortable(),

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

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

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

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

                IconColumn::make('is_featured')
                    ->boolean()
                    ->sortable(),

                TextColumn::make('posted_at')
                    ->dateTime()
                    ->sortable(),

                TextColumn::make('created_at')
                    ->dateTime()
                    ->sortable(),
            ])
            ->filters([
                SelectFilter::make('category')
                    ->options([
                        'tech' => 'Tech',
                        'design' => 'Design',
                        'marketing' => 'Marketing',
                        'sales' => 'Sales',
                        'writing' => 'Writing',
                        'support' => 'Support',
                        'product' => 'Product',
                        'operations' => 'Operations',
                        'virtual-assistant' => 'Virtual assistant',
                        'finance' => 'Finance',
                        'hr' => 'HR',
                        'data' => 'Data',
                        'ai' => 'AI',
                        'general' => 'General',
                    ]),

                SelectFilter::make('status')
                    ->options([
                        ImportedJob::STATUS_ACTIVE => 'Active',
                        ImportedJob::STATUS_INACTIVE => 'Inactive',
                        ImportedJob::STATUS_EXPIRED => 'Expired',
                    ]),
            ])
            ->recordActions([
                EditAction::make(),
                DeleteAction::make(),
            ])
            ->defaultSort('created_at', 'desc');
    }

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