import uuid
from datetime import datetime

from pydantic import BaseModel, ConfigDict

from api.models.sbom_entry import Ecosystem


class SBOMEntryCreate(BaseModel):
    package_name: str
    package_version: str | None = None
    ecosystem: Ecosystem
    license_spdx: str | None = None
    is_direct: bool = True
    is_dev: bool = False


class SBOMIngest(BaseModel):
    repo_slug: str
    entries: list[SBOMEntryCreate]


class SBOMEntryRead(BaseModel):
    model_config = ConfigDict(from_attributes=True)

    id: uuid.UUID
    repo_id: uuid.UUID
    snapshot_id: uuid.UUID
    package_name: str
    package_version: str | None = None
    ecosystem: Ecosystem
    license_spdx: str | None = None
    is_direct: bool
    is_dev: bool
    snapshot_at: datetime
    created_at: datetime


class SBOMSnapshotRead(BaseModel):
    model_config = ConfigDict(from_attributes=True)

    id: uuid.UUID
    repo_id: uuid.UUID
    snapshot_at: datetime
    source: str | None = None
    entry_count: int
    created_at: datetime


class SBOMSnapshotDetail(BaseModel):
    model_config = ConfigDict(from_attributes=True)

    id: uuid.UUID
    repo_id: uuid.UUID
    snapshot_at: datetime
    source: str | None = None
    entry_count: int
    created_at: datetime
    entries: list[SBOMEntryRead] = []


class LicenceGroup(BaseModel):
    license_spdx: str | None
    count: int
    repos: list[str]
    is_copyleft: bool


class LicenceReport(BaseModel):
    groups: list[LicenceGroup]
    copyleft_direct_count: int


class SBOMRepoView(BaseModel):
    repo_slug: str
    last_sbom_at: datetime | None = None
    entry_count: int
    entries: list[SBOMEntryRead]
