<?php

namespace App\Livewire\Website;

use App\Domain\Website\Models\WebsiteTestimonial;
use App\Domain\Website\Services\WebsiteCacheService;
use Livewire\Attributes\Layout;
use Livewire\Attributes\Title;
use Livewire\Component;

#[Layout('layouts.app')]
#[Title('آراء العملاء')]
class TestimonialManager extends Component
{
    public bool $showForm = false;
    public ?int $editingId = null;

    public string $name = '';
    public string $name_en = '';
    public string $role = '';
    public string $role_en = '';
    public string $content = '';
    public string $content_en = '';
    public int $rating = 5;
    public bool $is_featured = true;

    public function mount(): void
    {
        $this->authorize('settings.manage');
    }

    public function create(): void
    {
        $this->reset(['name', 'name_en', 'role', 'role_en', 'content', 'content_en', 'rating', 'is_featured', 'editingId']);
        $this->is_featured = true;
        $this->rating = 5;
        $this->showForm = true;
    }

    public function edit(int $id): void
    {
        $testimonial = WebsiteTestimonial::find($id);
        if (!$testimonial) return;

        $this->editingId = $id;
        $this->name = $testimonial->name;
        $this->name_en = $testimonial->name_en ?? '';
        $this->role = $testimonial->role ?? '';
        $this->role_en = $testimonial->role_en ?? '';
        $this->content = $testimonial->content;
        $this->content_en = $testimonial->content_en ?? '';
        $this->rating = $testimonial->rating;
        $this->is_featured = $testimonial->is_featured;
        $this->showForm = true;
    }

    public function save(): void
    {
        $this->validate([
            'name' => 'required|string|max:255',
            'content' => 'required|string|max:1000',
            'rating' => 'required|integer|min:1|max:5',
        ]);

        $data = [
            'name' => $this->name,
            'name_en' => $this->name_en ?: null,
            'role' => $this->role ?: null,
            'role_en' => $this->role_en ?: null,
            'content' => $this->content,
            'content_en' => $this->content_en ?: null,
            'rating' => $this->rating,
            'is_featured' => $this->is_featured,
        ];

        if ($this->editingId) {
            WebsiteTestimonial::find($this->editingId)->update($data);
        } else {
            WebsiteTestimonial::create($data);
        }

        app(WebsiteCacheService::class)->invalidate(app('current_academy')->id);
        $this->showForm = false;
        session()->flash('success', __('تم الحفظ بنجاح'));
    }

    public function delete(int $id): void
    {
        WebsiteTestimonial::find($id)?->delete();
        app(WebsiteCacheService::class)->invalidate(app('current_academy')->id);
        session()->flash('success', __('تم الحذف'));
    }

    public function render()
    {
        $testimonials = WebsiteTestimonial::orderBy('sort_order')->get();

        return view('livewire.website.testimonial-manager', [
            'testimonials' => $testimonials,
        ]);
    }
}
