<?php

namespace App\Livewire\Forms\Studio\Catalog;

use App\Models\Category;
use Illuminate\Validation\Rule;
use Livewire\Form;

class CategoryForm extends Form
{
    public ?Category $category = null;

    public string $name = '';

    public ?string $description = null;

    public function rules(): array
    {
        return [
            'name' => ['required', 'string', 'max:20', Rule::unique('categories', 'name')->whereNull('deleted_at')->ignore($this->category)],
            'description' => ['nullable', 'string'],
        ];
    }

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

    public function setCategory(Category $category)
    {
        $this->category = $category;

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

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

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

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

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

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

        Category::where('sort_order', '>', $this->category->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 = Category::where('sort_order', '<', $this->category->sort_order)
                    ->orderByDesc('sort_order')
                    ->first();
            } else {
                $swap = Category::where('sort_order', '>', $this->category->sort_order)
                    ->orderBy('sort_order')
                    ->first();
            }

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

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