from django.utils.translation import gettext_lazy as _

from oscar.apps.order import reports as oscar


class OrderLinesReportCSVFormatter(oscar.ReportCSVFormatter):
    filename_template = "order-lines-%s-to-%s.csv"

    def header_row(self):
        return [
            _("Product"),
            _("UPC"),
            _("Order number"),
            _("Date placed"),
            _("Quantity"),
            _("Unit Price (excl. tax)"),
            _("Unit Price (incl. tax)"),
            _("Total excl. tax."),
            _("Tax"),
            _("Total incl. tax."),
            _("Country"),
        ]

    def get_row_for_line(self, line):
        return [
            line.title,
            getattr(line.product, "upc", _("Unknown")),
            line.order.number,
            line.order.date_placed.strftime("%d-%m-%Y"),
            line.quantity,
            line.unit_price_excl_tax,
            line.unit_price_incl_tax,
            line.line_price_excl_tax,
            line.line_price_tax,
            line.line_price_incl_tax,
            (
                line.order.shipping_address.country.pk
                if line.order.shipping_address is not None
                else _("Unknown")
            ),
        ]

    def generate_csv(self, response, lines):
        writer = self.get_csv_writer(response)
        writer.writerow(self.header_row())

        for line in lines:
            writer.writerow(self.get_row_for_line(line))

    # pylint: disable=arguments-differ
    def filename(self, **kwargs):
        return self.filename_template % (kwargs["start_date"], kwargs["end_date"])


class OrderLinesReportHTMLFormatter(oscar.ReportHTMLFormatter):
    filename_template = "dashboard/reports/order_lines_report.html"


class ProductSalesReportCSVFormatter(oscar.ReportCSVFormatter):
    filename_template = "order-lines-%s-to-%s.csv"

    def header_row(self):
        return [_("Product"), _("Amount")]

    def make_row(self, product):
        return [product.get_title(), product.sale_count]

    def generate_csv(self, response, products):
        writer = self.get_csv_writer(response)
        writer.writerow(self.header_row())

        for product in products:
            writer.writerow(self.make_row(product))

    # pylint: disable=arguments-differ
    def filename(self, **kwargs):
        return self.filename_template % (kwargs["start_date"], kwargs["end_date"])


class ProductSalesReportHTMLFormatter(oscar.ReportHTMLFormatter):
    filename_template = "dashboard/reports/partials/product_sales_report.html"
