<?php

namespace App\Models\Goliath;

use App\Models\Asns;
use App\Models\Organisation;
use App\Notifications\SlackNotification;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Notification;

class GoliathAsnAttributes extends Model
{
    public $timestamps = false;

    protected $fillable = [
        'asn_id',
        'default_synproxy',
        'default_chain',
        'default_inline',
        'default_heuristics',
        'default_loose'
    ];

    private $changeLog = [];

    private $overwrite = false;

    protected static function booted()
    {

        $change = function ($_m) {

            try {
                $new = $_m->getDirty();
                $old = $_m->getOriginal();
    
                $user = Auth::user();
                $organisation = Organisation::find(Asns::find($_m->asn_id)->organisation_id);
    
                $_m->changeLog = [
                    'Asn' => Asns::find($_m->asn_id)->asn,
                    'User' => $user->name ?? 'NO USER SET',
                    'Organisation' => "$organisation->name",
                    'Inline' => [
                        'Old' => $old === [] ? 'N/A' : ($old['default_inline'] != 1 ? 'On Detect' : 'Inline'),
                        'New' => $new === [] ? 'N/A' : (($new['default_inline'] ?? $old['default_inline']) != 1 ? 'On Detect' : 'Inline')
                    ],
                    'Heuristics' => [
                        'Old' => $old === [] ? 'N/A' : ($old['default_heuristics'] === 0 ? 'Off' : $old['default_heuristics']),
                        'New' => $new === [] ? 'N/A' : (($new['default_heuristics'] ?? $old['default_heuristics']) === 0 ? 'Off' : ($new['default_heuristics'] ?? $old['default_heuristics']))
                    ],
                    'Synproxy' => [
                        'Old' => $old === [] ? 'N/A' : ($old['default_synproxy'] != 1 ? 'Off' : 'On'),
                        'New' => $new === [] ? 'N/A' : (($new['default_synproxy'] ?? $old['default_synproxy']) != 1 ? 'Off' : 'On')
                    ],
                    'Loose' => [
                        'Old' => $old === [] ? 'N/A' : ($old['default_loose'] != 1 ? 'Off' : 'On'),
                        'New' => $new === [] ? 'N/A' : (($new['default_loose'] ?? $old['default_loose'] ?? 0) != 1 ? 'Off' : 'On')
                    ],
                    'Overwrite' => $_m->overwrite
                ];
            }
            catch(\Exception $ex) {
                \Log::error($ex);
            }
        };

        static::creating($change);
        static::updating($change);
        static::deleting($change);

        $done = function ($_m) {

            try {

                $notification = "Mitigation Default Updated\n"
                    . 'ASN: <https://bgp.tools/as/' . $_m->changeLog['Asn'] . '|AS' . $_m->changeLog['Asn'] . '>' . "\n"
                    . 'User: ' . $_m->changeLog['User'] . "\n"
                    . 'Organisation: ' . $_m->changeLog['Organisation'] . "\n"
                    . 'Inline: ' . $_m->changeLog['Inline']['Old'] . ' -> ' . $_m->changeLog['Inline']['New'] . "\n"
                    . 'Heuristics: ' . $_m->changeLog['Heuristics']['Old'] . ' -> ' . $_m->changeLog['Heuristics']['New'] . "\n"
                    . 'Synproxy: ' . $_m->changeLog['Synproxy']['Old'] . ' -> ' . $_m->changeLog['Synproxy']['New'] . "\n"
                    . 'Origin Aware Heuristics: ' . $_m->changeLog['Loose']['Old'] . ' -> ' . $_m->changeLog['Loose']['New'] . "\n";
                
                if($_m->overwrite === true) {
                    $notification = $notification . "This will overwrite existing preferences.\n"; 
                }

                (new GoliathHistory([
                    'type'              => GoliathHistoryType::Default,
                    'history'           => json_encode($_m->changeLog),
                    'organisation_id'   => Asns::find($_m->asn_id)->organisation_id,
                    'user_id'           => Auth::user()->id ?? null
                ]))->save();

                Notification::route('slack', config('creatia.webhook.goliath'))->notify(new SlackNotification($notification));
            }
            catch(\Exception $ex) {
                \Log::error($ex);

                try { \Log::info($notification); } catch (\Exception $i) {}
            }
        };

        static::created($done);
        static::updated($done);
        static::deleted($done);

    }

    public function setOverwrite($overwrite = true) {
        $this->overwrite = $overwrite;
    }
}