<?php

declare(strict_types=1);

namespace App\Filament\Support;

use App\Models\Product;
use Illuminate\Database\Eloquent\Model;

final class SelectOptionLabels
{
    public static function label(?string $value, Model $record, string $prefix = 'رکورد'): string
    {
        if (filled($value)) {
            return $value;
        }

        return $prefix . ' #' . $record->getKey();
    }

    public static function project(Model $record): string
    {
        return self::label($record->name ?? null, $record, 'پروژه');
    }

    public static function producer(Model $record): string
    {
        return self::label($record->brand_name ?? null, $record, 'خدمات‌دهنده');
    }

    public static function product(Model $record): string
    {
        return self::label($record->name ?? null, $record, 'محصول');
    }

    public static function category(Model $record): string
    {
        $value = $record->title ?? $record->name ?? null;

        return self::label(is_string($value) ? $value : null, $record, 'دسته‌بندی');
    }

    public static function user(Model $record): string
    {
        $name = method_exists($record, 'getFilamentName')
            ? $record->getFilamentName()
            : ($record->name ?? trim(((string) ($record->first_name ?? '')) . ' ' . ((string) ($record->last_name ?? ''))));

        if (filled($name)) {
            $contact = $record->cellphone ?? $record->email ?? '-';

            return $name . ' | ' . $contact;
        }

        return self::label(null, $record, 'کاربر');
    }

    public static function expert(Model $record): string
    {
        $value = $record->brand_name ?? $record->display_name ?? null;

        return self::label(is_string($value) ? $value : null, $record, 'متخصص');
    }

    public static function generic(Model $record, string $attribute, string $prefix = 'رکورد'): string
    {
        $value = $record->{$attribute} ?? null;

        return self::label(is_string($value) ? $value : (is_numeric($value) ? (string) $value : null), $record, $prefix);
    }

    /** @return array<int|string, string> */
    public static function productOptions(): array
    {
        return Product::query()
            ->get(['id', 'name'])
            ->mapWithKeys(fn (Product $product): array => [
                $product->id => self::product($product),
            ])
            ->all();
    }

    public static function mediaCategory(Model $record): string
    {
        return self::label($record->name ?? null, $record, 'دسته‌بندی');
    }

    /** @return array<int|string, string> */
    public static function mediaCategoryOptions($query): array
    {
        return $query
            ->get(['id', 'name'])
            ->mapWithKeys(fn (Model $category): array => [
                $category->getKey() => self::mediaCategory($category),
            ])
            ->all();
    }

    /** @return array<int|string, string> */
    public static function userOptions($query = null): array
    {
        $query ??= \App\Models\User::query();

        return $query
            ->get(['id', 'name', 'first_name', 'last_name', 'cellphone', 'email'])
            ->mapWithKeys(fn (Model $user): array => [
                $user->getKey() => self::user($user),
            ])
            ->all();
    }
}
