<?php

declare(strict_types=1);

namespace App\Filament\Components\Form;

use Filament\Forms\Components\Hidden;
use Filament\Forms\Components\Repeater;
use Filament\Forms\Components\TagsInput;
use Filament\Forms\Components\TextInput;
use Filament\Forms\Components\Toggle;
use Filament\Schemas\Components\Grid;
use Illuminate\Support\Facades\Auth;

class ReviewsRepeater extends Repeater
{
    public static function create(): static
    {
        return parent::make('reviews')
            ->label('نظرات')
            ->relationship()
            ->schema([
                TextInput::make('desc')
                    ->label('متن نظر'),
                Grid::make(2)
                    ->schema([
                        TextInput::make('rate')
                            ->label('امتیاز')
                            ->numeric(),
                        Toggle::make('is_featured')
                            ->label('ویژه')
                            ->default(false),
                    ]),
                TagsInput::make('negative_points')
                    ->label('نکات منفی')
                    ->placeholder('نقطه ضعف را وارد کنید و Enter را بزنید')
                    ->afterStateHydrated(function ($component, $state) {
                        if (is_string($state)) {
                            $component->state(json_decode($state, true) ?? []);
                        }
                    })
                    ->dehydrateStateUsing(fn ($state) => json_encode($state)),
                TagsInput::make('positive_points')
                    ->label('نکات مثبت')
                    ->placeholder('نقطه قوت را وارد کنید و Enter را بزنید')
                    ->afterStateHydrated(function ($component, $state) {
                        if (is_string($state)) {
                            $component->state(json_decode($state, true) ?? []);
                        }
                    })
                    ->dehydrateStateUsing(fn ($state) => json_encode($state)),
                Hidden::make('created_by')
                    ->default(fn () => Auth::id()),
            ])
            ->collapsed()
            ->itemLabel(fn (array $state): ?string => $state['desc'] ?? null)
            ->columnSpan(12);
    }
}
