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

from pydantic import HttpUrl, ValidationInfo, field_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
    url: HttpUrl

    published_date: Optional[datetime] = None

    unique_id: Optional[UUID] = None

    @field_validator("unique_id")
    def generate_unique_id(cls, v: Optional[UUID], info: ValidationInfo) -> UUID:
        values = info.data

        if v is None:
            key_values = [
                values["name"],
                values["url"],
            ]

            v = generate_deterministic_uuid(key_values)

        return v

    def __str__(self) -> str:
        return self.name


#
# OUTGOING RELATIONSHIPS
#


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

    __relationshiptype__: ClassVar[str] = "EXPLOIT_EXPLOITS_VULNERABILITY"
