<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use App\Models\InstallRequest;
use App\Models\InstallRequestPhoto;
use Illuminate\Http\Request;
use Illuminate\Http\JsonResponse;

class InstallRequestPhotoController extends Controller
{
    // Загрузка фото монтажником
    public function upload(Request $request, InstallRequest $installRequest): JsonResponse
    {
        $request->validate([
            'photo' => 'required|image|max:5120', // максимум 5MB
            'type' => 'nullable|string|max:255',  // тип фото (по желанию)
        ]);

        $file = $request->file('photo');
        $path = $file->store('install_requests', 'public');

        $photo = InstallRequestPhoto::create([
            'install_request_id' => $installRequest->id,
            'uploaded_by' => $request->user()->id,
            'path' => $path,
            'type' => $request->input('type'),
        ]);

        return response()->json($photo, 201);
    }

    // Получение фото конкретной заявки
    public function index(InstallRequest $installRequest): JsonResponse
    {
        $photos = $installRequest->photos()->with('uploader')->get();
        return response()->json($photos);
    }
}
