<?php

// use GuzzleHttp\Psr7\Request;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\TestController;
use App\Events\Company\BulkUpdateBillableRateEvent;
use App\Events\Company\WorkerRequest\NotificationOnNewAddedWorkerRequestEvent;
use App\Events\Company\Admin\NotificationForApprovedPaymentEvent;
use Illuminate\Support\Facades\DB;
use App\Http\Controllers\PaymentMethodController;
use App\Http\Controllers\Webhooks\StripeWebhookController;
use App\Http\Controllers\PaymentStatusController;
use App\Http\Controllers\StripeConnectController;
use App\Http\Controllers\PaymentProfileController;
/*
------------------
This should be migrated to another api file
 */

/*
|--------------------------------------------------------------------------
| 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::get('/version', function () {
    return app()->version();
});

// Route::post('/test/file-upload', [TestController::class, 'uploadFile']);
// Route::get('/test/file-remove', [TestController::class, 'removeFile']);

    
Route::get('/test', function () {

    // return "test";

    // return App\Models\Worker::select(
    //     'workers.id as worker_id',
    //     'workers.flat_rate as original_rate',
    //     'workers.overhead_charge as overhead_amount',
    //     'workers.worker_id as worker_company_id',
    //     'workers.overhead_type',
    //     'workers.zipcode',
    //     'workers.date_hired',
    //     'companies.service_charge',
    //     'company_burden_rate.burden_rate',
    //     'company_burden_rate.burden_rate_type',
    //     'worker_experiences.worker_id as worker_experience',
    //     // DB::raw('DISTINCT(worker_experiences.worker_id)'),
    // )
    // ->join('worker_experiences', 'worker_experiences.worker_id', 'workers.id')
    // ->join('company_burden_rate', 'company_burden_rate.id', 'worker_experiences.burden_rate_id')
    // ->join('company_users', 'company_users.worker_id', 'workers.id')
    // ->join('companies', 'companies.id', 'company_users.company_id')
    // ->where('companies.id', 1)
    // ->where('worker_experiences.experience_id', 4)
    // ->distinct()
    // ->get();

    // $payload = (array) [
    //     'level_of_experience' => 1,
    //     'companyID' => 1,
    //     'overheadAmount' => 10,
    //     'overheadType' => 'percent',
    // ];

    // event(new BulkUpdateBillableRateEvent($payload)); 

    // $sampleCreatedWorkerRequest = (object)[
    //     'id' => 1,
    //     'company_id' => 1,
    //     'expertise_id' => 1,
    //     'areas_of_work' => array(),
    //     'zipcode_range_id' => 1,
    //     'experience' => 'Journeyman',
    //     'date_start' => '2022-09-21',
    //     'date_end' => '2022-12-30',
    // ]

    // return App\Models\CompanySettings::select(
    //     'company_id',
    // )
    // ->where('key', 'subscribed_zipcode')
    // ->whereIn('value', (array)["94035","94086","94088","94089","94090","95035","95101","95114","95161","95164"])
    // ->get();

    // return App\Models\CompanySettings::get();

    // $zipcodeList = [
    //         "94035",
    //         "94086",
    //         "94088",
    //         "94089",
    //         "94090",
    //         "95035",
    //         "95101",
    //         "95114",
    //         "95161",
    //         "95164"
    // ];
    // $companies = App\Models\Company::with('settings')->whereHas('settings', function ($query) use ($zipcodeList){
    //     return $query
    //     ->where('company_settings.key', 'subscribed_zipcode')
    //     ->whereIn('company_settings.value', $zipcodeList);
    // })
    // ->get();

    // $countHolder = [];

    // foreach($companies as $company){
    //     if(count($company->settings)){
    //         // return count($company->settings);
    //         array_push($countHolder, count($company->settings));
    //     }
    // }

    // return  $countHolder;

    // return $data = App\Models\ZipcodeRange::select('id')->where('zipcode', "94035")->first();

    $workerRequest = App\Models\WorkerRequest::first();

    $zipcodeNotificationData = [
        'id' => $workerRequest->id,
        'company_id' => $workerRequest->company_id,
        'expertise_id' => $workerRequest->expertise_id,
        'experience' => $workerRequest->experience,
        'zipcode' => '95002',
        'zipcode_range_id' => 1,//zipcode where the worker request is posted: based on state and city
        'range' => 5,
        'company_id' => 10,
    ];

    $zipcodeNotificationData;
    event(new NotificationOnNewAddedWorkerRequestEvent($zipcodeNotificationData)); 
    // event(new NotificationOnNewAddedWorkerRequestEvent(2, 2)); // with settings data 

    return 'event triggered';

    // testing view with data (compant)
    // $user_data = [
    //     'company_name' => 'Rendell Pogi',
    // ];
    // return view('email.company.admin-approved-billing-notice-email', compact('user_data'));  

});


/*
|
| UNAUTHENTICATED API's
|
 */

/* COMPANIES API's*/
include_once 'company/unauthenticated.php';
include_once 'company/authentication.php';

