<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\File;

class ImagesController extends Controller
{
    public function jobs()
    {
        $path = resource_path('images/Jobs');
        
        if (!File::exists($path)) {
            return response()->json([]);
        }

        $files = File::files($path);
        
        $filenames = array_map(function ($file) {
            return $file->getFilename();
        }, $files);

        return response()->json($filenames);
    }

    public function show(Request $request, string $file)
    {
        try {
            return response()->file($this->imagePath($file));
        } catch (\Exception $e) {
            abort(404);
        }
    }

    public function subDirectory(Request $request, string $directory, string $file)
    {
        try {
            return response()->file($this->imagePath("{$directory}/{$file}"));
        } catch (\Exception $e) {
            abort(404);
        }
    }

    private function imagePath(string $file): string
    {
        return resource_path('images/'.$file);
    }
}
