from __future__ import annotations

from dataclasses import asdict, dataclass, field
from pathlib import Path


@dataclass(slots=True)
class MountMapping:
    source: str
    target: str
    kind: str
    read_only: bool = False

    def to_dict(self) -> dict:
        return asdict(self)


@dataclass(slots=True)
class ServiceDefinition:
    name: str
    image: str | None = None
    restart: str | None = None
    env_files: list[str] = field(default_factory=list)
    mounts: list[MountMapping] = field(default_factory=list)

    def to_dict(self) -> dict:
        return asdict(self)


@dataclass(slots=True)
class ComposeProject:
    name: str
    root_path: str
    compose_files: list[str] = field(default_factory=list)
    services: list[ServiceDefinition] = field(default_factory=list)
    named_volumes: list[str] = field(default_factory=list)
    backup_paths: list[str] = field(default_factory=list)

    def to_dict(self) -> dict:
        return {
            "name": self.name,
            "root_path": self.root_path,
            "compose_files": self.compose_files,
            "services": [service.to_dict() for service in self.services],
            "named_volumes": self.named_volumes,
            "backup_paths": self.backup_paths,
        }

    @property
    def service_names(self) -> list[str]:
        return [service.name for service in self.services]


DEFAULT_COMPOSE_FILENAMES = {
    "docker-compose.yml",
    "docker-compose.yaml",
    "compose.yml",
    "compose.yaml",
}


def normalize_path(path: Path) -> str:
    return str(path.resolve())
