# Copyright (c) 2026, Company and contributors
# For license information, please see license.txt

import frappe
from frappe import _
from frappe.utils import flt


def execute(filters=None):
	"""Main function for report execution"""
	columns = get_columns(filters)
	data = get_data(filters)

	return columns, data


def get_columns(filters=None):
	"""Get report columns definition (Cədvəl 22 — ƏDV - Əvəzləşdirmə hesabatı)."""
	return [
		{
			"label": _("Purchase Invoice"),
			"fieldname": "purchase_invoice",
			"fieldtype": "Link",
			"options": "Purchase Invoice",
			"width": 160
		},
		{
			# Sətir kodu
			"label": _("Line Code"),
			"fieldname": "line_code",
			"fieldtype": "Data",
			"width": 100
		},
		{
			# VÖEN / Adı
			"label": _("VOEN / Name"),
			"fieldname": "voen_name",
			"fieldtype": "Data",
			"width": 240
		},
		{
			# Qaimənin seriyası
			"label": _("Invoice Series"),
			"fieldname": "invoice_series",
			"fieldtype": "Data",
			"width": 120
		},
		{
			# Qaimənin nömrəsi
			"label": _("Invoice Number"),
			"fieldname": "invoice_number",
			"fieldtype": "Data",
			"width": 120
		},
		{
			# Ümumi məbləğ, manatla
			"label": _("Total Amount"),
			"fieldname": "total_amount",
			"fieldtype": "Currency",
			"width": 140
		},
		{
			# ƏDV məbləği, manatla
			"label": _("VAT Amount"),
			"fieldname": "vat_amount",
			"fieldtype": "Currency",
			"width": 130
		},
		{
			# Ödənilmiş ümumi məbləğ, manatla
			"label": _("Paid Total"),
			"fieldname": "paid_total",
			"fieldtype": "Currency",
			"width": 150
		},
		{
			# Ödənilmiş ƏDV məbləği, manatla
			"label": _("Paid VAT"),
			"fieldname": "paid_vat",
			"fieldtype": "Currency",
			"width": 140
		}
	]


def get_data(filters=None):
	"""Submitted Purchase Invoices flagged for VAT offset with a line code.

	Paid amounts follow the Purchase Register logic: Grand Total minus the
	Outstanding amount. Paid VAT is taken proportionally to the paid share.
	"""
	filters = filters or {}

	conditions = (
		"pi.docstatus = 1 AND pi.vat_offset = 1 "
		"AND pi.vat_offset_line_code IS NOT NULL AND pi.vat_offset_line_code != ''"
	)
	params = {}

	if filters.get("company"):
		conditions += " AND pi.company = %(company)s"
		params["company"] = filters["company"]
	if filters.get("from_date"):
		conditions += " AND pi.posting_date >= %(from_date)s"
		params["from_date"] = filters["from_date"]
	if filters.get("to_date"):
		conditions += " AND pi.posting_date <= %(to_date)s"
		params["to_date"] = filters["to_date"]

	rows = frappe.db.sql(f"""
		SELECT
			pi.name AS purchase_invoice,
			pi.posting_date,
			pi.vat_offset_line_code AS line_code,
			pi.supplier_name,
			sup.tax_id,
			pi.etaxes_invoice_series,
			pi.etaxes_invoice_number,
			pi.grand_total,
			pi.outstanding_amount,
			COALESCE(SUM(CASE
				WHEN ptc.account_head = '521.3 - ƏDV' OR acc.account_number = '521.3'
				THEN ptc.tax_amount ELSE 0
			END), 0) AS vat_amount
		FROM
			`tabPurchase Invoice` pi
		LEFT JOIN `tabSupplier` sup ON sup.name = pi.supplier
		LEFT JOIN `tabPurchase Taxes and Charges` ptc ON ptc.parent = pi.name
		LEFT JOIN `tabAccount` acc ON acc.name = ptc.account_head
		WHERE
			{conditions}
		GROUP BY
			pi.name, pi.vat_offset_line_code, pi.supplier_name, sup.tax_id,
			pi.etaxes_invoice_series, pi.etaxes_invoice_number,
			pi.grand_total, pi.outstanding_amount, pi.posting_date
		ORDER BY
			pi.posting_date, pi.name
	""", params, as_dict=1)

	data = []
	for r in rows:
		grand_total = flt(r.grand_total)
		vat_amount = flt(r.vat_amount)
		paid_total = grand_total - flt(r.outstanding_amount)
		paid_vat = vat_amount * (paid_total / grand_total) if grand_total else 0.0

		data.append({
			"purchase_invoice": r.purchase_invoice,
			"posting_date": r.posting_date,
			"line_code": r.line_code,
			"voen_name": f"{(r.tax_id or '').strip()} / {(r.supplier_name or '').strip()}",
			"invoice_series": r.etaxes_invoice_series,
			"invoice_number": r.etaxes_invoice_number,
			"total_amount": grand_total,
			"vat_amount": vat_amount,
			"paid_total": paid_total,
			"paid_vat": paid_vat,
		})

	return data
