<?php

namespace App\Filament\Resources;

use App\Filament\Resources\ProgramResource\Pages;
use App\Models\Program;
use App\Models\Facilitymember;
use Filament\Forms;
use Filament\Forms\Form;
use Filament\Resources\Resource;
use Filament\Tables;
use Filament\Tables\Table;
use Illuminate\Support\Facades\Gate;

class ProgramResource extends Resource
{
    protected static ?string $model = Program::class;

    protected static ?string $navigationIcon = 'heroicon-o-academic-cap';

    protected static ?string $navigationGroup = 'Academic Management';

    protected static ?int $navigationSort = 2;

    public static function canViewAny(): bool
    {
        return Gate::allows('view program');
    }

    public static function canView($record): bool
    {
        return Gate::allows('view program');
    }

    public static function canCreate(): bool
    {
        return Gate::allows('create program');
    }

    public static function canEdit($record): bool
    {
        return Gate::allows('edit program');
    }

    public static function canDelete($record): bool
    {
        return Gate::allows('delete program');
    }

    public static function canRestore($record): bool
    {
        return Gate::allows('restore program');
    }

    public static function canForceDelete($record): bool
    {
        return Gate::allows('force delete program');
    }

    public static function form(Form $form): Form
    {
        return $form
            ->schema([
                Forms\Components\Section::make('Basic Information')
                    ->schema([
                        Forms\Components\TextInput::make('main_title')
                            ->required()
                            ->maxLength(255)
                            ->placeholder('Enter program main title'),
                        Forms\Components\TextInput::make('main_code')
                            ->required()
                            ->maxLength(255)
                            ->placeholder('Enter program code'),
                        Forms\Components\FileUpload::make('background_image')
                            ->image()
                            ->directory('program-backgrounds')
                            ->visibility('public')
                            ->imageEditor()
                            ->placeholder('Upload background image'),
                        Forms\Components\Textarea::make('description')
                            ->required()
                            ->rows(4)
                            ->placeholder('Enter program description')
                            ->columnSpanFull(),
                    ]),

                Forms\Components\Section::make('Button Configuration')
                    ->schema([
                        Forms\Components\TextInput::make('button_text')
                            ->required()
                            ->maxLength(255)
                            ->placeholder('Enter button text'),
                        Forms\Components\TextInput::make('button_link')
                            ->required()
                            ->maxLength(255)
                            ->placeholder('Enter button link'),
                        Forms\Components\ColorPicker::make('button_color')
                            ->required()
                            ->label('Button Text Color'),
                        Forms\Components\ColorPicker::make('button_background_color')
                            ->required()
                            ->label('Button Background Color'),
                    ]),

                Forms\Components\Section::make('Program Levels')
                    ->schema([
                        Forms\Components\TextInput::make('curriculum_title')
                            ->required()
                            ->maxLength(255)
                            ->placeholder('Enter curriculum title'),
                        Forms\Components\Repeater::make('level_contain')
                            ->schema([
                                Forms\Components\TextInput::make('name')
                                    ->required()
                                    ->maxLength(255)
                                    ->placeholder('Enter level name'),
                                Forms\Components\Textarea::make('description')
                                    ->required()
                                    ->rows(3)
                                    ->placeholder('Enter level description'),
                            ])
                            ->defaultItems(1)
                            ->minItems(1)
                            ->maxItems(10)
                            ->columnSpanFull()
                            ->label('Program Levels'),
                    ]),

                Forms\Components\Section::make('Job Opportunities')
                    ->schema([
                        Forms\Components\TextInput::make('job_title')
                            ->required()
                            ->maxLength(255)
                            ->placeholder('Enter job opportunities title'),
                        Forms\Components\Repeater::make('job_contain')
                            ->schema([
                                Forms\Components\TextInput::make('name')
                                    ->required()
                                    ->maxLength(255)
                                    ->placeholder('Enter job name'),
                                Forms\Components\FileUpload::make('image')
                                    ->image()
                                    ->required()
                                    ->directory('program-jobs')
                                    ->visibility('public')
                                    ->imageEditor()
                                    ->placeholder('Upload job image'),
                            ])
                            ->defaultItems(1)
                            ->minItems(1)
                            ->maxItems(10)
                            ->columnSpanFull()
                            ->label('Job Opportunities'),
                    ]),

                Forms\Components\Section::make('Program Members')
                    ->schema([
                        Forms\Components\Select::make('program_members')
                            ->options([
                                'faculty' => 'Faculty Members',
                                'students' => 'Students',
                                'alumni' => 'Alumni',
                                'industry_partners' => 'Industry Partners',
                            ])
                            ->multiple()
                            ->preload()
                            ->placeholder('Select program members')
                            ->columnSpanFull()
                            ->label('Program Members'),
                    ]),
            ]);
    }

