<?php

namespace App\Observers;

use App\Facades\Settings;
use Mtc\MercuryDataModels\VehicleAttribute;

class VehicleAttributeObserver
{
    public function updating(VehicleAttribute $vehicleAttribute): void
    {
        if ($vehicleAttribute->isDirty('type')) {
            $this->handleTypeChange($vehicleAttribute);
        }

        if ($vehicleAttribute->isDirty('sortable')) {
            $this->handleSortableChange($vehicleAttribute);
        }

        if ($vehicleAttribute->isDirty('filterable')) {
            $this->handleFilterableChange($vehicleAttribute);
        }
    }

    protected function handleTypeChange(VehicleAttribute $vehicleAttribute): void
    {
        $oldColumn = $this->columnForType($vehicleAttribute->getOriginal('type'));
        $newColumn = $this->columnForType($vehicleAttribute->type);

        if ($oldColumn === $newColumn) {
            // no change
            return;
        }

        $vehicleAttribute->values()->chunkById(100, function ($values) use ($oldColumn, $newColumn, $vehicleAttribute) {
            foreach ($values as $value) {
                if (!is_null($value->$oldColumn)) {
                    $value->$newColumn = $value->$oldColumn;
                }
                $value->type = $vehicleAttribute->type;
                $value->saveQuietly();
            }
        });
    }

    private function columnForType(string $type): string
    {
        return match ($type) {
            'text', 'textarea', 'editor' => 'value_text',
            'number' => 'value_float',
            'integer', 'boolean' => 'value_integer',
            'datetime' => 'value_datetime',
            default => 'value',
        };
    }

    protected function handleSortableChange(VehicleAttribute $vehicleAttribute): void
    {
        if (in_array($vehicleAttribute->type, ['integer', 'number'])) {
            $this->handleNumericSortableChange($vehicleAttribute);
            return;
        }

        $configKey = 'automotive-vehicle-sorting-' . $vehicleAttribute->slug;

        if ($vehicleAttribute->sortable) {
            if (!Settings::exists($configKey)) {
                Settings::make([
                    'tab' => 'Automotive',
                    'section' => 'Filter',
                    'group' => 'Sorting',
                    'name' => $vehicleAttribute->name,
                    'config_key' => $configKey,
                    'type' => 'boolean',
                    'value' => true,
                ]);
            }
        } else {
            Settings::delete($configKey);
        }
    }

    protected function handleNumericSortableChange(VehicleAttribute $vehicleAttribute): void
    {
        $directions = [
            'asc' => '(Low > High)',
            'desc' => '(High > Low)',
        ];

        foreach ($directions as $direction => $label) {
            $configKey = 'automotive-vehicle-sorting-' . $vehicleAttribute->slug . '-' . $direction;

            if ($vehicleAttribute->sortable) {
                if (!Settings::exists($configKey)) {
                    Settings::make([
                        'tab' => 'Automotive',
                        'section' => 'Filter',
                        'group' => 'Sorting',
                        'name' => $vehicleAttribute->name . ' ' . $label,
                        'config_key' => $configKey,
                        'type' => 'boolean',
                        'value' => true,
                    ]);
                }
            } else {
                Settings::delete($configKey);
            }
        }
    }

    protected function handleFilterableChange(VehicleAttribute $vehicleAttribute)
    {
        $configKey = 'automotive-vehicle-filters-' . $vehicleAttribute->slug;

        if ($vehicleAttribute->filterable) {
            if (!Settings::exists($configKey)) {
                Settings::make([
                    'tab' => 'Automotive',
                    'section' => 'Filter',
                    'group' => 'Filtering',
                    'name' => $vehicleAttribute->name,
                    'config_key' => $configKey,
                    'type' => 'boolean',
                    'value' => true,
                ]);
            }
        } else {
            Settings::delete($configKey);
        }
    }
}
