<?php

use App\Http\Controllers\BuscadorController;
use App\Http\Controllers\SaludoController;
use Illuminate\Support\Facades\Route;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use App\Models\Artista;
use App\Models\Playlist;
use App\Models\Track;


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

Route::get('/html/{nombre}', SaludoController::class );

Route::get('/prueba', function(Request $request){
    dd($request);
});

Route::get('/html', function(){
     return '
        <html>
            <body>
                <h1>Página 1</h1>
                <p>Hola a todos!</p>
            </body>
        </html>
    ';
})->name('miprimerhtml');

//Segunda versión usando views

// Si es invokable
Route::get('/html/{nombre}', SaludoController::class);

Route::get('/buscador', [BuscadorController::class, 'mostrarBuscador']);
Route::post('/buscador', [BuscadorController::class, 'procesarFormulario']);



Route::get('/ejer1', function(){
    return DB::table('artists')->get();
});

Route::get('/ejer6', function(Request $request){
    return DB::table('customers')->where('CustomerId', $request->input('id'))->firstOrFail();
});

Route::get('/ejer7', function(Request $request){
    return DB::table('albums')
        ->join('artists', 'albums.ArtistId', '=', 'artists.ArtistId')
        ->get();
});

Route::get('/listado/{artista}', function(Artista $artista){

    $artista = Artista::find(2);

    $albumCreado = $artista->albumes()->create([
        'Title' => "El nuevo album"
    ]);
});

Route::get('/ejer9', function(){
    $artistas = DB::table('artists')
        ->leftJoin('albums', 'artists.ArtistId', '=', 'albums.ArtistId')
        ->whereNull('albums.AlbumId')
        ->select('artists.*')
        ->get();
    return $artistas;
});


Route::get('/listado/{artista}/editar', function(Request $request, Artista $artista){
    $artista->name = $request->input('nombre');
    $artista->save();
    return "Modelo actualizado";
});


Route::get('/listado/{artista}/eliminar', function(Request $request, Artista $artista){
    $artista->delete();
    return "Modelo eliminado";
});


Route::get('/listadoPlaylist', function(){
    $track = Track::find(4);

    return $track->playlists;
});

Route::get('/asociar', function(){
    $track = Track::find(4);
    $playlist = Playlist::find(18);

    return $track->playlists()->attach($playlist);
});

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


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