<?php

namespace App\Livewire\Admin;

use Livewire\Component;
use App\Models\Slider;
use Livewire\WithPagination;
use Livewire\WithFileUploads;
use Illuminate\Support\Facades\Storage;

class SliderManagement extends Component
{
    use WithPagination, WithFileUploads;

    public $sliderId;
    public $title;
    public $description;
    public $image;
    public $existingImage;
    public $button_text;
    public $button_link;
    public $category;
    public $icon;
    public $color = 'from-blue-700 to-blue-900';
    public $order = 0;
    public $is_active = true;

    public $isEditing = false;
    public $showModal = false;
    public $search = '';
    public $perPage = 10;

    public $categories = [
        'Semua',
        'Pertanian',
        'Konstruksi',
        'TI',
        'Konsultasi',
        'Logistik',
        'Lainnya'
    ];

    public $colorOptions = [
        'from-blue-700 to-blue-900' => 'Biru',
        'from-green-700 to-green-900' => 'Hijau',
        'from-orange-700 to-orange-900' => 'Oranye',
        'from-purple-700 to-purple-900' => 'Ungu',
        'from-red-700 to-red-900' => 'Merah',
        'from-teal-700 to-teal-900' => 'Teal',
        'from-indigo-700 to-indigo-900' => 'Indigo',
        'from-pink-700 to-pink-900' => 'Pink',
    ];

    public $iconOptions = [
        'fa-file-contract' => 'Kontrak',
        'fa-gavel' => 'Gavel',
        'fa-building' => 'Bangunan',
        'fa-database' => 'Database',
        'fa-tractor' => 'Traktor',
        'fa-hard-hat' => 'Helm',
        'fa-laptop-code' => 'Laptop',
        'fa-handshake' => 'Jabat Tangan',
        'fa-truck' => 'Truk',
        'fa-chart-line' => 'Grafik',
    ];

    protected function rules()
    {
        return [
            'title' => 'required|string|max:255',
            'description' => 'nullable|string|max:500',
            'image' => 'nullable|image|max:2048',
            'button_text' => 'nullable|string|max:50',
            'button_link' => 'nullable|string|max:255',
            'category' => 'nullable|string|max:50',
            'icon' => 'nullable|string|max:50',
            'color' => 'nullable|string|max:50',
            'order' => 'nullable|integer|min:0',
            'is_active' => 'boolean',
        ];
    }

    public function mount()
    {
        $this->resetForm();
        if (!Storage::disk('public')->exists('sliders')) {
            Storage::disk('public')->makeDirectory('sliders');
        }
    }

    public function resetForm()
    {
        $this->sliderId = null;
        $this->title = '';
        $this->description = '';
        $this->image = null;
        $this->existingImage = null;
        $this->button_text = '';
        $this->button_link = '';
        $this->category = 'Semua';
        $this->icon = 'fa-file-contract';
        $this->color = 'from-blue-700 to-blue-900';
        $this->order = 0;
        $this->is_active = true;
        $this->isEditing = false;
        $this->showModal = false;
    }

    public function create()
    {
        $this->resetForm();
        $this->showModal = true;
        $this->isEditing = false;
    }

    public function edit($id)
    {
        $slider = Slider::findOrFail($id);
        $this->sliderId = $slider->id;
        $this->title = $slider->title;
        $this->description = $slider->description;
        $this->existingImage = $slider->image;
        $this->button_text = $slider->button_text;
        $this->button_link = $slider->button_link;
        $this->category = $slider->category ?? 'Semua';
        $this->icon = $slider->icon ?? 'fa-file-contract';
        $this->color = $slider->color ?? 'from-blue-700 to-blue-900';
        $this->order = $slider->order ?? 0;
        $this->is_active = $slider->is_active;
        $this->isEditing = true;
        $this->showModal = true;
    }

    public function store()
    {
        $this->validate();
        $imagePath = null;
        if ($this->image) {
            $imagePath = $this->image->store('sliders', 'public');
        }

        Slider::create([
            'title' => $this->title,
            'description' => $this->description,
            'image' => $imagePath,
            'button_text' => $this->button_text,
            'button_link' => $this->button_link,
            'category' => $this->category,
            'icon' => $this->icon,
            'color' => $this->color,
            'order' => $this->order,
            'is_active' => $this->is_active,
        ]);

        session()->flash('message', 'Slider berhasil ditambahkan! ✅');
        $this->resetForm();
        $this->showModal = false;
    }

    public function update()
    {
        $this->validate();
        $slider = Slider::findOrFail($this->sliderId);
        $imagePath = $this->existingImage;

        if ($this->image) {
            if ($imagePath && Storage::disk('public')->exists($imagePath)) {
                Storage::disk('public')->delete($imagePath);
            }
            $imagePath = $this->image->store('sliders', 'public');
        }

        $slider->update([
            'title' => $this->title,
            'description' => $this->description,
            'image' => $imagePath,
            'button_text' => $this->button_text,
            'button_link' => $this->button_link,
            'category' => $this->category,
            'icon' => $this->icon,
            'color' => $this->color,
            'order' => $this->order,
            'is_active' => $this->is_active,
        ]);

        session()->flash('message', 'Slider berhasil diperbarui! ✅');
        $this->resetForm();
        $this->showModal = false;
    }

    public function delete($id)
    {
        $slider = Slider::findOrFail($id);
        if ($slider->image && Storage::disk('public')->exists($slider->image)) {
            Storage::disk('public')->delete($slider->image);
        }
        $slider->delete();
        session()->flash('message', 'Slider berhasil dihapus! 🗑️');
    }

    public function toggleActive($id)
    {
        $slider = Slider::findOrFail($id);
        $slider->update(['is_active' => !$slider->is_active]);
        session()->flash('message', 'Status slider berhasil diubah! 🔄');
    }

    public function updatingSearch()
    {
        $this->resetPage();
    }

    public function render()
    {
        $sliders = Slider::when($this->search, function ($query) {
            return $query->where('title', 'like', '%' . $this->search . '%')
                ->orWhere('description', 'like', '%' . $this->search . '%');
        })
            ->orderBy('order', 'asc')
            ->paginate($this->perPage);

        return view('livewire.admin.slidermanagement', [
            'sliders' => $sliders,
        ])->layout('layouts.slider');
    }
}
