<?php

use App\Support\NostrAuth;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Route;
use Illuminate\Support\Facades\Session;
use Illuminate\Support\Facades\Storage;
use Spatie\MediaLibrary\MediaCollections\Models\Media;

Route::redirect('/', '/association/profile');

Route::get('dl/{media}', function (Media $media, Request $request) {
    return Storage::disk($media->disk)->download(
        $media->getPathRelativeToRoot(),
        $media->file_name
    );
})
    ->whereNumber('media')
    ->name('dl')
    ->middleware('signed');

Route::get('media/{media}', function (Media $media, Request $request) {
    $conversion = $request->query('conversion');

    if ($conversion && $media->hasGeneratedConversion($conversion)) {
        $path = $media->getPathRelativeToRoot($conversion);
    } else {
        $path = $media->getPathRelativeToRoot();
    }

    return Storage::disk($media->disk)->response(
        $path,
        $media->file_name,
        [
            'Content-Type' => $media->mime_type,
            'Cache-Control' => 'private, max-age=3600',
        ]
    );
})
    ->whereNumber('media')
    ->name('media.signed')
    ->middleware('signed');

Route::post('logout', function () {
    NostrAuth::logout();
    Session::flush();

    return redirect('/');
})->name('logout');

// Association Routes
Route::livewire('/association/profile', 'association.profile')->name('association.profile');
Route::livewire('/association/benefits', 'association.benefits')->name('association.benefits');

Route::livewire('/association/election', 'association.election.index')->name('association.elections');
Route::livewire('/association/election/{election:year}', 'association.election.show')->name('association.election');
Route::livewire('/association/election/admin/{election:year}', 'association.election.admin')->name('association.election.admin');

Route::livewire('/association/members/admin', 'association.members.admin')->name('association.members.admin');

Route::livewire('/association/news', 'association.news')->name('association.news');

Route::livewire('/association/project-support', 'association.project-support.index')->name('association.projectSupport');
Route::livewire('/association/project-support/create', 'association.project-support.form.create')->name('association.projectSupport.create');
Route::livewire('/association/project-support/{projectProposal:slug}', 'association.project-support.show')->name('association.projectSupport.item');
Route::livewire('/association/project-support/edit/{projectProposal:slug}', 'association.project-support.form.edit')->name('association.projectSupport.edit');
