from dataclasses import dataclass

from abra_python.resources.base import BaseResource


class Division(BaseResource):  # Stredisko - Item Category
    def list(self, query=None):
        if query is None:
            query = {'take': 10}

        response = self.requestor.request(
            method='GET',
            endpoint_url='/divisions',
            query=query
        )

        return [
            self.Data(item)
            for item in response
        ]

    def get(self, division_id: str):
        response = self.requestor.request(
            method='GET',
            endpoint_url=f'/divisions/{division_id}'
        )

        return self.Data(response)

    def create(self, payload: dict):
        response = self.requestor.request(
            method='POST',
            endpoint_url='/divisions',
            payload=payload
        )

        return self.Data(response)

    def update(self, division_id: str, payload: dict):
        response = self.requestor.request(
            method='PUT',
            endpoint_url=f'/divisions/{division_id}',
            payload=payload
        )

        return self.Data(response)

    @dataclass
    class Data:
        code: str
        hidden: bool
        id: str
        name: str
        note: str
        objversion: int
        parent_id: str

        def __init__(self, data):
            self.code = data.get("code")
            self.hidden = data.get("hidden", False)
            self.id = data.get("id")
            self.name = data.get("name")
            self.note = data.get("note")
            self.objversion = data["objversion"]
            self.parent_id = data.get("parent_id")
