<?php

namespace Tricks\CampaignMonitor;

use CS_REST_Transactional_SmartEmail;
use Illuminate\Support\ServiceProvider;
use Tricks\CampaignMonitor\Console\Commands\ListCampaignMonitorMails;

class CampaignMonitorServiceProvider extends ServiceProvider
{
    protected $defer = false;

    public function boot()
    {
        $this->mergeConfigFrom(__DIR__ . '/../config/campaignmonitor.php', 'campaignmonitor');

        $this->publishes([
            __DIR__ . '/../config/campaignmonitor.php' => config_path('campaignmonitor.php'),
        ]);

        $this->loadRoutesFrom(__DIR__ . '/../routes/api.php');

        if ($this->app->runningInConsole()) {
            $this->commands([
                ListCampaignMonitorMails::class
            ]);
        }
    }

    public function register()
    {
        $this->app->singleton(CampaignMonitor::class, function () {
            $campaignMonitorTransactional = new CS_REST_Transactional_SmartEmail('', [
                'api_key' => config('campaignmonitor.transactional.api_key')
            ]);

            return new CampaignMonitor($campaignMonitorTransactional);
        });

        $this->app->alias(CampaignMonitor::class, 'campaignmonitor');
    }
}
