<?php

namespace App\Providers;

use App\Http\Middleware\MongoMiddleware;
use App\Mail\SesConfigurationSetPlugin;
use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\Mail;
use Illuminate\Support\Facades\Queue;
use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Facades\Schema;

class AppServiceProvider extends ServiceProvider
{
    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot()
    {
        Schema::defaultStringLength(191);
        Queue::before(function ($event) {
            if ($event->connectionName === 'sqs' && _env('APP_ENV') == 'staging') {
                fwrite(STDOUT, 'Processing SQS job ID: '.$event->job->getJobId().PHP_EOL);
                fwrite(STDOUT, 'Processing SQS job body: '.$event->job->getRawBody().PHP_EOL);
            }
        });

        if (env('IS_LOCAL')){
            \URL::forceScheme('https');
        }

        // Automatically add X-SES-CONFIGURATION-SET header to every outgoing email.
        // Works with SMTP (SES SMTP endpoint) — no changes needed at each Mail::send() call.
        $configSet = env('AWS_SES_CONFIGURATION_SET');
        if ($configSet) {
            Mail::getSwiftMailer()->registerPlugin(
                new SesConfigurationSetPlugin($configSet)
            );
        }
    }

    /**
     * Register any application services.
     *
     * @return void
     */
    public function register()
    {
        $this->app->singleton('MongoMiddleware', function ($app) {
            return new MongoMiddleware();
        });
        if(in_array(App::environment(), ['production','staging'])) {
            $this->app->alias('bugsnag.logger', \Illuminate\Contracts\Logging\Log::class);
            $this->app->alias('bugsnag.logger', \Psr\Log\LoggerInterface::class);
        }
    }
}
