<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\HomepageTestimonial;
use App\Services\AdminActivityLogger;
use Illuminate\Contracts\View\View;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;

class HomepageTestimonialController extends Controller
{
    public function index(): View
    {
        return view('admin.cms.testimonials', [
            'testimonials' => HomepageTestimonial::query()
                ->orderBy('sort_order')
                ->orderBy('id')
                ->get(),
        ]);
    }

    public function store(Request $request, AdminActivityLogger $logger): RedirectResponse
    {
        $validated = $request->validate([
            'name' => ['required', 'string', 'max:191'],
            'role' => ['nullable', 'string', 'max:191'],
            'message' => ['required', 'string', 'max:2000'],
            'rating' => ['nullable', 'integer', 'min:1', 'max:5'],
            'image' => ['nullable', 'image', 'max:2048'],
            'sort_order' => ['nullable', 'integer', 'min:0'],
            'is_active' => ['nullable', 'boolean'],
        ]);

        $path = null;
        if ($request->hasFile('image')) {
            $path = $request->file('image')->store('homepage/testimonials', 'public');
        }

        $testimonial = HomepageTestimonial::query()->create([
            'name' => $validated['name'],
            'role' => $validated['role'] ?? null,
            'message' => $validated['message'],
            'rating' => (int) ($validated['rating'] ?? 5),
            'image_path' => $path,
            'sort_order' => (int) ($validated['sort_order'] ?? 0),
            'is_active' => (bool) ($validated['is_active'] ?? true),
        ]);

        $logger->log('homepage_testimonial.create', HomepageTestimonial::class, $testimonial->id);

        return back()->with('status', 'Testimonial added.');
    }

    public function update(
        Request $request,
        HomepageTestimonial $testimonial,
        AdminActivityLogger $logger
    ): RedirectResponse {
        $validated = $request->validate([
            'name' => ['required', 'string', 'max:191'],
            'role' => ['nullable', 'string', 'max:191'],
            'message' => ['required', 'string', 'max:2000'],
            'rating' => ['nullable', 'integer', 'min:1', 'max:5'],
            'image' => ['nullable', 'image', 'max:2048'],
            'sort_order' => ['nullable', 'integer', 'min:0'],
            'is_active' => ['nullable', 'boolean'],
        ]);

        if ($request->hasFile('image')) {
            $testimonial->image_path = $request->file('image')->store('homepage/testimonials', 'public');
        }

        $testimonial->name = $validated['name'];
        $testimonial->role = $validated['role'] ?? null;
        $testimonial->message = $validated['message'];
        $testimonial->rating = (int) ($validated['rating'] ?? 5);
        $testimonial->sort_order = (int) ($validated['sort_order'] ?? 0);
        $testimonial->is_active = (bool) ($validated['is_active'] ?? false);
        $testimonial->save();

        $logger->log('homepage_testimonial.update', HomepageTestimonial::class, $testimonial->id);

        return back()->with('status', 'Testimonial updated.');
    }

    public function destroy(HomepageTestimonial $testimonial, AdminActivityLogger $logger): RedirectResponse
    {
        $testimonial->delete();
        $logger->log('homepage_testimonial.delete', HomepageTestimonial::class, $testimonial->id);

        return back()->with('status', 'Testimonial deleted.');
    }
}
