<?php

namespace App\Filament\Resources;

use App\Filament\Resources\NewsPostResource\Pages;
use App\Filament\Resources\NewsPostResource\RelationManagers;
use App\Models\NewsPost;
use Filament\Forms;
use Filament\Forms\Form;
use Filament\Resources\Resource;
use Filament\Tables;
use Filament\Tables\Table;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\SoftDeletingScope;

class NewsPostResource extends Resource
{
    protected static ?string $model = NewsPost::class;

    protected static ?string $navigationIcon = 'heroicon-o-newspaper';
    protected static ?string $navigationLabel = 'News Articles';
    protected static ?string $navigationGroup = 'Content';
    protected static ?int $navigationSort = 50;

    public static function form(Form $form): Form
    {
        return $form
            ->schema([
                Forms\Components\Section::make('Article content')
                    ->description('Create and edit news items shown on the homepage preview and full News page.')
                    ->schema([
                        Forms\Components\TextInput::make('title')
                            ->label('Article title')
                            ->required()
                            ->maxLength(255),
                        Forms\Components\TextInput::make('slug')
                            ->label('URL slug')
                            ->required()
                            ->maxLength(255)
                            ->helperText('Used in the article URL. Example: supported-living-sector-update'),
                        Forms\Components\Textarea::make('excerpt')
                            ->label('Short summary')
                            ->rows(3)
                            ->columnSpanFull()
                            ->helperText('Shown on article cards and search previews.'),
                        Forms\Components\RichEditor::make('body')
                            ->label('Article body')
                            ->required()
                            ->columnSpanFull()
                            ->helperText('Use headings, bullet lists, links, and paragraphs for a readable article.'),
                    ])
                    ->columns(2),

                Forms\Components\Section::make('Publishing and media')
                    ->description('Control article visibility, category, and cover image.')
                    ->schema([
                        Forms\Components\Select::make('status')
                            ->label('Status')
                            ->options([
                                'draft' => 'Draft',
                                'published' => 'Published',
                            ])
                            ->required()
                            ->default('draft'),
                        Forms\Components\DateTimePicker::make('published_at')
                            ->label('Publish date/time'),
                        Forms\Components\Select::make('category_id')
                            ->label('Category')
                            ->relationship('category', 'name')
                            ->searchable()
                            ->preload(),
                        Forms\Components\Select::make('cover_media_id')
                            ->label('Cover image')
                            ->relationship('cover', 'filename')
                            ->searchable()
                            ->preload()
                            ->helperText('Image shown on article cards and the article page.'),
                        Forms\Components\TextInput::make('author_user_id')
                            ->label('Author user ID')
                            ->numeric()
                            ->helperText('Optional internal author ID.'),
                    ])
                    ->columns(2),

                Forms\Components\Section::make('SEO and sharing')
                    ->collapsed()
                    ->schema([
                        Forms\Components\TextInput::make('meta_title')
                            ->label('Meta title'),
                        Forms\Components\Textarea::make('meta_description')
                            ->label('Meta description')
                            ->columnSpanFull(),
                        Forms\Components\TextInput::make('og_title')
                            ->label('Social sharing title'),
                        Forms\Components\Textarea::make('og_description')
                            ->label('Social sharing description')
                            ->columnSpanFull(),
                        Forms\Components\Select::make('og_media_id')
                            ->label('Social sharing image')
                            ->relationship('ogMedia', 'filename')
                            ->searchable()
                            ->preload(),
                    ])
                    ->columns(2),
            ]);
    }

    public static function table(Table $table): Table
    {
        return $table
            ->columns([
                Tables\Columns\TextColumn::make('title')
                    ->label('Title')
                    ->searchable(),
                Tables\Columns\TextColumn::make('category.name')
                    ->label('Category')
                    ->badge()
                    ->sortable(),
                Tables\Columns\TextColumn::make('status')
                    ->label('Status')
                    ->badge()
                    ->searchable(),
                Tables\Columns\TextColumn::make('published_at')
                    ->label('Published')
                    ->dateTime()
                    ->sortable(),
                Tables\Columns\TextColumn::make('cover.filename')
                    ->label('Cover image')
                    ->toggleable(),
                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\SelectFilter::make('status')
                    ->options([
                        'draft' => 'Draft',
                        'published' => 'Published',
                    ]),
                Tables\Filters\SelectFilter::make('category_id')
                    ->label('Category')
                    ->relationship('category', 'name')
                    ->searchable()
                    ->preload(),
            ])
            ->actions([
                Tables\Actions\EditAction::make(),
                Tables\Actions\DeleteAction::make(),
            ])
            ->bulkActions([
                Tables\Actions\BulkActionGroup::make([
                    Tables\Actions\DeleteBulkAction::make(),
                ]),
            ]);
    }

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

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