<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Http\Requests\StoreWhatWeDoImageRequest;
use App\Services\WhatWeDoImageService;

class WhatWeDoImageController extends Controller
{
    private WhatWeDoImageService $whatWeDoImageService;

    public function __construct(WhatWeDoImageService $whatWeDoImageService)
    {
        $this->whatWeDoImageService = $whatWeDoImageService;
    }

    /**
     * Display the What-We-Do image management settings page.
     * Shows all 9 card slots with current image previews and upload forms.
     * Requirements: 8.1, 8.2, 8.3
     */
    public function index()
    {
        $slots = $this->whatWeDoImageService->getAllSlots();

        return view('admin.what-we-do-images.index', compact('slots'));
    }

    /**
     * Handle an image upload for a specific card slot.
     * Delegates storage to WhatWeDoImageService, then redirects with flash.
     * Requirements: 8.4, 8.5
     */
    public function update(StoreWhatWeDoImageRequest $request, int $slot)
    {
        try {
            $this->whatWeDoImageService->updateSlotImage($slot, $request->file('image'));

            return redirect()
                ->route('admin.what-we-do-images.index')
                ->with('success', 'Image updated successfully.');
        } catch (\Exception $e) {
            return redirect()
                ->route('admin.what-we-do-images.index')
                ->with('error', 'Failed to upload image. Please try again.');
        }
    }
}
