from scaleway import ALL_REGIONS, ALL_ZONES
from scaleway.instance.v1 import VolumeVolumeType
from scaleway.vpcgw.v2.api import PatRuleProtocol

from .constants import (
    DEFAULT_COMMERCIAL_TYPE,
    DEFAULT_GATEWAY_NAME,
    DEFAULT_GATEWAY_TYPE,
    DEFAULT_NETWORK_NAME,
    DEFAULT_VOLUME_BOOT,
    DEFAULT_VOLUME_SIZE,
    DEFAULT_VOLUME_TYPE,
)

SCHEMA = {
    "type": "object",
    "properties": {
        "resources": {"$ref": "#/resources_def"},
        "config_file": {
            "type": "string",
            "description": "Path to Scaleway config file",
        },
        "profile": {"type": "string", "description": "Scaleway profile name"},
        "region": {
            "enum": ALL_REGIONS,
            "description": "Scaleway private network region",
        },
        "architecture": {
            "enum": ["standalone", "private_network"],
            "default": "private_network",
            "description": "Decide if we deploy instances behind a private network or simply with a public IP",
        },
        "network_name": {"type": "string", "default": DEFAULT_NETWORK_NAME},
        "gateway_type": {"type": "string", "default": DEFAULT_GATEWAY_TYPE},
        "gateway_name": {"type": "string", "default": DEFAULT_GATEWAY_NAME},
        "gateway_zone": {"enum": ALL_ZONES},
        "dynamic_ip_required": {
            "type": "boolean",
            "default": False,
            "description": "Requests public IPs for the instances",
        },
        "enable_ipv6": {
            "type": "boolean",
            "description": "Add a flexible IPv6 address to the instances",
            "default": False,
        },
        "ports_to_open": {
            "type": "array",
            "items": {
                "type": "array",
                "prefixItems": [
                    {"type": "number", "description": "public_port"},
                    {"type": "number", "description": "private_port"},
                    {"type": "string", "description": "role"},
                    {
                        "enum": [prot.value for prot in PatRuleProtocol],
                        "description": "Protocol to open",
                    },
                ],
                "additionalItems": False,
                "minItems": 3,
                "maxItems": 4,
            },
            "description": "List of ports to open on the gateway, only works if architecture is 'private_network'",
        },
    },
    "additionalProperties": False,
    # "required": ["resources"],
    "resources_def": {
        "title": "Resource",
        "type": "object",
        "properties": {
            "machines": {"type": "array", "items": {"$ref": "#/machine"}},
            "networks": {"type": "array", "items": {"type": "string"}},
        },
        "additionalProperties": False,
        # "required": ["machines", "networks"],
    },
    "machine": {
        "title": "ScalewayInstance",
        "type": "object",
        "properties": {
            "zone": {"enum": ALL_ZONES},
            "commercial_type": {
                "type": "string",
                "default": DEFAULT_COMMERCIAL_TYPE,
            },
            "roles": {"type": "array", "items": {"type": "string"}},
            "name": {"type": "string"},
            "image": {"type": "string"},
            "volumes": {"$ref": "#/volumes"},
            "enable_ipv6": {"type": "boolean"},
            "protected": {"type": "boolean"},
            "number": {"type": "number"},
        },
        "required": ["number", "roles"],
        "additionalProperties": False,
    },
    "networks": {"type": "array", "items": {"type": "string"}},
    "volumes": {
        "type": "array",
        "items": {
            "type": "object",
            "properties": {
                "size": {"type": "number", "default": DEFAULT_VOLUME_SIZE},
                "boot": {"type": "boolean", "default": DEFAULT_VOLUME_BOOT},
                "volume_type": {
                    "enum": [vol.value for vol in VolumeVolumeType],
                    "default": DEFAULT_VOLUME_TYPE,
                },
            },
        },
    },
}
