from _decimal import Decimal
from dataclasses import dataclass
from datetime import datetime
from typing import Optional

from abra_python.resources.base import BaseResource


class ReceivedInvoice(BaseResource):
    def list(self, query: dict = None):
        if query is None:
            query = {'take': 10}

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

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

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

        return self.Data(response)

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

        return self.Data(response)

    @dataclass
    class Data:
        accountdate: datetime
        accountdocqueue_id: str
        accountpresetdef_id: str
        amount: Decimal
        amountwithoutvat: Decimal
        coefficient: int
        constantsymbol_id: str
        country_id: str
        createdat: datetime
        createdby_id: str

        currency_id: str
        currencyrate: Decimal

        description: str
        displayname: str
        docdate: datetime
        docqueue_id: str
        duedate: datetime
        externalnumber: str
        firm_id: str
        firmbankaccount_id: str
        firmoffice_id: str
        id: str
        isaccounted: bool

        localamount: Decimal
        localamountwithoutvat: Decimal
        localamountwithoutvatwithvatdeposit: Optional[Decimal]
        localamountwithvatdeposit: Optional[Decimal]
        localnotpaidamount: Optional[Decimal]
        localpaidamount: Decimal
        localcoefficient: int

        localcreditamount: Decimal
        localcreditamountwithoutvat: Decimal
        localpaidamount: Decimal
        localpaidcreditamount: Decimal
        localroundingamount: Decimal
        objversion: int
        ordnumber: int
        paidamount: Decimal

        period_id: str

        specificsymbol: str
        tradetype: int
        tradetypedescription: str
        variablesymbol: str
        vatadmitdate: datetime
        vatamount: Optional[Decimal]
        vatcountry_id: str
        vatdate: datetime
        vatdocument: bool

        rows: list

        def __init__(self, data):
            self.accountdate = datetime.fromisoformat(data.get("accdate$date").replace("Z", "+00:00"))
            self.accountdocqueue_id = data.get("accdocqueue_id")
            self.accountpresetdef_id = data.get("accpresetdef_id")
            self.amount = Decimal(data.get("amount"))
            self.amountwithoutvat = Decimal(data.get("amountwithoutvat"))
            self.coefficient = data.get("coef")
            self.constantsymbol_id = data.get("constsymbol_id")
            self.country_id = data.get("country_id")
            self.createdat = datetime.fromisoformat(data.get("createdat$date").replace("Z", "+00:00"))
            self.currency_id = data.get("currency_id")
            self.currencyrate = Decimal(data.get("currrate"))
            self.description = data.get("description")
            self.displayname = data.get("displayname")
            self.docdate = datetime.fromisoformat(data.get("docdate$date").replace("Z", "+00:00"))
            self.docqueue_id = data.get("docqueue_id")
            self.duedate = datetime.fromisoformat(data.get("duedate$date").replace("Z", "+00:00"))
            self.externalnumber = data.get("externalnumber")
            self.firm_id = data.get("firm_id")
            self.firmbankaccount_id = data.get("firmbankaccount_id")
            self.firmoffice_id = data.get("firmoffice_id")
            self.id = data.get("id")
            self.isaccounted = data.get('isaccounted', False)
            self.localamount = Decimal(data.get("localamount"))
            self.localamountwithoutvat = Decimal(data.get("localamountwithoutvat"))
            self.localamountwithoutvatwithvatdeposit = (
                Decimal(data['localamountwithoutvatwithvatdeposit'])
                if data.get('localamountwithoutvatwithvatdeposit') is not None else None
            )
            self.localamountwithvatdeposit = (
                Decimal(data['localamountwithvatdeposit'])
                if data.get('localamountwithvatdeposit') is not None else None
            )
            self.localnotpaidamount = (
                Decimal(data.get("localnotpaidamount"))
                if data.get("localnotpaidamount") is not None else None
            )
            self.localpaidamount = Decimal(data.get("localpaidamount"))
            self.localcoefficient = int(data.get("localcoef"))
            self.localcreditamount = Decimal(data.get("localcreditamount"))
            self.localcreditamountwithoutvat = Decimal(data.get("localcreditamountwithoutvat"))
            self.localpaidcreditamount = Decimal(data.get("localpaidcreditamount"))
            self.localroundingamount = Decimal(data.get("localroundingamount"))
            self.objversion = data.get("objversion")
            self.ordnumber = data.get("ordnumber")
            self.paidamount = Decimal(data.get("paidamount"))
            self.period_id = data.get("period_id")
            self.specificsymbol = data.get("specsymbol")
            self.tradetype = data.get("tradetype")
            self.tradetypedescription = data.get('tradetypedescription')
            self.variablesymbol = data.get("varsymbol")
            self.vatadmitdate = datetime.fromisoformat(data.get("vatadmitdate$date").replace("Z", "+00:00"))
            self.vatamount = (
                Decimal(data.get('vatamount')) if data.get('vatamount') is not None else None
            )
            self.vatcountry_id = data.get("vatcountry_id")
            self.vatdate = datetime.fromisoformat(data.get("vatdate$date").replace("Z", "+00:00"))
            self.vatdocument = data.get("vatdocument")
            self.rows = data.get('rows', list)
