<?php

namespace App\Filament\Resources\Publication\Announcements;

use App\Enums\AnnouncementType;
use App\Filament\Actions\DefaultBulkActions;
use App\Filament\Columns\TimestampColumns;
use App\Filament\Resources\Publication\Announcements\Pages\ManageAnnouncements;
use App\Models\Announcement;
use App\Models\PartnerMedia;
use BackedEnum;
use Filament\Actions\BulkActionGroup;
use Filament\Actions\DeleteAction;
use Filament\Actions\EditAction;
use Filament\Actions\ForceDeleteAction;
use Filament\Actions\RestoreAction;
use Filament\Forms\Components\CheckboxList;
use Filament\Forms\Components\Select;
use Filament\Forms\Components\Textarea;
use Filament\Resources\Resource;
use Filament\Schemas\Schema;
use Filament\Support\Enums\Width;
use Filament\Support\Icons\Heroicon;
use Filament\Tables\Columns\TextColumn;
use Filament\Tables\Filters\TrashedFilter;
use Filament\Tables\Table;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\SoftDeletingScope;
use UnitEnum;

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

    protected static string|BackedEnum|null $navigationIcon = Heroicon::Megaphone;

    protected static ?string $navigationLabel = 'Pengumuman';

    protected static string|UnitEnum|null $navigationGroup = 'Publikasi';

    protected static ?string $slug = 'publication/announcements';

    protected static ?string $recordTitleAttribute = 'content';

    protected static ?int $navigationSort = 2;

    public static function form(Schema $schema): Schema
    {
        return $schema
            ->components([
                Textarea::make('content')
                    ->label('Konten')
                    ->placeholder('Diberitahukan kepada seluruh media untuk update dokumen...')
                    ->autocomplete(false)
                    ->autofocus()
                    ->required()
                    ->rows(4)
                    ->columnSpanFull(),

                Select::make('type')
                    ->label('Tipe Pengumuman')
                    ->options(AnnouncementType::class)
                    ->required()
                    ->native(false)
                    ->default(AnnouncementType::PUBLIC->value)
                    ->live(),

                CheckboxList::make('partnerMedia')
                    ->label('Kirim Kepada')
                    ->relationship('partnerMedia', 'name')
                    ->visible(fn ($get) => in_array($get('type'), [AnnouncementType::COMPANY, AnnouncementType::COMPANY->value]))
                    ->required(fn ($get) => in_array($get('type'), [AnnouncementType::COMPANY, AnnouncementType::COMPANY->value]))
                    ->options(function () {
                        return PartnerMedia::query()->pluck('name', 'id');
                    })
                    ->searchable()
                    ->noSearchResultsMessage('Tidak ada media yang ditemukan.')
                    ->helperText('Pilih media yang akan menerima pengumuman ini')
                    ->columnSpanFull(),
            ])
            ->columns(1);
    }

    public static function table(Table $table): Table
    {
        return $table
            ->recordTitleAttribute('content')
            ->columns([
                TextColumn::make('content')
                    ->label('Konten')
                    ->searchable()
                    ->sortable()
                    ->wrap()
                    ->limit(100),

                TextColumn::make('type')
                    ->label('Tipe')
                    ->badge()
                    ->sortable(),

                TextColumn::make('partnerMedia.name')
                    ->label('Penerima')
                    ->listWithLineBreaks()
                    ->limitList(3)
                    ->expandableLimitedList()
                    ->placeholder(fn ($record) => $record->type === AnnouncementType::PUBLIC ? 'Publik' : 'Tidak ada penerima'),

                ...TimestampColumns::make(),
            ])
            ->filters([
                TrashedFilter::make()->native(false),
            ])
            ->recordActions([
                EditAction::make()
                    ->modalWidth(Width::Large),

                DeleteAction::make(),

                ForceDeleteAction::make(),

                RestoreAction::make(),
            ])
            ->toolbarActions([
                BulkActionGroup::make([
                    ...DefaultBulkActions::make('Pengumuman'),
                ]),
            ])
            ->emptyStateIcon(Heroicon::Megaphone)
            ->emptyStateDescription('Setelah Anda membuat data pertama, maka akan muncul disini.')
            ->defaultSort('created_at', 'desc')
            ->deferFilters(false);
    }

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

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

    public static function getRecordRouteBindingEloquentQuery(): Builder
    {
        return parent::getRecordRouteBindingEloquentQuery()
            ->withoutGlobalScopes([
                SoftDeletingScope::class,
            ]);
    }
}