    public static function table(Table $table): Table
    {
        return $table
            ->columns([
                Tables\Columns\TextColumn::make('main_title')
                    ->label('Program Title')
                    ->sortable()
                    ->searchable()
                    ->limit(50),
                Tables\Columns\TextColumn::make('main_code')
                    ->label('Code')
                    ->sortable()
                    ->searchable(),
                Tables\Columns\ImageColumn::make('background_image')
                    ->label('Background')
                    ->circular(),
                Tables\Columns\TextColumn::make('description')
                    ->label('Description')
                    ->limit(100)
                    ->searchable(),
                Tables\Columns\TextColumn::make('button_text')
                    ->label('Button Text')
                    ->sortable(),
                Tables\Columns\TextColumn::make('curriculum_title')
                    ->label('Curriculum Title')
                    ->sortable(),
                Tables\Columns\TextColumn::make('job_title')
                    ->label('Job Title')
                    ->sortable(),
                Tables\Columns\TextColumn::make('level_contain')
                    ->label('Levels Count')
                    ->formatStateUsing(fn ($state) => is_array($state) ? count($state) : 0)
                    ->badge()
                    ->color('info'),
                Tables\Columns\TextColumn::make('job_contain')
                    ->label('Jobs Count')
                    ->formatStateUsing(fn ($state) => is_array($state) ? count($state) : 0)
                    ->badge()
                    ->color('success'),
                Tables\Columns\TextColumn::make('created_at')
                    ->dateTime()
                    ->sortable()
                    ->toggleable(isToggledHiddenByDefault: true),
                Tables\Columns\TextColumn::make('updated_at')
                    ->dateTime()
                    ->sortable()
                    ->toggleable(isToggledHiddenByDefault: true),
            ])
            ->filters([
                Tables\Filters\Filter::make('has_levels')
                    ->query(fn ($query) => $query->whereJsonLength('level_contain', '>', 0))
                    ->label('Has Program Levels'),
                Tables\Filters\Filter::make('has_jobs')
                    ->query(fn ($query) => $query->whereJsonLength('job_contain', '>', 0))
                    ->label('Has Job Opportunities'),
            ])
            ->actions([
                Tables\Actions\ViewAction::make()
                    ->visible(fn () => Gate::allows('view program')),
                Tables\Actions\EditAction::make()
                    ->visible(fn () => Gate::allows('edit program')),
                Tables\Actions\DeleteAction::make()
                    ->visible(fn () => Gate::allows('delete program')),
            ])
            ->bulkActions([
                Tables\Actions\BulkActionGroup::make([
                    Tables\Actions\DeleteBulkAction::make()
                        ->visible(fn () => Gate::allows('delete program')),
                ]),
            ]);
    }

    public static function getRelations(): array
    {
        return [
            //
        ];
    }

    public static function getPages(): array
    {
        return [
            'index' => Pages\ListPrograms::route('/'),
            'create' => Pages\CreateProgram::route('/create'),
            'edit' => Pages\EditProgram::route('/{record}/edit'),
        ];
    }
}
