<?php

namespace App\Filament\Resources;

use App\Models\NewsEvent;
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\ColorPicker;
use Filament\Tables\Columns\TextColumn;
use App\Filament\Resources\NewsEventResource\Pages;
use Illuminate\Support\Facades\Gate;

class NewsEventResource extends Resource
{
    protected static ?string $model = NewsEvent::class;

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

    protected static ?string $navigationLabel = 'News & Events';

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

    protected static ?int $navigationSort = 2;

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

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

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

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

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

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

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

    public static function form(Form $form): Form
    {
        return $form
            ->schema([
                Section::make('Content Information')
                    ->schema([
                        TextInput::make('main_title')
                            ->label('Main Title')
                            ->required(),
                        Textarea::make('text')
                            ->label('Content Text')
                            ->required(),
                        TextInput::make('news_count')
                            ->label('Number of News Items to Display')
                            ->numeric()
                            ->default(3)
                            ->required(),
                    ])->columns(1),

                Section::make('Button Settings')
                    ->schema([
                        TextInput::make('button_text')
                            ->label('Button Text')
                            ->required(),
                        ColorPicker::make('button_color')
                            ->label('Button Text Color')
                            ->default('#ffffff')
                            ->required(),
                        ColorPicker::make('button_background_color')
                            ->label('Button Background Color')
                            ->default('#000000')
                            ->required(),
                    ])->columns(2),
            ]);
    }

    public static function table(Table $table): Table
    {
        return $table
            ->columns([
                TextColumn::make('main_title')
                    ->label('Title')
                    ->searchable()
                    ->sortable(),
                TextColumn::make('button_text')
                    ->label('Button Text')
                    ->searchable(),
                TextColumn::make('news_count')
                    ->label('News Count')
                    ->sortable(),
                TextColumn::make('created_at')
                    ->dateTime()
                    ->sortable()
                    ->toggleable(isToggledHiddenByDefault: true),
                TextColumn::make('updated_at')
                    ->dateTime()
                    ->sortable()
                    ->toggleable(isToggledHiddenByDefault: true),
            ])
            ->filters([
                //
            ])
            ->actions([
                Tables\Actions\EditAction::make()
                    ->visible(fn () => Gate::allows('edit newsevent')),
                Tables\Actions\DeleteAction::make()
                    ->visible(fn () => Gate::allows('delete newsevent')),
            ])
            ->bulkActions([
                Tables\Actions\BulkActionGroup::make([
                    Tables\Actions\DeleteBulkAction::make()
                        ->visible(fn () => Gate::allows('delete newsevent')),
                ]),
            ]);
    }

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

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