<?php

use Illuminate\Support\Facades\Response;
use Illuminate\Support\Facades\Route;
use Illuminate\Support\Facades\Storage;

Route::get('/', function () {
    return view('welcome');
});

Route::get('storage/logos/accounts/{filename}', function ($filename) {
    $path = 'logos/accounts/' . $filename;

    if (!Storage::disk('public')->exists($path)) {
        abort(404);
    }

    $file = Storage::disk('public')->get($path);
    $type = Storage::disk('public')->mimeType($path);

    return Response::make($file, 200)->header("Content-Type", $type);
});

Route::get('storage/companies_logo/{filename}', function ($filename) {
    $path = 'companies_logo/' . $filename;

    if (!Storage::disk('public')->exists($path)) {
        abort(404);
    }

    $file = Storage::disk('public')->get($path);
    $type = Storage::disk('public')->mimeType($path);

    return Response::make($file, 200)->header("Content-Type", $type);
});

Route::get('storage/profile_image/{filename}', function ($filename) {
    $path = 'profile_image/' . $filename;

    if (!Storage::disk('public')->exists($path)) {
        abort(404);
    }

    $file = Storage::disk('public')->get($path);
    $type = Storage::disk('public')->mimeType($path);

    return Response::make($file, 200)->header("Content-Type", $type);
});

Route::get('storage/asset_images/{filename}', function ($filename) {
    $path = 'asset_images/' . $filename;

    if (!Storage::disk('public')->exists($path)) {
        abort(404);
    }

    $file = Storage::disk('public')->get($path);
    $type = Storage::disk('public')->mimeType($path);

    return Response::make($file, 200)->header("Content-Type", $type);
});

Route::get('storage/visit_images/{filename}', function ($filename) {
    $path = 'visit_images/' . $filename;

    if (!Storage::disk('public')->exists($path)) {
        abort(404);
    }

    $file = Storage::disk('public')->get($path);
    $type = Storage::disk('public')->mimeType($path);

    return Response::make($file, 200)->header("Content-Type", $type);
});

Route::get('storage/project_files/{filename}', function ($filename) {
    $path = 'project_files/' . $filename;

    if (!Storage::disk('public')->exists($path)) {
        abort(404);
    }

    $file = Storage::disk('public')->get($path);
    $type = Storage::disk('public')->mimeType($path);

    return Response::make($file, 200)->header("Content-Type", $type);
});

Route::get('storage/attendance_images/check-in/{filename}', function ($filename) {
    $path = 'attendance_images/check-in/' . $filename;

    if (!Storage::disk('public')->exists($path)) {
        abort(404);
    }

    $file = Storage::disk('public')->get($path);
    $type = Storage::disk('public')->mimeType($path);

    return Response::make($file, 200)->header("Content-Type", $type);
});

Route::get('storage/attendance_images/check-out/{filename}', function ($filename) {
    $path = 'attendance_images/check-out/' . $filename;

    if (!Storage::disk('public')->exists($path)) {
        abort(404);
    }

    $file = Storage::disk('public')->get($path);
    $type = Storage::disk('public')->mimeType($path);

    return Response::make($file, 200)->header("Content-Type", $type);
});

Route::get('storage/attendance/check-in/{filename}', function ($filename) {
    $path = 'attendance/check-in/' . $filename;

    if (!Storage::disk('public')->exists($path)) {
        abort(404);
    }

    $file = Storage::disk('public')->get($path);
    $type = Storage::disk('public')->mimeType($path);

    return Response::make($file, 200)->header("Content-Type", $type);
});

Route::get('storage/attendance/check-out/{filename}', function ($filename) {
    $path = 'attendance/check-out/' . $filename;

    if (!Storage::disk('public')->exists($path)) {
        abort(404);
    }

    $file = Storage::disk('public')->get($path);
    $type = Storage::disk('public')->mimeType($path);

    return Response::make($file, 200)->header("Content-Type", $type);
});

Route::get('storage/{filename}', function ($filename) {
    $allowedRootFilePrefixes = [
        'account_document_',
        'user_aadhaar_',
        'user_bank_',
        'user_document_',
        'user_pan_',
    ];

    foreach ($allowedRootFilePrefixes as $prefix) {
        if (str_starts_with($filename, $prefix)) {
            if (!Storage::disk('public')->exists($filename)) {
                abort(404);
            }

            $file = Storage::disk('public')->get($filename);
            $type = Storage::disk('public')->mimeType($filename);

            return Response::make($file, 200)->header("Content-Type", $type);
        }
    }

    abort(404);
})->where('filename', '[^/]+');

Route::get('apk_files/{filename}', function ($filename) {
    $path = public_path('apk_files/' . $filename);

    if (!is_file($path)) {
        abort(404);
    }

    return response()->file($path);
})->where('filename', '[^/]+');
