<?php
declare(strict_types=1);

namespace PpPhp\Core\Designer\DataGrid;

final class DesignerDataGridColumn
{
    public function __construct(
        private readonly string $name,
        private readonly string $type = 'text',
        private readonly string $label = '',
        private readonly array $options = [],
        private readonly array $style = []
    ) {}

    public static function create(string $name, string $type = 'text', string $label = '', array $options = []): self
    {
        return new self(
            $name,
            $type,
            $label !== '' ? $label : self::humanize($name),
            array_replace(self::defaultOptions($type), $options),
            self::defaultStyle($type)
        );
    }

    public static function fromArray(array $data): self
    {
        return new self(
            (string) ($data['name'] ?? $data['field'] ?? 'column'),
            (string) ($data['type'] ?? 'text'),
            (string) ($data['label'] ?? self::humanize((string) ($data['name'] ?? 'column'))),
            (array) ($data['options'] ?? []),
            (array) ($data['style'] ?? [])
        );
    }

    public function name(): string { return $this->name; }
    public function type(): string { return $this->type; }

    public function toArray(): array
    {
        return [
            'name' => $this->name,
            'field' => $this->name,
            'type' => $this->type,
            'label' => $this->label,
            'options' => $this->options,
            'style' => $this->style,
            'sortable' => (bool) ($this->options['sortable'] ?? true),
            'filterable' => (bool) ($this->options['filterable'] ?? true),
            'visible' => (bool) ($this->options['visible'] ?? true),
            'exportable' => (bool) ($this->options['exportable'] ?? true),
            'printable' => (bool) ($this->options['printable'] ?? true),
        ];
    }

    private static function defaultOptions(string $type): array
    {
        return [
            'sortable' => !in_array($type, ['image', 'avatar', 'icon'], true),
            'filterable' => !in_array($type, ['image', 'avatar', 'icon'], true),
            'visible' => true,
            'exportable' => !in_array($type, ['image', 'avatar', 'icon'], true),
            'printable' => true,
            'width' => match ($type) {
                'boolean', 'badge', 'icon' => '110px',
                'image', 'avatar' => '90px',
                'date', 'datetime' => '150px',
                default => 'auto',
            },
            'align' => in_array($type, ['number', 'currency', 'percentage'], true) ? 'right' : 'left',
        ];
    }

    private static function defaultStyle(string $type): array
    {
        return [
            'class' => 'ppd-grid-col-' . preg_replace('/[^a-z0-9_-]+/i', '-', $type),
        ];
    }

    private static function humanize(string $value): string
    {
        return ucwords(str_replace(['_', '-'], ' ', $value));
    }
}
