<?php

namespace App\Livewire\Forms\Studio\Master;

use App\Enums\CategoryType;
use App\Models\Category;
use App\Traits\Media\WithMediaHandler;
use App\Traits\Utilities\WithSortOrder;
use Illuminate\Support\Facades\DB;
use Illuminate\Validation\Rule;
use Livewire\Form;

class CategoryForm extends Form
{
    use WithMediaHandler, WithSortOrder;

    public ?Category $category = null;

    public string $name = '';

    public ?string $description = null;

    public int $type = CategoryType::PERFUME->value;

    public array $image = [];

    public function rules(): array
    {
        return [
            'name' => ['required', 'string', 'max:20', Rule::unique('categories', 'name')->whereNull('deleted_at')->ignore($this->category)],
            'description' => $this->type === CategoryType::PERFUME->value ? 'nullable' : 'prohibited',
            'type' => ['required', Rule::in(CategoryType::values())],
            'image' => $this->type === CategoryType::PERFUME->value ? 'array' : 'prohibited',
        ];
    }

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

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

        $this->name = $category->name;
        $this->description = $category->description;
        $this->type = $category->type->value;

        $this->image = $this->mapMediaCollection($category->getMedia('image'));
    }

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

        DB::transaction(function () {
            // Increment all existing categories' sort_order
            Category::query()->increment('sort_order');

            $category = Category::create([
                'name' => $this->name,
                'description' => $this->type === CategoryType::PERFUME->value ? $this->description : null,
                'type' => $this->type,
                'sort_order' => 1,
            ]);

            if ($this->type === CategoryType::PERFUME->value) {
                $this->uploadMedia($this->image, $category, 'image');
            }
        });
    }

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

        DB::transaction(function () {
            $this->category->update([
                'name' => $this->name,
                'description' => $this->type === CategoryType::PERFUME->value ? $this->description : null,
                'type' => $this->type,
            ]);

            if ($this->type === CategoryType::PERFUME->value) {
                $this->syncMedia($this->image, $this->category, 'image');
                $this->uploadMedia($this->image, $this->category, 'image');
            } else {
                $this->category->clearMediaCollection('image');
            }
        });
    }

    public function delete(): void
    {
        $this->category->delete();
        $this->adjustSortOrderAfterDelete($this->category);
    }

    protected function getModelClass(): string
    {
        return Category::class;
    }

    protected function getModelInstance(): Category
    {
        return $this->category;
    }
}
