<?php

namespace App\Filament\Resources;

use App\Filament\Resources\ProductResource\Pages;
use App\Filament\Resources\ProductResource\RelationManagers;
use App\Models\Product;
use App\Support\TranslatableFields;
use Filament\Forms;
use Filament\Forms\Components\SpatieMediaLibraryFileUpload;
use Filament\Forms\Form;
use Filament\Resources\Resource;
use Filament\Tables;
use Filament\Tables\Columns\SpatieMediaLibraryImageColumn;
use Filament\Tables\Table;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\SoftDeletingScope;
use Filament\Forms\Components\TextInput;
use Filament\Forms\Components\Textarea;
use Filament\Forms\Components\Select;
use Filament\Tables\Columns\TextColumn;
use Filament\Tables\Filters\SelectFilter;

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

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

    protected static ?int $navigationSort = 3;

    public static function form(Form $form): Form
    {
        return $form->schema([
            TranslatableFields::tabs([
                ['text', 'name'],
                ['textarea', 'description', ['rows' => 3]],
            ]),

            Select::make('category_id')
                ->relationship('category', 'name')
                ->label('Category')
                ->required()
                ->preload(),

            // For tags relationship with Tag
            Select::make('tags')
                ->relationship('tags', 'name')
                ->multiple()
                ->preload(),

            // For optionTypes relationship (hasMany Option)
            Select::make('options')
                ->relationship('options', 'name')
                ->multiple()
                ->preload()
                ->disabled() // hasMany - display only, not editable in select
                ->helperText('Options are managed elsewhere'),

            // For images
            SpatieMediaLibraryFileUpload::make('images')
                ->collection('images')
                ->multiple()
                ->reorderable()
                ->image()
                // ->imagePreviewHeight('80') // (optional) set preview size
                // ->panelAspectRatio('1:1')  // (optional) square preview
                ->downloadable()
                ->openable()
                ->responsiveImages() // enables responsive thumbnails if configured
                ->label('Product Images'),
        ]);
    }


    public static function table(Table $table): Table
    {
        return $table
            ->columns([
                TextColumn::make('name')->searchable()->sortable(),
                TextColumn::make('category.name')->label('Category')->sortable(),
                TextColumn::make('description')->limit(50)->wrap(),
                TextColumn::make('tags.name')
                    ->label('Tags')
                    ->badge()
                    ->separator(', ')
                    ->limit(3),
                SpatieMediaLibraryImageColumn::make('images')->collection('images'),
            ])
            ->filters([
                SelectFilter::make('category_id')
                    ->label('Category')
                    ->relationship('category', 'name'),
            ])
            ->defaultSort('name')
            ->actions([
                // Tables\Actions\EditAction::make(),
            ])
            ->bulkActions([
                Tables\Actions\DeleteBulkAction::make(),
            ]);
    }


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

    public static function getPages(): array
    {
        return [
            'index' => Pages\ListProducts::route('/'),
            'create' => Pages\CreateProduct::route('/create'),
            'edit' => Pages\EditProduct::route('/{record}/edit'),
        ];
    }
}
