from datetime import datetime
from typing import Any, ClassVar, Dict, Optional
from uuid import UUID

from pydantic import HttpUrl, validator

from ..node import OntolocyNode
from ..relationship import OntolocyRelationship
from ..utils import generate_deterministic_uuid
from .cve import CVE


class Exploit(OntolocyNode):

    __primaryproperty__: ClassVar[str] = "unique_id"
    __primarylabel__: ClassVar[Optional[str]] = "Exploit"

    name: str
    published_date: Optional[datetime]
    url: HttpUrl

    unique_id: Optional[UUID] = None

    @validator("unique_id", always=True)
    def generate_unique_id(cls, v: Optional[UUID], values: Dict[str, Any]) -> UUID:

        if v is None:

            key_values = [
                values["name"],
                values["url"],
            ]

            v = generate_deterministic_uuid(key_values)

        return v


#
# OUTGOING RELATIONSHIPS
#


class ExploitExploitsVulnerability(OntolocyRelationship):
    source: Exploit
    target: CVE

    __relationshiptype__: ClassVar[str] = "EXPLOIT_EXPLOITS_VULNERABILITY"
