<?php

namespace Convoy\Repositories\Proxmox\Server;

use Convoy\Models\Server;
use Webmozart\Assert\Assert;
use Convoy\Repositories\Proxmox\ProxmoxRepository;

class ProxmoxFirewallRepository extends ProxmoxRepository
{
    public function updateOptions(array $payload)
    {
        Assert::isInstanceOf($this->server, Server::class);

        $response = $this->getHttpClient()
            ->withUrlParameters([
                'node' => $this->node->cluster,
                'server' => $this->server->vmid,
            ])
            ->put('/api2/json/nodes/{node}/qemu/{server}/firewall/options', $payload)
            ->json();

        return $this->getData($response);
    }

    public function getIpsets()
    {
        Assert::isInstanceOf($this->server, Server::class);

        $response = $this->getHttpClient()
            ->withUrlParameters([
                'node' => $this->node->cluster,
                'server' => $this->server->vmid,
            ])
            ->get('/api2/json/nodes/{node}/qemu/{server}/firewall/ipset')
            ->json();

        return $this->getData($response);
    }

    public function createIpset(string $name, string $comments = 'Generated by Convoy')
    {
        Assert::isInstanceOf($this->server, Server::class);

        $response = $this->getHttpClient()
            ->withUrlParameters([
                'node' => $this->node->cluster,
                'server' => $this->server->vmid,
            ])
            ->post('/api2/json/nodes/{node}/qemu/{server}/firewall/ipset', [
                'name' => $name,
                'comment' => $comments,
            ])
            ->json();

        return $this->getData($response);
    }

    public function deleteIpset(string $name)
    {
        Assert::isInstanceOf($this->server, Server::class);

        $response = $this->getHttpClient()
            ->withUrlParameters([
                'node' => $this->node->cluster,
                'server' => $this->server->vmid,
                'ipset' => $name,
            ])
            ->delete('/api2/json/nodes/{node}/qemu/{server}/firewall/ipset/{ipset}')
            ->json();

        return $this->getData($response);
    }

    public function getLockedIps(string $ipset)
    {
        Assert::isInstanceOf($this->server, Server::class);

        $response = $this->getHttpClient()
            ->withUrlParameters([
                'node' => $this->node->cluster,
                'server' => $this->server->vmid,
                'ipset' => $ipset,
            ])
            ->get('/api2/json/nodes/{node}/qemu/{server}/firewall/ipset/{ipset}')
            ->json();

        return $this->getData($response);
    }

    public function lockIp(string $ipset, string $address, string $comments = 'Generated by Convoy')
    {
        Assert::isInstanceOf($this->server, Server::class);

        $response = $this->getHttpClient()
            ->withUrlParameters([
                'node' => $this->node->cluster,
                'server' => $this->server->vmid,
                'ipset' => $ipset,
            ])
            ->post('/api2/json/nodes/{node}/qemu/{server}/firewall/ipset/{ipset}', [
                'cidr' => $address,
                'nomatch' => false,
                'comment' => $comments,
            ])
            ->json();

        return $this->getData($response);
    }

    public function unlockIp(string $ipset, string $address)
    {
        Assert::isInstanceOf($this->server, Server::class);

        $response = $this->getHttpClient()
            ->withUrlParameters([
                'node' => $this->node->cluster,
                'server' => $this->server->vmid,
                'ipset' => $ipset,
                'address' => $address,
            ])
            ->delete('/api2/json/nodes/{node}/qemu/{server}/firewall/ipset/{ipset}/{address}')
            ->json();

        return $this->getData($response);
    }
}
