<?php

use Daite\Coopdigital\Http\Controllers\ControladorCodigoVerificacion;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Route;

use Daite\Coopdigital\Http\Controllers\ControladorInicioSesion;
use Daite\Coopdigital\Http\Controllers\ControladorConsultas;
use Daite\Coopdigital\Mail\CorreoRegistro;
use Illuminate\Support\Facades\Mail;
use Daite\Coopdigital\Mail\CorreoTransferencia;
use Daite\Coopdigital\Mail\CorreoRecuperarContrasena;

/*
|--------------------------------------------------------------------------
| API Routes
|--------------------------------------------------------------------------
|
| Here is where you can register API routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| is assigned the "api" middleware group. Enjoy building your API!
|
*/

Route::middleware('auth:sanctum')->get('/user', function (Request $request) {
    return $request->user();
});


Route::prefix('api')->middleware('api')->group(function() {
    Route::post('/iniciarSesion', [ControladorInicioSesion::class, 'iniciarSesion']);
    Route::post('/consulta', [ControladorConsultas::class, 'consulta']);
    Route::get('/traerConexiones', [ControladorConsultas::class, 'traerConexiones']);
    Route::post('/enviarCodigoVerificacion', [ControladorCodigoVerificacion::class, 'enviarCodigoVerificacion']);

    Route::get('correo/transferencia', function (Request $request) {

        $datosCorreo = [

            'nombreUsuario' => $request->nombreUsuario,

            'correo' => $request->correo,

            'cuentaOrigen' => $request->cuentaOrigen,
            'cuentaDestino' => $request->cuentaDestino,
            'monto' => $request->monto,
            'concepto' => $request->concepto

        ];

        Mail::to($request->correo)->send(new CorreoTransferencia($datosCorreo));

        return 1;

    });

    Route::get('correo/recuperarContrasena', function(Request $request) {

        $datosFormulario = [
    
        'correo' => $request->correo,
        'cedula' => $request->cedula,
        'usuario' => $request->usuario,
        'contrasena' => $request->contrasena
    
        ];
    
        Mail::to($datosFormulario['correo'])->send(new CorreoRecuperarContrasena($datosFormulario));
    
        return 1;
    
    });

    Route::get('correo/inicioSesion', function (Request $request) {
        //TODO: Eliminar cuando la app de rio grande se actualice
        return 1;
    });

    Route::get('correo/registro', function (Request $request) {
        if (!trim($request->correo)) return 0;
        $datosCorreo = [

            'nombreUsuario' => $request->nombreUsuario,
            'correo' => $request->correo,

        ];

        Mail::to($request->correo)->send(new CorreoRegistro($datosCorreo));

        return 1;

    });
});
