<?php

namespace App\Providers;

use Illuminate\Support\Facades\Config;
use GuzzleHttp\Client;
use Illuminate\Support\ServiceProvider;
use App\Microsoft\Dynamics\CrmAuth;
use App\Microsoft\Dynamics\CrmAuthenticationHeader;
use App\Microsoft\Dynamics\CrmExecuteSoap;
use App\Microsoft\Dynamics\CrmDynamicsPHP;
use Illuminate\Support\Facades\Blade;
use Illuminate\Support\Facades\View;
use Illuminate\Database\Eloquent\Relations\Relation;
use Illuminate\Support\Facades\URL;

class AppServiceProvider extends ServiceProvider
{
    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot()
    {
        // Add Form facade replacement
        if (!class_exists('Form')) {
            class_alias('App\\Support\\Form', 'Form');
        }

        if (config('app.env') === 'production') {
            URL::forceScheme('https');
        }

        // Configure morph map for legacy database values
        Relation::morphMap([
            'user' => 'App\Models\User',
            'task' => 'App\Modules\Bookings\Task',
            'project' => 'App\Modules\Bookings\Project',
            'client' => 'App\Models\Client',
            'customer' => 'App\Models\Customer',
        ]);
    }

    /**
     * Register any application services.
     *
     * @return void
     */
    public function register()
    {
        require_once app_path().'/helpers.php';

        // Removed Kashflow binding - using Xero integration instead

        $this->app->bind('HipChat', function ($app) {
            return new \HipChat\HipChat('72b7d876643c4cb6ca2daaac965876');
        });

        // SugarRest binding removed - dependency asakusuma/sugarcrm-wrapper no longer available
        // $this->app->bind('SugarRest', function ($app) {
        //     if (! config('services.suitecrm.host')) {
        //         return;
        //     }
        //     $sugar = new \Asakusuma\SugarWrapper\Rest();
        //     $sugar->setUrl(config('services.suitecrm.host'));
        //     $sugar->setUsername(config('services.suitecrm.username'));
        //     $sugar->setPassword(config('services.suitecrm.password'));
        //     $sugar->connect();
        //     return $sugar;
        // });

        $this->app->bind('MicrosoftDynamics', function($app) {
          //$crmAuth = new CrmAuth ();
          $url = env('DYNAMICS_URL');
          $username = env('DYNAMICS_CRM_USERNAME');
          $password = env('DYNAMICS_CRM_PASSWORD');
          $crmDynamics = new CrmDynamicsPHP($username, $password, $url);
          return $crmDynamics;
        });
    }
}
