<?php

namespace App\Http\Controllers\Admin;

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

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

    public function store(Request $request, AdminActivityLogger $logger): RedirectResponse
    {
        $validated = $request->validate([
            'name' => ['required', 'string', 'max:191'],
            'city' => ['nullable', 'string', 'max:120'],
            'rank' => ['nullable', 'string', 'max:120'],
            'badge' => ['nullable', 'string', 'max:120'],
            '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/achievers', 'public');
        }

        $achiever = HomepageAchiever::query()->create([
            'name' => $validated['name'],
            'city' => $validated['city'] ?? null,
            'rank' => $validated['rank'] ?? null,
            'badge' => $validated['badge'] ?? null,
            'image_path' => $path,
            'sort_order' => (int) ($validated['sort_order'] ?? 0),
            'is_active' => (bool) ($validated['is_active'] ?? true),
        ]);

        $logger->log('homepage_achiever.create', HomepageAchiever::class, $achiever->id);

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

    public function update(Request $request, HomepageAchiever $achiever, AdminActivityLogger $logger): RedirectResponse
    {
        $validated = $request->validate([
            'name' => ['required', 'string', 'max:191'],
            'city' => ['nullable', 'string', 'max:120'],
            'rank' => ['nullable', 'string', 'max:120'],
            'badge' => ['nullable', 'string', 'max:120'],
            'image' => ['nullable', 'image', 'max:2048'],
            'sort_order' => ['nullable', 'integer', 'min:0'],
            'is_active' => ['nullable', 'boolean'],
        ]);

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

        $achiever->name = $validated['name'];
        $achiever->city = $validated['city'] ?? null;
        $achiever->rank = $validated['rank'] ?? null;
        $achiever->badge = $validated['badge'] ?? null;
        $achiever->sort_order = (int) ($validated['sort_order'] ?? 0);
        $achiever->is_active = (bool) ($validated['is_active'] ?? false);
        $achiever->save();

        $logger->log('homepage_achiever.update', HomepageAchiever::class, $achiever->id);

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

    public function destroy(HomepageAchiever $achiever, AdminActivityLogger $logger): RedirectResponse
    {
        $achiever->delete();
        $logger->log('homepage_achiever.delete', HomepageAchiever::class, $achiever->id);

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