<?php

namespace Wave\Http\Controllers\API;

use App\Http\Controllers\Controller;
use App\Mail\ForgetPasswordOtp;
use App\Models\User;
use Carbon\Carbon;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Mail;
use Illuminate\Support\Facades\Notification;
use Illuminate\Support\Facades\Validator;
use Illuminate\Support\Str;
use Tymon\JWTAuth\Facades\JWTAuth;
use Wave\ApiKey;
use Wave\Notifications\VerifyEmail;
use Illuminate\Support\Facades\Storage;

class FileController extends Controller
{
    /**
     * Create a new AuthController instance.
     *
     * @return void
     */
    public function __construct()
    {
        // $this->middleware('auth:api');
    }

    public function storeFile(Request $request)
    {
        if ($request->hasFile('file')) {
            $file = $request->file('file');
            $originalFileName = $file->getClientOriginalName();
            $sanitizedFileName = str_replace(' ', '_', $originalFileName); // Replac
            $fileName = time() . '_' . $sanitizedFileName;
            // Store the file in the 'test' directory in your S3 bucket
            $filePath = 'test/' . $fileName;
            Storage::disk('s3')->put($filePath, file_get_contents($file), 'public');

            // Generate the URL
            $url = env('AWS_ENDPOINT').env('AWS_BUCKET').'/'.$filePath;

            return response()->json(['url' => $url], 200);
        }

        return response()->json(['message' => 'No file uploaded'], 400);
    }
}
