<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use App\Services\UploadService;
use App\Traits\ApiResponse;
use Illuminate\Http\Request;

class UploadController extends Controller
{
    use ApiResponse;

    protected $uploadService;

    public function __construct(UploadService $uploadService)
    {
        $this->uploadService = $uploadService;
    }

    public function uploadImage(Request $request)
    {
        $request->validate([
            'image' => 'required|image|max:5120',
            'type' => 'required|string'
        ]);

        try {
            $serverPath = $this->uploadService->uploadImage(
                $request->file('image'), 
                $request->type
            );

            return $this->successResponse(['server_path' => $serverPath ],'upload image success', 200);
        } catch (\Exception $e) {
            return $this->errorResponse('upload image failed', 500, $e->getMessage());
        }
    }
}
