<?php

declare(strict_types=1);

use Illuminate\Support\Facades\Route;
use App\Http\Controllers\ImpersonateController;
use App\Http\Controllers\Tenant\IndexController;
use App\Http\Controllers\Auth\VerificationController;
use App\Http\Controllers\Auth\ResetPasswordController;
use App\Http\Controllers\Auth\ForgotPasswordController;
use Stancl\Tenancy\Middleware\InitializeTenancyByDomain;
use Stancl\Tenancy\Middleware\PreventAccessFromCentralDomains;

/*
|--------------------------------------------------------------------------
| Tenant Routes
|--------------------------------------------------------------------------
|
| Here you can register the tenant routes for your application.
| These routes are loaded by the TenantRouteServiceProvider.
|
| Feel free to customize them however you want. Good luck!
|
*/

Route::group([
    'middleware' => [
        'api',
        InitializeTenancyByDomain::class,
        PreventAccessFromCentralDomains::class,
    ],
    'prefix' => 'api/auth',
], function () {
    Route::post('/login', [AuthController::class, 'login'])->name('tenant.auth.login');
    Route::post('/logout', [AuthController::class, 'logout']);
    Route::get('/refresh', [AuthController::class, 'refresh']);
    Route::get('/user', [AuthController::class, 'user']);
    Route::post('/register', [RegisterController::class, 'register']);

    Route::post('password/phone', [ForgotPasswordPhoneController::class, 'sendResetCodePhoneNumber']);
    Route::post('password/resend', [ForgotPasswordPhoneController::class, 'resendResetCodePhoneNumber']);
    Route::post('password/code', [ResetPasswordPhoneController::class, 'code']);
    Route::post('password/reset', [ResetPasswordPhoneController::class, 'reset']);
    
    Route::post('phone/send', [VerificationPhoneController::class, 'send']);
    Route::post('phone/verify', [VerificationPhoneController::class, 'verify']);
    Route::post('phone/resend', [VerificationPhoneController::class, 'resend']);

    Route::get('/{provider}/callback', [SocialAuthController::class, 'handleProviderCallback']);
});

Route::middleware([
    'web',
    InitializeTenancyByDomain::class,
    PreventAccessFromCentralDomains::class,
])->group(function () {
    Route::post('password/email', [ForgotPasswordController::class , 'sendResetLinkEmail'])->name('tenant.password.email');
    Route::post('password/reset', [ResetPasswordController::class, 'reset'])->name('tenant.password.update');
  
    Route::get('email/verify/{id}', [VerificationController::class, 'verify'])->name('tenant.verification.verify');
    Route::post('email/resend/{id}',[ VerificationController::class, 'resend'])->name('tenant.verification.resend');
  
    Route::get('/{any}', [IndexController::class, 'index'])->where('any', '.*')->name('tenant.index');

    Route::get('/impersonate/{token}', ImpersonateController::class);
});
