<?php

namespace App\Livewire\Forms\Studio\Feature;

use App\Models\PerfumeFeature;
use Livewire\Form;

class PerfumeFeatureForm extends Form
{
    public ?PerfumeFeature $perfumeFeature = null;

    public string $name = '';

    public string $description = '';

    public function rules(): array
    {
        return [
            'name' => ['required', 'string', 'max:50'],
            'description' => ['required', 'string'],
        ];
    }

    public function validationAttributes(): array
    {
        return [
            'name' => 'nama',
            'description' => 'keterangan',
        ];
    }

    public function setPerfumeFeature(PerfumeFeature $perfumeFeature)
    {
        $this->perfumeFeature = $perfumeFeature;

        $this->name = $perfumeFeature->name;
        $this->description = $perfumeFeature->description;
    }

    public function store()
    {
        $this->validate();

        PerfumeFeature::create([
            'name' => $this->name,
            'description' => $this->description,
            'sort_order' => PerfumeFeature::count() + 1,
        ]);
    }

    public function update()
    {
        $this->validate();

        $this->perfumeFeature->update([
            'name' => $this->name,
            'description' => $this->description,
        ]);
    }

    public function delete()
    {
        $this->perfumeFeature->delete();

        PerfumeFeature::where('sort_order', '>', $this->perfumeFeature->sort_order)
            ->orderBy('sort_order')
            ->get()
            ->each(function ($cat) {
                $cat->update(['sort_order' => $cat->sort_order - 1]);
            });
    }

    public function sortOrder(string $direction)
    {
        if (in_array($direction, ['up', 'down'])) {
            $swap = null;

            if ($direction === 'up') {
                $swap = PerfumeFeature::where('sort_order', '<', $this->perfumeFeature->sort_order)
                    ->orderByDesc('sort_order')
                    ->first();
            } else {
                $swap = PerfumeFeature::where('sort_order', '>', $this->perfumeFeature->sort_order)
                    ->orderBy('sort_order')
                    ->first();
            }

            if ($swap) {
                $temp = $this->perfumeFeature->sort_order;
                $this->perfumeFeature->update(['sort_order' => $swap->sort_order]);
                $swap->update(['sort_order' => $temp]);
            }
        } elseif ($direction === 'first') {
            $this->perfumeFeature->update(['sort_order' => PerfumeFeature::min('sort_order') - 1]);
            $this->normalizeSortOrder();
        } elseif ($direction === 'last') {
            $this->perfumeFeature->update(['sort_order' => PerfumeFeature::max('sort_order') + 1]);
            $this->normalizeSortOrder();
        }
    }

    protected function normalizeSortOrder()
    {
        $perfumeFeatures = PerfumeFeature::orderBy('sort_order')->get();
        foreach ($perfumeFeatures as $index => $perfumeFeature) {
            $perfumeFeature->update(['sort_order' => $index + 1]);
        }
    }
}
