<?php

namespace App\Http\Controllers;

use App\Models\UploadSiswaKelas;
use App\Traits\ApiResponseTrait;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;

class DownloadFileController extends Controller
{
    use ApiResponseTrait;

    public function filesiswakelas($id)
    {
        $upload = UploadSiswaKelas::findOrFail($id);

        // File disimpan di: storage/app/public/upload/siswa_kelas/namafile.xlsx
        $relativePath = str_replace('public/', '', $upload->path);
        $fullPath = storage_path('app/public/' . $relativePath);

        if (!file_exists($fullPath)) {
            return response()->json(['error' => 'File tidak ditemukan', 'path' => $fullPath], 404);
        }

        return response()->download($fullPath, $upload->original_name);
    }
}
