<?php

use Illuminate\Support\Facades\Route;

Route::get('/', function () {
    $photoAlbum = \App\Models\PhotoAlbumModel::with('photos')->get();
    foreach ($photoAlbum as $album) {
        $album->url = \Illuminate\Support\Facades\Storage::url($album->url);
        $album->created_at = date('Y-m-d', $album->created_at);
        foreach ($album['photos'] as &$photo) {
            $photo->original = \Illuminate\Support\Facades\Storage::url($photo->original);
            $photo->compressed = \Illuminate\Support\Facades\Storage::url($photo->compressed);
            $photo->watermarked = \Illuminate\Support\Facades\Storage::url($photo->watermarked);
        }
    }
//    exit(json_encode($photoAlbum));
    return view('index', [
        'photo_album' => $photoAlbum,
    ]);
});

Route::get('/o', function () {
    $photoAlbum = \App\Models\PhotoAlbumModel::with('photos')->get();
    foreach ($photoAlbum as $album) {
        $album->url = \Illuminate\Support\Facades\Storage::url($album->url);
        $album->created_at = date('Y-m-d', $album->created_at);
        $album->count = count($album['photos']);
    }
//    exit(json_encode($photoAlbum));
    return view('o-index', [
        'photo_album' => $photoAlbum,
    ]);
});

Route::get('/detail', function () {
    $id =  request()->get('id');
    $photoAlbum = \App\Models\PhotoAlbumModel::with('photos')->where('id', $id)->first();

    $photoAlbum->url = \Illuminate\Support\Facades\Storage::url($photoAlbum->url);
    $photoAlbum->created_at = date('Y-m-d', $photoAlbum->created_at);
    foreach ($photoAlbum['photos'] as $index => &$photo) {
        $photo->original = \Illuminate\Support\Facades\Storage::url($photo->original);
        $photo->compressed = \Illuminate\Support\Facades\Storage::url($photo->compressed);
        $photo->watermarked = \Illuminate\Support\Facades\Storage::url($photo->watermarked);
    }

//    exit(json_encode($photoAlbum));
    return view('detail', [
        'photo_album' => $photoAlbum,
    ]);
});
Route::get('/upload', function() {
    $photoAlbum = \App\Models\PhotoAlbumModel::with('photos')->get();
    return view('upload',[
        'photo_album' => $photoAlbum,
    ]);
});
Route::post('/upload', [\App\Http\Controllers\ImageController::class, 'upload']);
