<?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\MonyExport;
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 App\Filament\Resources\Panel\MonyExportResource\Pages;
use App\Filament\Resources\Panel\MonyExportResource\RelationManagers;
use Illuminate\Support\Facades\Auth;
use Filament\Tables\Filters\SelectFilter;
use Filament\Tables\Filters\Filter;
use Filament\Resources\RelationManagers\RelationManager;
use Carbon\Carbon;

class MonyExportResource extends Resource
{
    protected static ?string $model = MonyExport::class;

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

    protected static ?int $navigationSort = 9;

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

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

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

    public static function getNavigationLabel(): string
    {
        return __('crud.monyExports.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),

                    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('employee_id')
                        ->nullable()
                        ->relationship('employee', 'name')
                        ->searchable()
                        ->preload()
                        ->native(false),

                    Select::make('equip_id')
                        ->nullable()
                        ->relationship('equip', 'type')
                        ->searchable()
                        ->preload()
                        ->native(false),

                    Select::make('land_id')
                        ->nullable()
                        ->relationship('lands', 'name')
                        ->searchable()
                        ->preload()
                        ->multiple()
                        ->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),

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

    public static function table(Table $table): Table
    {
        return $table
            ->poll('60s')
            ->columns([
                TextColumn::make('monyType.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')->html()->badge()
                    ->searchable()
                    ->html()
                    ->sortable()
                    ->toggleable(),

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

                TextColumn::make('equip.type')->badge()
                    ->searchable()
                    ->sortable()
                    ->toggleable(),

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

                TextColumn::make('customer.name'),

                TextColumn::make('supplier.name'),
                TextColumn::make('user.name'),
            ])
            ->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('mony_type_id')
                    ->relationship('monyType', 'name')
                    ->label('Money Type'),
                SelectFilter::make('customer_id')
                    ->relationship('customer', 'name')
                    ->label('Customer'),
                SelectFilter::make('employee_id')
                    ->relationship('employee', 'name')
                    ->label('Employee'),
                SelectFilter::make('equip_id')
                    ->relationship('equip', 'type')
                    ->label('Equip'),
                SelectFilter::make('supplier_id')
                    ->relationship('supplier', 'name')
                    ->label('Supplier'),
                SelectFilter::make('land_id')
                    ->relationship('lands', 'name')
                    ->label('Land'),
               
            ])
            ->defaultSort('date', 'desc')
            // ->footer([
            //     TextColumn::make('value')
            //         ->label('Total Value')
            //         ->getStateUsing(fn (RelationManager $livewire) => $livewire->getTableQuery()->sum('value'))
            //         ->money('usd'),
            // ])
            ->actions([
                Tables\Actions\EditAction::make(),
                Tables\Actions\ViewAction::make(),
            ])
            ->bulkActions([
                Tables\Actions\BulkActionGroup::make([
                    Tables\Actions\DeleteBulkAction::make(),
                ]),
            ])
            ->defaultSort('id', 'desc');
    }

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

    public static function getPages(): array
    {
        return [
            'index' => Pages\ListMonyExports::route('/'),
            'create' => Pages\CreateMonyExport::route('/create'),
            'view' => Pages\ViewMonyExport::route('/{record}'),
            'edit' => Pages\EditMonyExport::route('/{record}/edit'),
        ];
    }
}
