<?php

namespace App\Filament\Resources;

use App\Models\AboutUsMessage;
use Filament\Forms;
use Filament\Tables;
use Filament\Forms\Form;
use Filament\Tables\Table;
use Filament\Resources\Resource;
use Filament\Forms\Components\Section;
use Filament\Forms\Components\TextInput;
use Filament\Forms\Components\Textarea;
use Filament\Forms\Components\FileUpload;
use Filament\Tables\Columns\TextColumn;
use Filament\Tables\Columns\ImageColumn;
use App\Filament\Resources\AboutUsMessageResource\Pages;
use Illuminate\Support\Facades\Gate;

class AboutUsMessageResource extends Resource
{
    protected static ?string $model = AboutUsMessage::class;
    protected static ?string $navigationIcon = 'heroicon-o-chat-bubble-left-right';
    protected static ?string $navigationGroup = 'Website Content';

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

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

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

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

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

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

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

    public static function form(Form $form): Form
    {
        return $form->schema([
            Section::make('Message Information')
                ->schema([
                    TextInput::make('messages_tittle')
                        ->label('Title')
                        ->required()
                        ->maxLength(255),
                    TextInput::make('messages_name')
                        ->label('Name')
                        ->required()
                        ->maxLength(255),
                    TextInput::make('messages_position')
                        ->label('Position')
                        ->required()
                        ->maxLength(255),
                    Textarea::make('messages_text')
                        ->label('Message')
                        ->required()
                        ->columnSpanFull(),
                    FileUpload::make('messages_photo')
                        ->label('Photo')
                        ->image()
                        ->directory('about-us-messages')
                        ->required(),
                ])
                ->columns(2),
        ]);
    }

    public static function table(Table $table): Table
    {
        return $table
            ->columns([
                ImageColumn::make('messages_photo')
                    ->label('Photo')
                    ->circular(),
                TextColumn::make('messages_tittle')
                    ->label('Title')
                    ->searchable()
                    ->sortable(),
                TextColumn::make('messages_name')
                    ->label('Name')
                    ->searchable()
                    ->sortable(),
                TextColumn::make('messages_position')
                    ->label('Position')
                    ->searchable(),
                TextColumn::make('created_at')
                    ->dateTime()
                    ->sortable()
                    ->toggleable(isToggledHiddenByDefault: true),
                TextColumn::make('updated_at')
                    ->dateTime()
                    ->sortable()
                    ->toggleable(isToggledHiddenByDefault: true),
            ])
            ->defaultSort('created_at', 'desc')
            ->actions([
                Tables\Actions\EditAction::make()
                    ->visible(fn () => Gate::allows('edit aboutusmessage')),
                Tables\Actions\DeleteAction::make()
                    ->visible(fn () => Gate::allows('delete aboutusmessage')),
            ])
            ->bulkActions([
                Tables\Actions\BulkActionGroup::make([
                    Tables\Actions\DeleteBulkAction::make()
                        ->visible(fn () => Gate::allows('delete aboutusmessage')),
                ]),
            ]);
    }

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