# -*- coding: utf-8 -*-

import logging

import inventree.base
import inventree.order

logger = logging.getLogger('inventree')


class Contact(inventree.base.InventreeObject):
    """Class representing the Contact model"""

    URL = 'company/contact/'
    MIN_API_VERSION = 104


class Address(inventree.base.InventreeObject):
    """Class representing the Address model"""

    URL = 'company/address/'
    MIN_API_VERSION = 126


class Company(
    inventree.base.AttachmentMixin,
    inventree.base.ParameterMixin,
    inventree.base.ImageMixin,
    inventree.base.MetadataMixin,
    inventree.base.InventreeObject
):
    """ Class representing the Company database model """

    URL = 'company/'
    MODEL_TYPE = "company"

    def getContacts(self, **kwargs):
        """Return contacts associated with this Company"""
        kwargs['company'] = self.pk
        return Contact.list(self._api, **kwargs)

    def getAddresses(self, **kwargs):
        """Return addresses associated with this Company"""
        kwargs['company'] = self.pk
        return Address.list(self._api, **kwargs)

    def getSuppliedParts(self, **kwargs):
        """
        Return list of SupplierPart objects supplied by this Company
        """
        return SupplierPart.list(self._api, supplier=self.pk, **kwargs)

    def getManufacturedParts(self, **kwargs):
        """
        Return list of ManufacturerPart objects manufactured by this Company
        """
        return ManufacturerPart.list(self._api, manufacturer=self.pk, **kwargs)

    def getPurchaseOrders(self, **kwargs):
        """
        Return list of PurchaseOrder objects associated with this company
        """
        return inventree.order.PurchaseOrder.list(self._api, supplier=self.pk, **kwargs)

    def createPurchaseOrder(self, **kwargs):
        """
        Create (and return) a new PurchaseOrder against this company
        """

        kwargs['supplier'] = self.pk

        return inventree.order.PurchaseOrder.create(
            self._api,
            data=kwargs
        )

    def getSalesOrders(self, **kwargs):
        """
        Return list of SalesOrder objects associated with this company
        """
        return inventree.order.SalesOrder.list(self._api, customer=self.pk, **kwargs)

    def createSalesOrder(self, **kwargs):
        """
        Create (and return) a new SalesOrder against this company
        """

        kwargs['customer'] = self.pk

        return inventree.order.SalesOrder.create(
            self._api,
            data=kwargs
        )

    def getReturnOrders(self, **kwargs):
        """Return list of ReturnOrder objects associated with this company"""
        return inventree.order.ReturnOrder.list(self._api, customer=self.pk, **kwargs)

    def createReturnOrder(self, **kwargs):
        """Create (and return) a new ReturnOrder against this company"""
        kwargs['customer'] = self.pk

        return inventree.order.ReturnOrder.create(self._api, data=kwargs)


class SupplierPart(
    inventree.base.AttachmentMixin,
    inventree.base.ParameterMixin,
    inventree.base.BarcodeMixin,
    inventree.base.BulkDeleteMixin,
    inventree.base.MetadataMixin,
    inventree.base.InventreeObject
):
    """Class representing the SupplierPart database model

    - Implements the BulkDeleteMixin
    """

    URL = 'company/part/'

    def getPriceBreaks(self):
        """ Get a list of price break objects for this SupplierPart """

        return SupplierPriceBreak.list(self._api, part=self.pk)


class ManufacturerPart(
    inventree.base.AttachmentMixin,
    inventree.base.ParameterMixin,
    inventree.base.BulkDeleteMixin,
    inventree.base.MetadataMixin,
    inventree.base.InventreeObject,
):
    """Class representing the ManufacturerPart database model

    - Implements the BulkDeleteMixin
    """

    URL = 'company/part/manufacturer/'
    MODEL_TYPE = "manufacturerpart"

    def getParameters(self, **kwargs):
        """
        GET a list of all ManufacturerPartParameter objects for this ManufacturerPart
        """

        # Support legacy API version which uses a different endpoint
        if self._api.api_version < inventree.base.Parameter.MIN_API_VERSION:
            return ManufacturerPartParameter.list(self._api, manufacturer_part=self.pk, **kwargs)

        return super().getParameters(**kwargs)


class ManufacturerPartParameter(inventree.base.BulkDeleteMixin, inventree.base.InventreeObject):
    """Class representing the ManufacturerPartParameter database model.

    Note: This class was removed in API version 418 and later.
    Ref: https://github.com/inventree/InvenTree/pull/10699
    """

    URL = 'company/part/manufacturer/parameter/'
    MAX_API_VERSION = 428


class SupplierPriceBreak(inventree.base.InventreeObject):
    """ Class representing the SupplierPriceBreak database model """

    URL = 'company/price-break/'
