<?php

use App\Configuration;
use App\DataMigration;
use App\ExternalService;
use App\Facade\UserConfig;
use Illuminate\Support\Facades\DB;

class DataMigrationV2_2_0_beta_2 extends DataMigration
{
    public function getVersion()
    {
        return '2.2.0-beta.2';
    }

    public function run($currentVersion)
    {
        DB::transaction(function()
        {
            $this->moveFacebookSettingsToService();
            $this->moveGoogleSettingsToService();
            $this->moveTwitterSettingsToService();
        });
    }

    private function moveFacebookSettingsToService()
    {
        /** @var Configuration $facebookAppID */
        $facebookAppID = Configuration::where(['key' => 'facebook_app_id'])->first();

        /** @var Configuration $facebookAppID */
        $facebookAppSecret = Configuration::where(['key' => 'facebook_app_secret'])->first();

        if (is_null($facebookAppID) || is_null($facebookAppSecret))
        {
            return;
        }

        $externalService = ExternalService::create([
            'service_type' => ExternalService::FACEBOOK,
            'name' => 'Facebook (migrated from settings)',
            'app_id' => encrypt($facebookAppID->value), // app ID needs to be encrypted now
            'app_secret' => $facebookAppSecret->value   // secret is already encrypted
        ]);

        /** @var ExternalService $facebookExternalServiceConfig */
        $facebookExternalServiceConfig = UserConfig::getOrCreateModel('facebook_external_service_id');
        $facebookExternalServiceConfig->value = $externalService->id;
        $facebookExternalServiceConfig->save();

        $facebookAppID->delete();
        $facebookAppSecret->delete();
    }

    private function moveGoogleSettingsToService()
    {
        /** @var Configuration $googleAppID */
        $googleAppID = Configuration::where(['key' => 'google_app_id'])->first();

        /** @var Configuration $facebookAppID */
        $googleAppSecret = Configuration::where(['key' => 'google_app_secret'])->first();

        if (is_null($googleAppID) || is_null($googleAppSecret))
        {
            return;
        }

        $externalService = ExternalService::create([
            'service_type' => ExternalService::GOOGLE,
            'name' => 'Google (migrated from settings)',
            'app_id' => encrypt($googleAppID->value), // app ID needs to be encrypted now
            'app_secret' => $googleAppSecret->value   // secret is already encrypted
        ]);

        /** @var ExternalService $googleExternalServiceConfig */
        $googleExternalServiceConfig = UserConfig::getOrCreateModel('google_external_service_id');
        $googleExternalServiceConfig->value = $externalService->id;
        $googleExternalServiceConfig->save();

        $googleAppID->delete();
        $googleAppSecret->delete();
    }

    private function moveTwitterSettingsToService()
    {
        /** @var Configuration $twitterAppID */
        $twitterAppID = Configuration::where(['key' => 'twitter_app_id'])->first();

        /** @var Configuration $facebookAppID */
        $twitterAppSecret = Configuration::where(['key' => 'twitter_app_secret'])->first();

        if (is_null($twitterAppID) || is_null($twitterAppSecret))
        {
            return;
        }

        $externalService = ExternalService::create([
            'service_type' => ExternalService::TWITTER,
            'name' => 'Twitter (migrated from settings)',
            'app_id' => encrypt($twitterAppID->value), // app ID needs to be encrypted now
            'app_secret' => $twitterAppSecret->value   // secret is already encrypted
        ]);

        /** @var ExternalService $twitterExternalServiceConfig */
        $twitterExternalServiceConfig = UserConfig::getOrCreateModel('twitter_external_service_id');
        $twitterExternalServiceConfig->value = $externalService->id;
        $twitterExternalServiceConfig->save();

        $twitterAppID->delete();
        $twitterAppSecret->delete();
    }
}