<?php

namespace App\Filament\Resources;

use App\Filament\Resources\FacilitymemberResource\Pages;
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 FacilitymemberResource extends Resource
{
    protected static ?string $model = Facilitymember::class;

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

    protected static ?string $navigationLabel = 'Facility Members';

    protected static ?int $navigationSort = 5;
    protected static ?string $navigationGroup = 'Academic Management';

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

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

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

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

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

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

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

    public static function form(Form $form): Form
    {
        return $form
            ->schema([
                Forms\Components\Section::make('Member Information')
                    ->schema([
                        Forms\Components\TextInput::make('member_name')
                            ->required()
                            ->maxLength(255)
                            ->label('Name'),

                        Forms\Components\Select::make('program_id')
                            ->label('Program')
                            ->relationship('program', 'main_title')
                            ->required(),

                        Forms\Components\FileUpload::make('member_img')
                            ->image()
                            ->directory('facility/members')
                            ->required()
                            ->label('Profile Image'),

                        Forms\Components\TextInput::make('member_position')
                            ->required()
                            ->maxLength(255)
                            ->label('Position'),

                        Forms\Components\TextInput::make('member_job_title')
                            ->required()
                            ->maxLength(255)
                            ->label('Job Title'),

                        Forms\Components\Textarea::make('member_desc')
                            ->required()
                            ->maxLength(65535)
                            ->columnSpanFull()
                            ->label('Description'),
                    ])
                    ->columns(2),
            ]);
    }

    public static function table(Table $table): Table
    {
        return $table
            ->columns([
                Tables\Columns\TextColumn::make('member_name')
                    ->searchable()
                    ->sortable()
                    ->label('Name'),

                Tables\Columns\TextColumn::make('program.main_title')
                    ->label('Program')
                    ->sortable()
                    ->searchable(),

                Tables\Columns\ImageColumn::make('member_img')
                    ->label('Profile Image'),

                Tables\Columns\TextColumn::make('member_position')
                    ->searchable()
                    ->sortable()
                    ->label('Position'),

                Tables\Columns\TextColumn::make('member_job_title')
                    ->searchable()
                    ->sortable()
                    ->label('Job Title'),

                Tables\Columns\TextColumn::make('created_at')
                    ->dateTime()
                    ->sortable()
                    ->toggleable(isToggledHiddenByDefault: true),

                Tables\Columns\TextColumn::make('updated_at')
                    ->dateTime()
                    ->sortable()
                    ->toggleable(isToggledHiddenByDefault: true),
            ])
            ->filters([
                //
            ])
            ->actions([
                Tables\Actions\EditAction::make()
                    ->visible(fn () => Gate::allows('edit facilitymember')),
                Tables\Actions\DeleteAction::make()
                    ->visible(fn () => Gate::allows('delete facilitymember')),
            ])
            ->bulkActions([
                Tables\Actions\BulkActionGroup::make([
                    Tables\Actions\DeleteBulkAction::make()
                        ->visible(fn () => Gate::allows('delete facilitymember')),
                ]),
            ]);
    }

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

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