<?php

declare(strict_types=1);

namespace App\GraphQL\Schema\Types;

use App\Contracts\GraphQL\EnumSort;
use App\Contracts\GraphQL\Fields\DeprecatedField;
use App\Contracts\GraphQL\Fields\DisplayableField;
use App\Contracts\GraphQL\Fields\FilterableField;
use App\GraphQL\Schema\Fields\Field;
use App\GraphQL\Schema\Fields\Relations\Relation;
use Illuminate\Support\Str;
use Rebing\GraphQL\Support\Type as RebingType;
use UnitEnum;

abstract class BaseType extends RebingType
{
    /**
     * @return array<string, mixed>
     */
    public function attributes(): array
    {
        return [
            'name' => $this->name(),
            'description' => $this->description(),
            'baseType' => $this,
        ];
    }

    /**
     * The name displayed of type.
     * By default, it will base on the class name without 'Type'.
     */
    public function name(): string
    {
        return Str::of(class_basename($this))
            ->remove('Type')
            ->__toString();
    }

    public function description(): string
    {
        return '';
    }

    /**
     * The relations of the type.
     *
     * @return Relation[]
     */
    public function relations(): array
    {
        return collect($this->fieldClasses())
            ->filter(fn (Field $field): bool => $field instanceof Relation)
            ->all();
    }

    /**
     * The fields of the type.
     *
     * @return Field[]
     */
    public function fieldClasses(): array
    {
        return [];
    }

    /**
     * Convert the fields to rebing resolve.
     *
     * @return array<string, array<string, mixed>>
     */
    public function fields(): array
    {
        $relations = collect($this->relations())
            ->mapWithKeys(fn (Relation $relation): array => [
                $relation->name() => [
                    'type' => $relation->type(),
                    'args' => $relation->args(),
                    'resolve' => $relation->resolve(...),
                    'deprecationReason' => $relation instanceof DeprecatedField ? $relation->deprecationReason() : $relation->deprecationReason,
                ],
            ]);

        $fields = collect($this->fieldClasses())
            ->reject(fn (Field $field): bool => $field instanceof Relation)
            ->filter(fn (Field $field): bool => $field instanceof DisplayableField && $field->canBeDisplayed())
            ->mapWithKeys(fn (Field $field): array => [
                $field->name() => [
                    'type' => $field->type(),
                    'description' => $field->description(),
                    'alias' => $field->getColumn(),
                    'args' => $field->args(),
                    'resolve' => $field->resolve(...),
                    'deprecationReason' => $field instanceof DeprecatedField ? $field->deprecationReason() : $field->deprecationReason,
                    'fieldClass' => $field,
                ],
            ]);

        return $fields->merge($relations)->all();
    }

    /**
     * @return class-string<UnitEnum&EnumSort>|null
     */
    public function getEnumSortClass(): ?string
    {
        return null;
    }

    public function hasFilterableColumns(): bool
    {
        return array_any($this->fieldClasses(), fn (Field $field): bool => $field instanceof FilterableField);
    }
}