Route::prefix('company')->group(function () {
    include_once 'company/password.php';
    include_once 'company/register.php';
    include_once 'company/freeze_account.php';
});

/* ADMIN API's*/
include_once 'admin/unauthenticated.php';
include_once 'admin/authentication.php';

Route::prefix('admin')->group(function () {
    include_once 'admin/password.php';
});

/* JOBSEEKER API's*/
Route::prefix('jobseeker')->group(function () {
    include_once 'jobseeker/authentication.php';
});
include_once 'jobseeker/register.php';
include_once 'jobseeker/password.php';

/* MOBIELE PWA API's*/
include_once 'mobile/authentication.php';
include_once 'mobile/unauthenticated.php';

Route::prefix('miscellaneous')->group(function () {
        include_once 'miscellaneous/states.php';
        include_once 'miscellaneous/states/state-city.php';
    }
);


/*
|
| AUTHENTICATED API's
|
 */
// Conx system with sanctum authentication
Route::middleware(['auth:sanctum'])->group(function () {

    Route::group(['middleware' => ['jobseeker']], function () {
        Route::prefix('jobseeker')->group(function () {
            include_once 'jobseeker/authenticated.php';
            include_once 'jobseeker/subscription.php';
        });
        include_once 'jobseeker/engagement.php';
        include_once 'jobseeker/account.php';
        include_once 'jobseeker/resume.php';
    });
    

    /* COMPANIES API's*/
    Route::prefix('company')->group(function () {
        include_once 'company/authenticated.php';
        include_once 'company/settings.php';
        include_once 'company/worker-request.php';
    });

    include_once 'company/account.php';
    include_once 'company/company.php';
    include_once 'company/experience.php';
    include_once 'company/workers.php';

    include_once 'company/whiteboard.php';
    include_once 'company/cart.php';
    include_once 'company/search.php';

    include_once 'company/loan/loan-requests.php';
    include_once 'company/loan/loan-financials.php';
    include_once 'company/loan/loan-billings.php';

    include_once 'company/borrow/borrow-requests.php';
    include_once 'company/borrow/borrow-financials.php';
    include_once 'company/borrow/borrow-billings.php';

    include_once 'company/jobseeker/applicant.php';
    include_once 'company/jobseeker/resume-search.php';
    
    include_once 'company/users.php';
    include_once 'company/billable-rate.php';
    include_once 'company/subscription.php';
    include_once 'company/notifications.php';
    include_once 'company/reports.php';
    
    /* ADMIN-CONX API's*/
    Route::group(['middleware' => ['conxuser']], function () {

        include_once 'admin/authenticated.php';

        Route::prefix('admin')->group(function () {
            include_once 'admin/account.php';
            include_once 'admin/dashboard.php';
            include_once 'admin/companies.php';
            include_once 'admin/financials.php';
            include_once 'admin/reports.php';
        });

        // superadmin access
        Route::group(['middleware' => ['conxsuperadmin']], function () {
            Route::prefix('admin')->group(function () {
                include_once 'admin/libraries.php';
                include_once 'admin/subscriptions.php';
                include_once 'admin/users.php';
                include_once 'admin/notification.php';
            });
            // include_once 'admin/users.php';
        });
    });


    /* MOBIELE PWA API's*/
    include_once 'mobile/authenticated.php';
    include_once 'mobile/supervisor/overview.php';
    include_once 'mobile/supervisor/tasks.php';


    Route::prefix('mobile')->group(function () {
        include_once 'mobile/worker/overview.php';
        include_once 'mobile/worker/tasks.php';
        include_once 'mobile/worker/profile.php';
        include_once 'mobile/worker/company.php';

        include_once 'mobile/supervisor/supervisor.php';
        include_once 'mobile/supervisor/timelogs.php';
        include_once 'mobile/supervisor/profile.php';

        include_once 'mobile/manager/profile.php';
        include_once 'mobile/manager/timelogs.php';
        include_once 'mobile/manager/manage.php';
        include_once 'mobile/manager/check-supervisor-timelogs.php';
    });

    // Shared stripe/payment route
    Route::post('/payment-method', [PaymentMethodController::class, 'update']);
    Route::get('/payment-method/current', [PaymentMethodController::class, 'current']);
    Route::get('/payment-method/setup-intent', [PaymentMethodController::class, 'createSetupIntent']);
    Route::get('/payment-status', [PaymentStatusController::class, 'show']);

    // Shared stripe-connect route
    Route::prefix('stripe-connect')->group(function () {
        Route::post('onboard', [StripeConnectController::class, 'startOnboarding']);
        Route::get('onboard/verify/{accountId}', [StripeConnectController::class, 'verifyOnboarding']);
        Route::get('dashboard-link', [StripeConnectController::class, 'getExpressDashboardLink']);
    });
    // Payment profile route (authenticated)
     Route::get('/payment-profile/{provider}', [PaymentProfileController::class, 'getAccountByProvider']);


});

// Stripe webhook (unauthenticated, must be publicly accessible)
Route::post('/webhooks/stripe', [StripeWebhookController::class, 'handle']);
