<?php

namespace App\Filament\Resources\Panel;

use Filament\Forms;
use Filament\Tables;
use Livewire\Component;
use Filament\Forms\Form;
use Filament\Tables\Table;
use App\Models\MonyImport;
use Filament\Resources\Resource;
use Filament\Forms\Components\Grid;
use Filament\Forms\Components\Select;
use Filament\Forms\Components\Section;
use Filament\Tables\Columns\TextColumn;
use Filament\Forms\Components\TextInput;
use Illuminate\Database\Eloquent\Builder;
use Filament\Forms\Components\DatePicker;
use Filament\Forms\Components\RichEditor;
use Filament\Forms\Components\DateTimePicker;
use Illuminate\Database\Eloquent\SoftDeletingScope;
use App\Filament\Resources\Panel\MonyImportResource\Pages;
use App\Filament\Resources\Panel\MonyImportResource\RelationManagers;
use Illuminate\Support\Facades\Auth;
use Carbon\Carbon;
use Filament\Tables\Filters\SelectFilter;
use Filament\Tables\Filters\Filter;

class MonyImportResource extends Resource
{
    protected static ?string $model = MonyImport::class;

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

    protected static ?int $navigationSort = 10;

    protected static ?string $navigationGroup = 'العمل اليومي';

    public static function getModelLabel(): string
    {
        return __('crud.monyImports.itemTitle');
    }

    public static function getPluralModelLabel(): string
    {
        return __('crud.monyImports.collectionTitle');
    }

    public static function getNavigationLabel(): string
    {
        return __('crud.monyImports.collectionTitle');
    }

    public static function form(Form $form): Form
    {
        return $form->schema([
            Section::make()->schema([
                Grid::make(['default' => 1])->schema([
                    Select::make('mony_type_id')
                        ->required()
                        ->relationship('monyType', 'name')
                        ->searchable()
                        ->preload()
                        ->native(false),

                    Select::make('customer_id')
                        ->nullable()
                        ->relationship('customer', 'name')
                        ->searchable()
                        ->preload()
                        ->native(false),

                    Select::make('supplier_id')
                        ->nullable()
                        ->relationship('supplier', 'name')
                        ->searchable()
                        ->preload()
                        ->native(false),

                    DatePicker::make('date')
                    ->rules(['date'])
                    ->default(Carbon::today())                        ->required()
                        ->native(false),

                    TextInput::make('value')
                        ->required()
                        ->numeric()
                        ->step(1),

                    RichEditor::make('notes')
                        ->nullable()
                        ->string()
                        ->fileAttachmentsVisibility('public'),

                        Select::make('land_id')
                        ->nullable()
                        ->multiple()
                        ->relationship('lands', 'name')
                        ->searchable()
                        ->preload()
                        ->native(false),

                    Select::make('user_id')
                    ->required()
                    ->default(Auth::user()->id)
                    ->disabled()
                    ->relationship('user', 'name')
                        ->searchable()
                        ->preload()
                        ->native(false),

                  
                 
                ]),
            ]),
        ]);
    }

    public static function table(Table $table): Table
    {
        return $table
            ->poll('60s')
            ->columns([
                TextColumn::make('monyType.name')->badge()
                ->searchable()
                ->sortable()
                ->toggleable(),

                TextColumn::make('customer.name')->badge()
                ->searchable()
                ->sortable()
                ->toggleable(),

                TextColumn::make('supplier.name')->badge()
                ->searchable()
                ->sortable()
                ->toggleable(),

                TextColumn::make('date')->date('D-d-M-Y')->badge()
                ->searchable()
                ->sortable()
                ->toggleable(),

                TextColumn::make('value')->badge()
                ->searchable()
                ->sortable()
                ->toggleable(),

                TextColumn::make('notes')->limit(255)->badge()
                ->searchable()
                ->html()
                ->sortable()
                ->toggleable(),

                // TextColumn::make('user.name')->badge()
                // ->searchable()
                // ->sortable()
                // ->toggleable(),

                TextColumn::make('lands.name')->badge()
                ->searchable()
                ->sortable()
                ->toggleable(),

            ])
            ->filters([
                Filter::make('date')
                ->label('Date Range')
                ->form([
                    DatePicker::make('start_date')->label('Start Date'),
                    DatePicker::make('end_date')->label('End Date'),
                ])
                ->query(function (Builder $query, array $data): Builder {
                    return $query
                        ->when($data['start_date'], fn(Builder $query) => $query->where('date', '>=', $data['start_date']))
                        ->when($data['end_date'], fn(Builder $query) => $query->where('date', '<=', $data['end_date']));
                }),

                SelectFilter::make('money_type_id')
                    ->relationship('monyType', 'name')
                    ->label('Money Type'),
                SelectFilter::make('customer_id')
                    ->relationship('customer', 'name')
                    ->label('Customer'),
                
                SelectFilter::make('supplier_id')
                    ->relationship('supplier', 'name')
                    ->label('Supplier'),
                // SelectFilter::make('land_id')
                //     ->relationship('land', 'name')
                //     ->label('Land'),
            ])
            ->actions([
                Tables\Actions\EditAction::make(),
                Tables\Actions\ViewAction::make(),
            ])
            ->bulkActions([
                Tables\Actions\BulkActionGroup::make([
                    Tables\Actions\DeleteBulkAction::make(),

                    Tables\Actions\ForceDeleteBulkAction::make(),
                    Tables\Actions\RestoreBulkAction::make(),
                ]),
            ])
            ->defaultSort('date', 'desc');
    }

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

    public static function getPages(): array
    {
        return [
            'index' => Pages\ListMonyImports::route('/'),
            'create' => Pages\CreateMonyImport::route('/create'),
            'view' => Pages\ViewMonyImport::route('/{record}'),
            'edit' => Pages\EditMonyImport::route('/{record}/edit'),
        ];
    }

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