<?php

namespace App\Helpers\Api;

use Illuminate\Support\Facades\Http;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\Log;

class RADb {

  static function get_request($_address, $_parameters) {
    return Http::withBasicAuth(config('security.radb_username'), config('security.radb_password'))->get($_address, $_parameters);
  }

  static function put_request($_address, $_parameters) {
    // Construct and send our request
    return Http::withBasicAuth(config('security.radb_username'), config('security.radb_password'))->put($_address, $_parameters);
  }

  static function get_as_set_attributes() {
    $as_set = env('RADB_ASSET');
    $current_attributes = json_decode(RADb::get_request(config('security.radb_endpoint') . "/radb/as-set/$as_set", [
      'format' => 'json'
    ])->body())->objects->object[0]->attributes->attribute;

    $formatted_attributes = [];
    $formatted_attributes['members'] = [];

    // Isolate existing members attribute.
    foreach($current_attributes as $attribute) {
      if($attribute->name == 'members') {
        $formatted_attributes[$attribute->name] = explode(',', str_replace(' ', '', $attribute->value));
        break;
      }
      else {
        $formatted_attributes[$attribute->name] = $attribute->value;
      }
    }

    return $formatted_attributes;
  }

  static function rewrite_as_set_members($_members) {
    $as_set = env('RADB_ASSET');

    $raw_attributes = json_decode(RADb::get_request(config('security.radb_endpoint') . "/radb/as-set/$as_set", [
      'format' => 'json'
    ])->body())->objects->object[0]->attributes->attribute;

    $attributes = [];

    // Isolate existing members attribute.
    foreach($raw_attributes as $raw) {
        $attributes[$raw->name] = $raw->value;
    }

    // Combine together our members.
    $members_string = implode(', ', array_unique($_members));

    $payload = [
      'objects' => [
        'object' => array([
            'type' => 'as-set',
            'primary-key' => [
              'attribute' => [
                [
                  'name' => 'as-set',
                  'value' => $as_set
                ]
              ] 
            ],
            'source' => [
              'id' => 'radb'
            ],
            'attributes' => [
              'attribute' => [
                [
                  'name' => 'as-set',
                  'value' => $as_set
                ],
                [
                  'name' => 'descr',
                  'value' => 'AS-SET specifically for customers of automated platform.'
                ],
                [
                  'name' => 'members',
                  'value' => $members_string
                ],
                [
                  'name' => 'mnt-by',
                  'value' => 'MAINT-GSLNETWORKS-AU'
                ],
                [
                  'name' => 'changed',
                  'value' => 'peering@gslnetworks.com.au ' . Carbon::now()->format('Ymd')
                ],
                [
                  'name' => 'source',
                  'value' => 'RADB'
                ]
              ]
            ]
        ])
      ]
    ];

    return RADb::put_request(config('security.radb_endpoint') . '/radb/as-set/' . $as_set . '?password=' . env('RADB_CRYPT'), $payload);

  }

}

?>