"""ITL schema sub-module."""

import re
from collections import UserDict
from json import loads
from pathlib import Path

from jsonschema import validate

SCHEMA_FOLDER = Path(__file__).parent / 'schema'


class ItlJsonSchema(UserDict):
    """ITL JSON schema object."""

    def __init__(self, fname: str | Path):
        self.fname = Path(fname)
        self.data = loads(self.fname.read_text(encoding='utf-8'))

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

    def __repr__(self) -> str:
        return f'{self.__class__.__name__}> {self}'

    @property
    def name(self) -> str:
        """Schema file name."""
        return self.fname.stem

    @property
    def title(self) -> str:
        """Schema title description."""
        return self.data.get('title', '(undefined)')

    @property
    def version(self) -> int:
        """Schema version.

        At the moment this value is extracted from the title property.

        """
        if 'version' in self.data:
            return int(self.data['version'])

        for version in re.findall(r'v(\d+)', self.title):
            return int(version)

        return 0

    def validate(self, itl: dict) -> None:
        """Validate ITL JSON content."""
        validate(instance=itl, schema=self.data)


MAJIS_ITL_SCHEMAS = [
    ItlJsonSchema(SCHEMA_FOLDER / 'jsoc-itl-schema.json'),
    ItlJsonSchema(SCHEMA_FOLDER / 'majis-itl-schema.json'),
]
