<?php

namespace App\Http\Controllers;

use App\Http\Requests\MediaUploadRequest;
use App\Services\MediaUploadService;
use Illuminate\Http\JsonResponse;

class MediaUploadController extends Controller
{
    public function __construct(
        private readonly MediaUploadService $service,
    ) {}

    public function store(MediaUploadRequest $request): JsonResponse
    {
        $media = $this->service->store(
            $request->user(),
            $request->file('file'),
            $request->validated('collection') ?? 'content',
        );

        return response()->json([
            'id' => $media->id,
            'name' => $media->name,
            'file_name' => $media->file_name,
            'mime_type' => $media->mime_type,
            'size' => $media->size,
            'url' => $media->getUrl(),
        ]);
    }
}
