<?php

declare(strict_types=1);

namespace App\Filament\Components\Form;

use App\Models\Variant;
use Filament\Actions\Action;
use Filament\Forms\Components\Repeater;
use Filament\Schemas\Components\Actions;

class VariationsRepeater extends Repeater
{
    public static function create(): static
    {
        return parent::make('variations')
            ->hiddenLabel()
            ->relationship()
            ->schema([
                Actions::make([
                    Action::make('view_product')
                        ->label('مشاهده در ویانا')
                        ->icon('heroicon-m-arrow-top-right-on-square')
                        ->url(fn ($record) => 'https://vianasystem.com/products/' . $record->variant?->product_code)
                        ->extraAttributes([
                            'style' => 'background-color: #6b46c1; color: white;',
                        ])
                        ->openUrlInNewTab(),
                    Action::make('view_product')
                        ->label('مشاهده در ای‌ویز')
                        ->icon('heroicon-m-arrow-top-right-on-square')
                        ->url(fn ($record) => 'https://panel.eways.co/Store/Detail/0/' . $record->variant?->product_code)
                        ->extraAttributes([
                            'style' => 'background-color: #f97316; color: white;',
                        ])
                        ->openUrlInNewTab(),
                ]),
            ])
            ->columns(2)
            ->collapsed()
            ->itemLabel(function (array $state): ?string {
                if (! $state['variant_id']) {
                    return null;
                }

                $variant = Variant::find($state['variant_id']);
                $count = $state['count'];
                $price = $state['price'];

                return "{$count} عدد | {$variant?->display_name} | به قیمت {$price}";
            })
            ->columnSpan(12);
    }
}
