<?php

namespace App\Filament\Resources\ProductResource\RelationManagers;

use App\Models\Supplier;
use Filament\Actions;
use Filament\Forms;
use Filament\Resources\RelationManagers\RelationManager;
use Filament\Schemas\Schema;
use Filament\Tables;
use Filament\Tables\Table;

class VariantsRelationManager extends RelationManager
{
    protected static string $relationship = 'variants';

    protected static ?string $title = 'النقشات (Variants)';

    protected static ?string $modelLabel = 'نقشة';

    public function form(Schema $schema): Schema
    {
        return $schema
            ->components([
                Forms\Components\FileUpload::make('image')
                    ->label('صورة النقشة')
                    ->image()
                    ->directory('variants')
                    ->disk('public')
                    ->imageResizeMode('cover')
                    ->imageResizeTargetWidth('600')
                    ->imageResizeTargetHeight('600')
                    ->columnSpanFull(),

                Forms\Components\TextInput::make('pattern_name')
                    ->label('اسم النقشة')
                    ->required()
                    ->maxLength(255),

                Forms\Components\TextInput::make('color')
                    ->label('اللون')
                    ->maxLength(100),

                Forms\Components\TextInput::make('sku')
                    ->label('كود المنتج (SKU)')
                    ->required()
                    ->unique(ignoreRecord: true)
                    ->maxLength(50),

                Forms\Components\TextInput::make('stock')
                    ->label('المخزون')
                    ->numeric()
                    ->default(0)
                    ->minValue(0),

                Forms\Components\TextInput::make('cost_price')
                    ->label('سعر التكلفة (ج.م)')
                    ->numeric()
                    ->prefix('ج.م')
                    ->required(),

                Forms\Components\Select::make('default_supplier_id')
                    ->label('المورد الافتراضي')
                    ->options(Supplier::where('is_active', true)->pluck('name', 'id'))
                    ->searchable()
                    ->nullable(),

                Forms\Components\TextInput::make('lead_time_days')
                    ->label('مدة التوريد (أيام)')
                    ->numeric()
                    ->default(0)
                    ->minValue(0),

                Forms\Components\Toggle::make('is_active')
                    ->label('نشط')
                    ->default(true),
            ]);
    }

    public function table(Table $table): Table
    {
        return $table
            ->recordTitleAttribute('pattern_name')
            ->columns([
                Tables\Columns\ImageColumn::make('image')
                    ->label('الصورة')
                    ->disk('public')
                    ->getStateUsing(fn ($record) => $record->image_thumbnail ?? $record->image)
                    ->size(50)
                    ->circular()
                    ->defaultImageUrl(fn () => 'https://ui-avatars.com/api/?name=V&background=10b981&color=fff&size=50'),

                Tables\Columns\TextColumn::make('pattern_name')
                    ->label('النقشة')
                    ->searchable()
                    ->sortable(),

                Tables\Columns\TextColumn::make('color')
                    ->label('اللون'),

                Tables\Columns\TextColumn::make('sku')
                    ->label('SKU')
                    ->searchable(),

                Tables\Columns\TextColumn::make('stock')
                    ->label('المخزون')
                    ->sortable()
                    ->badge()
                    ->color(fn (int $state): string => match (true) {
                        $state === 0 => 'danger',
                        $state <= 10 => 'warning',
                        default => 'success',
                    }),

                Tables\Columns\TextColumn::make('cost_price')
                    ->label('التكلفة (ج.م)')
                    ->money('EGP'),

                Tables\Columns\TextColumn::make('defaultSupplier.name')
                    ->label('المورد')
                    ->placeholder('—'),

                Tables\Columns\IconColumn::make('is_active')
                    ->label('نشط')
                    ->boolean(),
            ])
            ->filters([
                Tables\Filters\TernaryFilter::make('is_active')
                    ->label('الحالة'),
            ])
            ->headerActions([
                Actions\CreateAction::make()->label('إضافة نقشة'),
            ])
            ->actions([
                Actions\EditAction::make()->label('تعديل'),
                Actions\DeleteAction::make()->label('حذف'),
            ])
            ->bulkActions([
                Actions\BulkActionGroup::make([
                    Actions\DeleteBulkAction::make()->label('حذف المحدد'),
                ]),
            ]);
    }
}
