<?php

declare(strict_types=1);

namespace App\Filament\Components\Form;

use App\Models\Bid;
use App\Models\User;
use Filament\Forms\Components\Repeater;

class BidsRepeater extends Repeater
{
    public static function create(): static
    {
        return parent::make('bids')
            ->relationship()
            ->label('پیشنهادها')
            ->collapsed()
            ->itemLabel(
                function (array $state): ?string {
                    $price = $state['price'] ?? '';
                    $duration = $state['duration'] ?? '';
                    $userId = $state['user_id'] ?? null;

                    $userName = '';
                    if ($userId) {
                        $user = User::find($userId);
                        $userName = $user ? $user->name : '';
                    }

                    return $price . ' تومان | ' . $duration . ' روز | ' . $userName . ' | ' . $state['status'];
                }
            )
            ->schema([
                // Project details row
                PriceTextInput::create()
                    ->columnSpan(3),
                DurationTextInput::create()
                    ->columnSpan(3),
                StatusSelect::create(Bid::class)
                    ->columnSpan(3),
                SelectedAtDateTimePicker::create()
                    ->columnSpan(3)
                    ->default(now()),

                // User assignment row
                UserSelect::create()
                    ->columnSpan(6),
                CreatedBySelect::create()
                    ->columnSpan(6),
            ])
            ->columns(12)
            ->columnSpan(12);
    }
}
