<?php

use Illuminate\Support\Facades\Route;
use App\Http\Controllers\AccountController;
use App\Http\Controllers\AttendeeController;
use App\Http\Controllers\BookController;
use App\Http\Controllers\CalendarController;
use App\Http\Controllers\CalendarSettingsController;
use App\Http\Controllers\CardController;
use App\Http\Controllers\DavController;
use App\Http\Controllers\EventController;
use App\Http\Controllers\IcsController;
use App\Http\Controllers\LocationController;
use App\Http\Controllers\SubscriptionController;
use Illuminate\Foundation\Http\Middleware\VerifyCsrfToken;

/**
 *
 * public unauthenticated pages
 */

Route::view('/', 'welcome');

/**
 *
 * starter pages
 * @todo replace these
 */

Route::view('/dashboard', 'dashboard')
    ->middleware(['auth', 'verified'])
    ->name('dashboard');

Route::view('/settings', 'settings')
    ->middleware(['auth', 'verified'])
    ->name('settings');

/**
 *
 * main authentication block
 */

Route::middleware('auth')->group(function ()
{
    /* user account */
    Route::middleware(['web', 'auth'])
    ->prefix('account')
    ->name('account.')
    ->group(function () {

        // landing: send them to the first pane
        Route::get('/', [AccountController::class, 'index'])->name('index');

        // panes
        Route::get('info', [AccountController::class, 'infoForm'])->name('info');
        Route::patch('info', [AccountController::class, 'infoStore'])->name('info.store');

        Route::get('locale', [AccountController::class, 'localeForm'])->name('locale');
        Route::patch('locale', [AccountController::class, 'localeStore'])->name('locale.store');

        Route::get('addresses', [AccountController::class, 'addressesForm'])->name('addresses');
        Route::patch('addresses', [AccountController::class, 'addressesStore'])->name('addresses.store');

        Route::get('password', [AccountController::class, 'passwordForm'])->name('password');
        Route::patch('password', [AccountController::class, 'passwordStore'])->name('password.store');

        Route::get('delete', [AccountController::class, 'deleteForm'])->name('delete');
        Route::get('delete/confirm', [AccountController::class, 'deleteConfirm'])->name('delete.confirm');
        Route::delete('/', [AccountController::class, 'destroy'])->name('destroy');
    });

    /* calendar core */
    Route::middleware('auth')->group(function () {
        // list, create, store, show, edit, update, destroy
        Route::resource('calendar', CalendarController::class)
             ->whereUuid('calendar'); // constrain the {calendar} param
    });

    /* calendar other */
    Route::middleware(['web','auth'])
        ->prefix('calendar')
        ->name('calendar.')
        ->group(function () {

            // settings landing
            Route::get('settings', [CalendarSettingsController::class, 'index'])->name('settings');

            // display preferences
            Route::get('/settings/display', [CalendarSettingsController::class, 'displayForm'])->name('settings.display');
            Route::post('/settings/display', [CalendarSettingsController::class, 'displayStore'])->name('settings.display.store');

            // settings / subscribe to a calendar
            Route::get('settings/subscribe', [CalendarSettingsController::class, 'subscribeForm'])->name('settings.subscribe');
            Route::post('settings/subscribe', [CalendarSettingsController::class, 'subscribeStore'])->name('settings.subscribe.store');

            // settings / create a calendar
            Route::get('settings/create', [CalendarSettingsController::class, 'createForm'])->name('settings.create');
            Route::post('settings/create', [CalendarSettingsController::class, 'createStore'])->name('settings.create.store');

            // remote calendar subscriptions
            Route::resource('subscriptions', SubscriptionController::class)
                 ->except(['show']); // index, create, store, edit, update, destroy

            // calendar settings for a specific calendar instance/container
            Route::get('settings/calendars/{calendarUri}', [CalendarSettingsController::class, 'calendarForm'])
                ->whereUuid('calendarUri')
                ->name('settings.calendars.show');
            Route::patch('settings/calendars/{calendarUri}', [CalendarSettingsController::class, 'calendarStore'])
                ->whereUuid('calendarUri')
                ->name('settings.calendars.update');

            // events
            Route::prefix('{calendar}')->whereUuid('calendar')->group(function () {
                // create & store
                Route::get ('event/create',        [EventController::class, 'create'])->name('event.create');
                Route::post('event',               [EventController::class, 'store' ])->name('event.store');
                // read
                Route::get ('event/{event}',       [EventController::class, 'show'  ])->name('event.show');
                // edit & update
                Route::get ('event/{event}/edit',  [EventController::class, 'edit'  ])->name('event.edit');
                Route::put ('event/{event}',       [EventController::class, 'update'])->name('event.update');
                // delete
                Route::delete('event/{event}',     [EventController::class, 'destroy'])->name('event.destroy');
            });
        });

    // autocomplete suggestions for event locations
    Route::get('/location/suggest', [LocationController::class, 'suggest'])
        ->name('location.suggest');

    Route::get('/attendee/suggest', [AttendeeController::class, 'suggest'])
        ->name('attendee.suggest');

    // address books
    Route::resource('book', BookController::class)
        ->names('book')                       // books.index, books.create, …
        ->parameter('book', 'book');          // {book} binding

    /** contacts inside a book
        nested so urls look like /books/{book}/contacts/{contact} */
    /*Route::resource('book.contacts', CardController::class)
        ->names('book.contacts')
        ->parameter('contacts', 'contact')
        ->except(['index'])                    // you may add an index later
        ->shallow();*/
});

/* Breeze auth routes (login, register, password reset, etc.) */
require __DIR__.'/auth.php';

/*
|--------------------------------------------------------------------------
| SabreDAV endpoint (CalDAV + CardDAV)
|--------------------------------------------------------------------------
| Leave this outside the auth middleware; the SabreLaravelAuthPlugin handles
| authentication for DAV clients.
*/

// default dav handling
Route::match(
    ['GET', 'POST', 'PUT', 'PATCH', 'DELETE', 'OPTIONS', 'HEAD', 'PROPFIND', 'PROPPATCH', 'MKCOL', 'COPY', 'MOVE', 'REPORT', 'LOCK', 'UNLOCK'],
    '/dav/{any?}',
    [DavController::class, 'handle']
)->where('any', '.*')
 ->withoutMiddleware([VerifyCsrfToken::class]);

// our subscriptions
Route::get('/ics/{calendarUri}.ics', [IcsController::class, 'download'])
    ->name('calendar.ics');

// remote subscriptions
Route::middleware(['auth'])->group(function () {
    Route::resource('calendar/subscriptions', SubscriptionController::class)
         ->except(['show']);      // index • create • store • edit • update • destroy
});
