<?php

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Route;

use App\Danoral\Dolarizacion\TasaCambio;
use App\Danoral\Condominios\Recibo;
use App\Danoral\Entidades\Entidad;

use App\Http\Resources\User as UserResource;
use App\Http\Controllers\Api\AuthController;
use App\Http\Controllers\Api\Elementos\ElementoSearchController;
use App\Http\Controllers\Api\Entidades\EntidadController;
use App\Http\Controllers\Api\Cuotas\CuotaController;
use App\Http\Controllers\Api\Condominios\CondominioController;
use App\Http\Controllers\Api\Danoral\DanoralController;
use App\Http\Controllers\Api\Transacciones\TransController;
use App\Http\Controllers\Api\Transacciones\TransDTController;
use App\Http\Controllers\Api\Cobranzas\CobroController;
use App\Http\Controllers\Api\Cobranzas\CobroFormaPagoController;
use App\Http\Controllers\Api\Bancos\BancoController;
use App\Http\Controllers\Api\Cobranzas\CobrosListaController;
use App\Http\Controllers\Api\Cobranzas\NotiPagoController;
use App\Http\Controllers\Api\Elementos\ListItemController;
use App\Http\Controllers\Api\EMails\EMailController;
use App\Http\Controllers\Api\Users\UserController;
use App\Http\Controllers\Api\Exec\ExecController;
use App\Http\Resources\NotiPago;

/*
|--------------------------------------------------------------------------
| 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 response()->json(new UserResource($request->user()),200);
    //return $request->user();
});
*/

Route::post('/register-euser',          [AuthController::class, 'registerEntidadUser'])->middleware('auth:sanctum');
Route::post('/login',                   [AuthController::class, 'login']);
Route::get('/logout',                   [AuthController::class, 'logout'])->middleware('auth:sanctum');

Route::middleware('auth:sanctum')->group(function() {
    Route::resource('entidad',              EntidadController::class);
    Route::resource('cobros-lista',         CobrosListaController::class);
    Route::resource('condominio',           CondominioController::class);
    Route::resource('trans',                TransController::class);  
    Route::resource('transdt',              TransDTController::class);  
    Route::resource('notipago',             NotiPagoController::class);  
    Route::resource('banco',                BancoController::class);
    
    Route::get('search',                    [ElementoSearchController::class, 'search']);
    Route::get('cli',                       [ElementoSearchController::class, 'cli']);

    //Exec
    Route::get('exec-listas-cobros-abrir',  [ExecController::class, 'ExecListasCobrosAbrir'])
        ->middleware('can:exec.listas-cobros.abrir');

    //Entidades
    Route::get('entidad-datos',             [EntidadController::class, 'getDatos']);
    Route::get('entidad-dolarizados',       [EntidadController::class, 'getDolarizados']);
    Route::get('entidad-migrar',            [EntidadController::class, 'migrarEdif'])
        ->middleware('can:entidades.migrar');
    
    //users
    Route::get('/user',                     [AuthController::class, 'getUserSession']);
    Route::get('users',                     [UserController::class, 'index']);
    
    //Condominios
    Route::post('condominio-generar',       [CondominioController::class, 'generarCondominio'])
        ->middleware('can:condominios.generar');
    Route::get('condominio-items-jsys',     [CondominioController::class, 'getCondominioItems_jsys']);
        //->middleware('can:condominios.importar');
    Route::get('condominio-dolarizados',    [CondominioController::class, 'getCondominiosDolarizados']);  
    
    //Cuotas
    Route::get('cuotas-entidad',            [CuotaController::class, 'getCuotaE']);
    Route::patch('cuota-alta/{id}',         [CuotaController::class, 'altaCuota'])
        ->middleware('can:cuotas.alta');
    Route::get('cuotas-cobro-jsys',         [CuotaController::class, 'getCobro_jsys']);
    Route::get('cuotas-trans',              [CuotaController::class, 'getCuotasTrans']);
    
    //transacciones
    Route::post('transdt-search',           [TransDTController::class, 'getTransDTSearch' ]);
    Route::get('transdt-balance',           [TransDTController::class, 'getBalance' ]);
    Route::get('trans-types',               [TransController::class, 'getTypeT']);
    Route::get('trans-inicial',             [TransController::class, 'getInicialEntidad']);

    //emails
    Route::get('email-condominio',          [EMailController::class, 'sendCond'])
        ->middleware('can:email.condominio');

    //cobros
    Route::get('cobros',                    [CobroController::class, 'getCobros']);
    Route::get('cobros-view',               [CobroController::class, 'getCobrosView']);
    Route::get('cobros-dt-view',            [CobroController::class, 'getCobrosDTView']);
    Route::get('cobros-users',              [CobroController::class, 'getUsersCobros']);
    Route::post('cobro-procesar',           [CobroController::class, 'procesarCobro'])
        ->middleware('can:cobros.procesar');
    
    //cobrosLista    
    Route::get('cobros-listas',             [CobrosListaController::class, 'getCobrosListas']);
    Route::get('cobros-lista-types',        [CobrosListaController::class, 'getCobrosListaTypes']);
    
    //notipago
    Route::post('notipago-procesar-cobro',  [NotiPagoController::class, 'procesarNotipagoCobro'])
        ->middleware('can:cobros.notipago.procesar');
    Route::get('notipago-cambio-estado',    [NotiPagoController::class, 'cambioEstadoNotipago'])
        ->middleware('can:cobros.notipago.procesar');

    //Bancos
    Route::get('banco-trans',               [BancoController::class, 'getTrans']);
    Route::get('banco-ingresos',            [BancoController::class, 'getIngresos']);
    Route::get('banco-pago-lotes',          [BancoController::class, 'getFileTXT']);

    Route::get('list-item',                 [ListItemController::class, 'getListItem']);

    //Danoral
    Route::get('danoral-clientes',          [DanoralController::class, 'getClientes'])
        ->middleware('can:danoral.clientes');
    Route::get('danoral-facturas',          [DanoralController::class, 'getFacturas'])
        ->middleware('can:danoral.facturacion');



});

