from dataclasses import dataclass

from abra_python.resources.base import BaseResource


class Address(BaseResource):
    def list(self, take: int = 10) -> list:
        response = self.requestor.request(
            method='GET',
            endpoint_url='/addresses',
            query={'take': take}
        )

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

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

        return self.Data(response)

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

        return self.Data(response)

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

        return self.Data(response)

    @dataclass
    class Data:
        city: str
        classid: str
        displayname: str
        id: str
        objversion: int
        officialcity: str
        officialhousenumber: str
        officialstreet: str
        postcode: str
        shortaddress: str
        street: str
        zip: str

        def __init__(self, data):
            self.city = data.get("city")
            self.classid = data.get("classid")
            self.displayname = data.get("displayname")
            self.id = data.get("id")
            self.objversion = data.get("objversion")
            self.officialcity = data.get("officialcity")
            self.officialhousenumber = data.get("officialhousenumber")
            self.officialstreet = data.get("officialstreet")
            self.postcode = data.get("postcode")
            self.shortaddress = data.get("shortaddress")
            self.street = data.get("street")
            self.zip = data.get("zip")
