from __future__ import annotations

from typing import Optional, Any, List
from dataclasses import dataclass
from dataclasses import field as dataclass_field
import json


@dataclass
class Item:
    superlocation: str | None = dataclass_field(default_factory=str)
    status: str | None = dataclass_field(default_factory=str)
    availability: str | None = dataclass_field(default_factory=str)
    notes: str | None = dataclass_field(default_factory=str)
    limitation: str | None = dataclass_field(default_factory=str)
    duedate: str | None = dataclass_field(default_factory=str)
    id: str | None = dataclass_field(default_factory=str)
    item_id: str | None = dataclass_field(default_factory=str)
    ilslink: str | None = dataclass_field(default_factory=str)
    number: int | None = dataclass_field(default_factory=int)
    barcode: str | None = dataclass_field(default_factory=str)
    reserve: str | None = dataclass_field(default_factory=str)
    callnumber: str | None = dataclass_field(default_factory=str)
    department: str | None = dataclass_field(default_factory=str)
    locationhref: str | None = dataclass_field(default_factory=str)
    location: str | None = dataclass_field(default_factory=str)

    def from_dict(self, data: dict):
        """Import data from dict"""
        data = data["items"]
        for entry in data:
            for key, value in entry.items():
                setattr(self, key, value)
        return self


@dataclass
class RDS_AVAIL_DATA:
    """Class to store RDS availability data"""

    library_sigil: str = dataclass_field(default_factory=str)
    items: List[Item] = dataclass_field(default_factory=list)

    def import_from_dict(self, data: str):
        """Import data from dict"""
        edata = json.loads(data)
        # library sigil is first key

        self.library_sigil = str(list(edata.keys())[0])
        # get data from first key
        edata = edata[self.library_sigil]
        for location in edata:
            item = Item(superlocation=location).from_dict(edata[location])

            self.items.append(item)
        return self


@dataclass
class RDS_DATA:
    """Class to store RDS data"""

    RDS_SIGNATURE: str = dataclass_field(default_factory=str)
    RDS_STATUS: str = dataclass_field(default_factory=str)
    RDS_LOCATION: str = dataclass_field(default_factory=str)
    RDS_URL: Any = dataclass_field(default_factory=str)
    RDS_HINT: Any = dataclass_field(default_factory=str)
    RDS_COMMENT: Any = dataclass_field(default_factory=str)
    RDS_HOLDING: Any = dataclass_field(default_factory=str)
    RDS_HOLDING_LEAK: Any = dataclass_field(default_factory=str)
    RDS_INTERN: Any = dataclass_field(default_factory=str)
    RDS_PROVENIENCE: Any = dataclass_field(default_factory=str)
    RDS_LOCAL_NOTATION: str = dataclass_field(default_factory=str)
    RDS_LEA: Any = dataclass_field(default_factory=str)

    def import_from_dict(self, data: dict) -> RDS_DATA:
        """Import data from dict"""
        for key, value in data.items():
            setattr(self, key, value)
        return self


@dataclass
class RDS_GENERIC_DATA:
    LibrarySigil: str = dataclass_field(default_factory=str)
    RDS_DATA: List[RDS_DATA] = dataclass_field(default_factory=list)

    def import_from_dict(self, data: str) -> RDS_GENERIC_DATA:
        """Import data from dict"""
        edata = json.loads(data)
        # library sigil is first key
        self.LibrarySigil = str(list(edata.keys())[0])
        # get data from first key
        edata = edata[self.LibrarySigil]
        for entry in edata:
            rds_data = RDS_DATA()  # Create a new RDS_DATA instance
            # Populate the RDS_DATA instance from the entry
            # This assumes that the entry is a dictionary that matches the structure of the RDS_DATA class
            rds_data.import_from_dict(entry)
            self.RDS_DATA.append(rds_data)  # Add the RDS_DATA instance to the list
        return self


@dataclass
class LoksatzData:
    type: Optional[str] = None
    adis_idn: Optional[str] = None
    t_idn: Optional[str] = None
    ktrl_nr: Optional[str] = None
    adis_isil: Optional[str] = None
    adis_sigel: Optional[str] = None
    bib_sigel: Optional[str] = None
    standort: Optional[str] = None
    signatur: Optional[str] = None
    ausleihcode: Optional[str] = None
    sig_katalog: Optional[str] = None
    erwerb_datum: Optional[str] = None
    medientypcode: Optional[str] = None
    bestellart: Optional[str] = None
    faecherstatistik: Optional[str] = None
    exemplar_stat: Optional[str] = None
    so_standort: Optional[str] = None