//entidades
Route::get('edif-cod/{codEdif}', function ($codEdif) {
    return response()->json(Entidad::getEdif_cod($codEdif),200);
});
Route::get('apto-cod/{idE_edif}/{codApto}', function ($idE_edif, $codApto) {
    return response()->json(Entidad::getApto_cod($idE_edif, $codApto),200);
});
  
Route::get('tasa-cambio-dia',           [TasaCambio::class, 'getTasaDia']);
Route::get('tasa-cambio-fecha',         [TasaCambio::class, 'getTasaFechaR']);
Route::get('tasa-cambio-lista',         [TasaCambio::class, 'getTasaLista']);

Route::get('deuda',                     [CobroController::class, 'getDeuda']);
Route::get('deuda-edif',                [CobroController::class, 'getDeudaEdif']);

//Recibos (relacion de gastos)
Route::get('recibo/{idE}/{ano}/{mes}', function ($idE, $ano, $mes) {
    return (new Recibo($idE, null, $ano, $mes))->getRecibo();
});
Route::get('recibos-cond/{idE}/{ano}/{mes}', function ($idE, $ano, $mes) {
    return Recibo::getRecibosCond($idE, $ano, $mes);
});
Route::get('recibos-apto-demanda/{idE_apto}/{ano}', function ($idE_apto, $ano) {
    return Recibo::getRecibosApto($idE_apto, true, $ano);
});
Route::get('recibos-apto/{idE_apto}/{ano}', function ($idE_apto, $ano) {
    return Recibo::getRecibosApto($idE_apto, false, $ano);
});

//bancos
Route::get('bancos-codigos',            [BancoController::class, 'getBancosCodigos']);
Route::get('bancos-entidad/{idE}', function ($idE) {
    return BancoController::getBancos_Entidad($idE);
});

Route::post('notipago-procesar',        [NotiPagoController::class, 'procesarNotiPago']);

Route::post('cobro-generar',            [CobroController::class, 'generarCobro']);


Route::get('cfp-types',                 [CobroFormaPagoController::class, 'getTypeCFP']);

//pago movil
Route::post('pago-movil',               [CobroFormaPagoController::class, 'pagoMovil']);
