<?php

namespace App\Filament\Resources\Announcements;

use App\Filament\Resources\Announcements\Pages\ManageAnnouncements;
use App\Models\Announcement;
use BackedEnum;
use Filament\Actions\BulkActionGroup;
use Filament\Actions\DeleteAction;
use Filament\Actions\DeleteBulkAction;
use Filament\Actions\EditAction;
use Filament\Forms\Components\TextInput;
use Filament\Forms\Components\Textarea;
use Filament\Forms\Components\Toggle;
use Filament\Resources\Resource;
use Filament\Schemas\Schema;
use Filament\Support\Icons\Heroicon;
use Filament\Tables\Columns\IconColumn;
use Filament\Tables\Columns\TextColumn;
use Filament\Tables\Table;

use Filament\Forms\Components\ColorPicker;
use Filament\Schemas\Components\Section;

class AnnouncementResource extends Resource
{
    protected static ?string $model = Announcement::class;

    protected static string|BackedEnum|null $navigationIcon = 'heroicon-o-megaphone';

    protected static string|\UnitEnum|null $navigationGroup = 'Site Builder';

    protected static ?int $navigationSort = 6;

    protected static ?string $recordTitleAttribute = 'text';

    public static function form(Schema $schema): Schema
    {
        return $schema
            ->components([
                Section::make('Announcement Bar details')
                    ->description('Provide the text, destination link, and background/text colors for the top announcement banner.')
                    ->schema([
                        Textarea::make('text')
                            ->label('Banner Text')
                            ->required()
                            ->rows(3)
                            ->placeholder('e.g. Free shipping on orders over ৳1,000! Use code FREE1000')
                            ->columnSpanFull(),
                        TextInput::make('link_url')
                            ->label('Redirect Link (Optional)')
                            ->url()
                            ->placeholder('e.g. /offers/free-shipping'),
                        ColorPicker::make('bg_color')
                            ->label('Background Color')
                            ->required()
                            ->default('#003b20'),
                        ColorPicker::make('text_color')
                            ->label('Text Color')
                            ->required()
                            ->default('#ffffff'),
                        Toggle::make('is_active')
                            ->label('Active Storefront')
                            ->default(true)
                            ->required(),
                    ])->columns(2)
            ]);
    }

    public static function table(Table $table): Table
    {
        return $table
            ->recordTitleAttribute('text')
            ->columns([
                TextColumn::make('link_url')
                    ->searchable(),
                TextColumn::make('bg_color')
                    ->searchable(),
                TextColumn::make('text_color')
                    ->searchable(),
                IconColumn::make('is_active')
                    ->boolean(),
                TextColumn::make('created_at')
                    ->dateTime()
                    ->sortable()
                    ->toggleable(isToggledHiddenByDefault: true),
                TextColumn::make('updated_at')
                    ->dateTime()
                    ->sortable()
                    ->toggleable(isToggledHiddenByDefault: true),
            ])
            ->filters([
                //
            ])
            ->recordActions([
                EditAction::make(),
                DeleteAction::make(),
            ])
            ->toolbarActions([
                BulkActionGroup::make([
                    DeleteBulkAction::make(),
                ]),
            ]);
    }

    public static function getPages(): array
    {
        return [
            'index' => ManageAnnouncements::route('/'),
        ];
    }
}
