<?php

use App\Models\Category;
use Illuminate\Support\Str;
use Livewire\Component;
use Livewire\WithFileUploads; // Import for images
use Illuminate\Support\Facades\Storage; // Ensure this is imported

return new class extends Component
{
    use WithFileUploads;

    public $categories;
    public $name, $description, $meta_title, $meta_description;
    public $editingId, $deletingId;

    public $sortBy = 'id';
    public $sortDirection = 'asc';
    public $search = '';

    public $is_active = true;   
    public $is_featured = false;
    public $image;             
    public $existingImage;     
    public $showModal = false;
    public $showDeleteModal = false; 

    

    public function categories() {
        return Category::query()
        ->tap(fn ($query) => $this->sortBy ? $query->orderBy($this->sortBy, $this->sortDirection) : $query)
        ->when($this->search, function ($query) {
            $query->where('name', 'like', '%' . $this->search . '%')
                  ->orWhere('description', 'like', '%' . $this->search . '%');
        })
        ->paginate(25);
    }

    public function create() {
        $this->reset();
        $this->showModal = true;
    }

    public function sort($column) {
        if ($this->sortBy === $column) {
            $this->sortDirection = $this->sortDirection === 'asc' ? 'desc' : 'asc';
        } else {
            $this->sortBy = $column;
            $this->sortDirection = 'asc';
        }
    }

    public function edit($id) {
        $category = Category::find($id);
        if($category->is_active == 0){$is_active = false;}elseif($category->is_active == 1){$is_active = true;};
        if($category->is_featured == 0){$is_featured = false;}elseif($category->is_featured == 1){$is_featured = true;};

        $this->editingId = $id;
        $this->name = $category->name;
        $this->description = $category->description;
        $this->is_active = $is_active;
        $this->is_featured = $is_featured;
        $this->existingImage = $category->image;
        $this->meta_title = $category->meta_title;
        $this->meta_description = $category->meta_description;        
        $this->showModal = true;
    }

    public function save() {
        $this->validate([
            'name' => 'required|min:3', 
            'image' => 'nullable|image|max:2048'
        ]);

        $category = Category::updateOrCreate
        (['id' => $this->editingId], 
        [
            'name' => $this->name,
            'slug' => Str::slug($this->name),
            'description' => $this->description,
            'is_active' => $this->is_active,
            'is_featured' => $this->is_featured,
            'meta_title' => $this->meta_title,
            'meta_description' => $this->meta_description,
        ]);

        if ($this->image) {
            // Delete the file first
            if ($category->image) {
                Storage::disk('public')->delete($category->image);
            }
            $path = $this->image->store('categories', 'public');
            $category->update(['image' => $path]);
        }

        $this->reset();
        $this->dispatch('toast', 
            type: 'success', 
            text: 'Category saved Successfully!'
        );
        $this->categories();
    }

    public function confirmDelete($id) {
        $this->deletingId = $id;
        $this->showDeleteModal = true;
    }

    public function delete() {
        if ($this->deletingId) {
            $category = Category::find($this->deletingId);
        
            // Delete the file first
            if ($category->image) {
                Storage::disk('public')->delete($category->image);
            }

            $category->delete();
            $this->reset('deletingId', 'showDeleteModal');
            $this->dispatch('toast', 
                type: 'error', 
                text: 'Category Deleted Successfully!'
            );
            $this->categories();
        }
    }

    private function resetForm(){
        $this->reset(['name', 'description', 'image', 'is_active', 'is_featured', 'editingId','meta_title','meta_description', 'showModal']);
    }


    protected function view($data = [])
    {
        return app('view')->file('C:\laragon\www\ecommerce\storage\framework/views/livewire/views/ea64aae8.blade.php', $data);
    }
};

