# !/usr/bin/env python
# Copyright 2025 NetBox Labs, Inc.
"""Diode NetBox Plugin - Models."""
from urllib.parse import urlparse

from django.core.exceptions import ValidationError
from django.db import models
from django.urls import reverse
from netbox.models import NetBoxModel


def diode_target_validator(target):
    """Diode target validator."""
    try:
        parsed_target = urlparse(target)

        if parsed_target.scheme not in ["grpc", "grpcs"]:
            raise ValueError("target should start with grpc:// or grpcs://")
    except ValueError as exc:
        raise ValidationError(exc)


class Setting(NetBoxModel):
    """Setting model."""

    diode_target = models.CharField(max_length=255, validators=[diode_target_validator])

    class Meta:
        """Meta class."""

        verbose_name = "Settings"
        verbose_name_plural = "Settings"

    def __str__(self):
        """Return string representation."""
        return ""

    def get_absolute_url(self):
        """Return absolute URL."""
        return reverse("plugins:netbox_diode_plugin:settings")



