<?php

namespace App\Filament\Resources\Panel;

use Filament\Forms;
use Filament\Tables;
use Livewire\Component;
use App\Models\Product;
use Filament\Forms\Form;
use Filament\Tables\Table;
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 Filament\Tables\Columns\ImageColumn;
use Illuminate\Database\Eloquent\Builder;
use Filament\Forms\Components\RichEditor;
use Filament\Forms\Components\FileUpload;
use App\Filament\Resources\Panel\ProductResource\Pages;
use App\Filament\Resources\Panel\ProductResource\RelationManagers;

class ProductResource extends Resource
{
    protected static ?string $model = Product::class;

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

    protected static ?int $navigationSort = 6;

    protected static ?string $navigationGroup = 'البيانات الأساسيه';

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

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

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

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

                    TextInput::make('name')
                        ->required()
                        ->string()
                        ->unique('products', 'name', ignoreRecord: true),

                    TextInput::make('package')
                        ->required()
                        ->string(),

                    TextInput::make('purch_price')
                        ->nullable()
                        ->numeric()
                        ->step(1),

                    TextInput::make('sell_price')
                        ->nullable()
                        ->numeric()
                        ->step(1),

                    TextInput::make('stock')
                        ->nullable()
                        ->numeric()
                        ->step(1),

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

                    FileUpload::make('photo')
                        ->rules(['image'])
                        ->nullable()
                        ->maxSize(1024)
                        ->image()
                        ->imageEditor()
                        ->imageEditorAspectRatios([null, '16:9', '4:3', '1:1']),

                    // Select::make('sale_id')
                    //     ->required()
                    //     ->relationship('sale', 'date')
                    //     ->searchable()
                    //     ->preload()
                    //     ->native(false),

                    // Select::make('purchase_id')
                    //     ->required()
                    //     ->relationship('purchase', 'date')
                    //     ->searchable()
                    //     ->preload()
                    //     ->native(false),
                ])->columns(2),
            ]),
        ]);
    }

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

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

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

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

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

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

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

                ImageColumn::make('photo')->visibility('public'),

                // TextColumn::make('sale.date'),

                // TextColumn::make('purchase.date'),
            ])
            ->filters([])
            ->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\ListProducts::route('/'),
            'create' => Pages\CreateProduct::route('/create'),
            'view' => Pages\ViewProduct::route('/{record}'),
            'edit' => Pages\EditProduct::route('/{record}/edit'),
        ];
    }
}
