#!/usr/bin/env python
"""Convert XLSX from ŠB to QIF

The QIFs are produced with the intent to import them to Banktivity.

This is configured by a configuration file (~/.invl2qif) which should have a
single section named '3PF' with a single key 'security':
[3PF]
security = foo
"""

import argparse
import configparser
import os
import sys

import pandas as pd

sys.path.append(os.path.expanduser("~/usr/lib/python"))

from common import (  # noqa: E402  # pylint: disable=wrong-import-position
    QIFInvestmentTransaction,
)

CONFIG_FN = "~/.invl2qif"

config = configparser.ConfigParser()
config.read(os.path.expanduser(CONFIG_FN))

security = config["3PF"]["security"]

arg_parser = argparse.ArgumentParser(
    description="Convert ŠB pension fund XLSX statement to QIF for importing "
    "to Banktivity."
)

arg_parser.add_argument("input", type=str, help="The input file")
arg_parser.add_argument("output", type=str, help="The output file")

args = arg_parser.parse_args()

source = pd.read_excel(args.input)

with open(args.output, "w", encoding="utf-8") as output_file:
    output_file.write("!Type:Invst\n")
    for index, row in source.iterrows():
        date = row["Data"]
        amount = row["Įmoka/išmoka"].strip(" €")
        unit_price = row["Vnt. kaina"].strip(" €")
        count = row["Vnt."].strip(" €")
        fee = row["Mokestis"].strip(" €")
        qif_trx = QIFInvestmentTransaction(
            date=date,
            security=security,
            fee=fee,
            amount=amount,
            unit_price=unit_price,
            count=count,
        )
        print(qif_trx)
        qif_trx.write(output_file)
