<?php

namespace App\Http\Controllers\api;

use Exception;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class FileController extends Controller
{
    private $MESSAGES = [
        'DOWNLOAD' => 'WARNING.DOWNLOAD'
    ];

    public function download(Request $request, $filename)
    {
        try {
            $filePath = storage_path('app/files/' . $filename . '.xlsx');

            if (file_exists($filePath)) {
                return response()->download($filePath);
            }

            return response()->json(['error' => 'File not found'], 404);
        } catch (Exception $ex) {
            return $this->responseJson($this->MESSAGES['DOWNLOAD'], null, 501);
        }
    }
}
