#!/bin/bash
source "${HOME}/.config/xrstats/config"
DBFILE="$RECHNUNGEN_DIR/index.db"

get_field()    { printf "%s" "$2" | grep -m1 "^${1}=" | cut -d= -f2-; }
sql()          { printf "'%s'" "$(printf '%s' "$1" | sed "s/'/''/g")"; } #'
to_real()      { [ -z "$1" ] && echo "NULL" || printf '%s' "$1" | tr -d ' \t' | sed 's/,/./'; }
to_int()       { [ -z "$1" ] && echo "NULL" || printf '%s' "$1"; }
to_bool()      { [ "$1" = "true" ] && echo 1 || echo 0; }
parse_fmtver() { printf '%s' "$1" | grep -oE '[0-9]+\.[0-9]+' | head -1; }

MODE="full"
[ "$1" = "--diff" ] && MODE="diff"

if [ "$MODE" = "full" ]; then
    rm -f "$DBFILE"
    sqlite3 "$DBFILE" "
    CREATE TABLE invoices (
        id              INTEGER PRIMARY KEY AUTOINCREMENT,
        folder          TEXT NOT NULL UNIQUE,
        seller          TEXT,
        invoice_id      TEXT,
        issue_date      TEXT,
        receipt_date    TEXT,
        buyer_ref       TEXT,
        payable_amount  REAL,
        currency        TEXT,
        format_version  REAL,
        created_at      TEXT,
        updated_at      TEXT,
        processed_by    TEXT,
        reviewed        INTEGER,
        note            TEXT,
        status          TEXT,
        type            TEXT,
        versions        INTEGER,
        invoice_type    INTEGER,
        due_date        TEXT,
        leitweg_id      TEXT,
        seller_vat_id   TEXT,
        seller_street   TEXT,
        seller_zip      TEXT,
        seller_city     TEXT,
        seller_country  TEXT,
        buyer_name      TEXT,
        buyer_street    TEXT,
        buyer_zip       TEXT,
        buyer_city      TEXT,
        buyer_country   TEXT,
        net_amount      REAL,
        vat_rate        REAL,
        tax_amount      REAL,
        payment_means   TEXT,
        iban            TEXT,
        bic             TEXT,
        payment_terms   TEXT,
        sub_budget_code TEXT,
        source_xml      TEXT,
        reviewed_text   TEXT,
        retention_years INTEGER,
        disposal_action TEXT,
        fileplan_link   TEXT,
        version_count   INTEGER,
        current_version TEXT,
        ssaso_beleg     INTEGER,
        ssaso_art       INTEGER,
        ssaso_man_nr    INTEGER,
        ssaso_anw_nr    INTEGER,
        ssaso_buchschl  INTEGER,
        ssaso_belegart  TEXT,
        ssaso_buchungskreis TEXT
    );"
fi

flush_to_database() {
	sqlite3 "$DBFILE" "INSERT OR REPLACE INTO invoices (
		folder, seller, invoice_id,
		issue_date, receipt_date, buyer_ref,
		payable_amount, currency, format_version,
		created_at, updated_at, processed_by,
		reviewed, note,
		status, type, versions,
		invoice_type, due_date, leitweg_id, seller_vat_id,
		seller_street, seller_zip, seller_city, seller_country,
		buyer_name, buyer_street, buyer_zip, buyer_city, buyer_country,
		net_amount, vat_rate, tax_amount,
		payment_means, iban, bic, payment_terms,
		sub_budget_code, source_xml, reviewed_text,
		retention_years, disposal_action, fileplan_link,
		version_count, current_version
	) VALUES $INSERT_SQL_VALUES;"
	INSERT_SQL_VALUES=""
}

INSERT_SQL_VALUES=""
COUNTER=0

for DIR in "$RECHNUNGEN_DIR"/*/; do

	COUNTER="$(( COUNTER + 1 ))"
	echo "[$COUNTER] Ingesting $DIR ..."
	
    [ -d "$DIR" ] || continue
    DIR_FILE="${DIR}.directory"
    [ -f "$DIR_FILE" ] || continue

    FOLDER_NAME=$(basename "$DIR")

    if [ "$MODE" = "diff" ]; then
        sqlite3 "$DBFILE" "SELECT COUNT(*) FROM invoices WHERE folder=$(sql "$FOLDER_NAME");" | grep -q '^0$' || continue
    fi

    DIR_FILE_CONTENT="$(cat "$DIR_FILE")"

    SELLER=$(get_field Seller "$DIR_FILE_CONTENT")
    ID=$(get_field ID "$DIR_FILE_CONTENT")
    ISSUE_DATE=$(get_field IssueDate "$DIR_FILE_CONTENT")
    RECEIPT_DATE=$(get_field ReceiptDate "$DIR_FILE_CONTENT")
    BUYER_REF=$(get_field BuyerReference "$DIR_FILE_CONTENT")
    [ -z "$BUYER_REF" ] && BUYER_REF=$(get_field BudgetCode "$DIR_FILE_CONTENT")
    AMOUNT=$(get_field PayableAmount "$DIR_FILE_CONTENT")
    CURRENCY=$(get_field PayableCurrency "$DIR_FILE_CONTENT")
    FORMAT_VERSION=$(parse_fmtver "$(get_field FormatVersion "$DIR_FILE_CONTENT")")
    CREATED_AT=$(get_field CreatedAt "$DIR_FILE_CONTENT")
    UPDATED_AT=$(get_field UpdatedAt "$DIR_FILE_CONTENT")
    PROCESSED_BY=$(get_field ProcessedBy "$DIR_FILE_CONTENT")
    REVIEWED=$(get_field Reviewed "$DIR_FILE_CONTENT")
    NOTE=$(get_field Note "$DIR_FILE_CONTENT")
    ICON=$(get_field Icon "$DIR_FILE_CONTENT")
    INVOICE_TYPE=$(get_field InvoiceType "$DIR_FILE_CONTENT")
    DUE_DATE=$(get_field DueDate "$DIR_FILE_CONTENT")
    LEITWEG_ID=$(get_field LeitwegId "$DIR_FILE_CONTENT")
    SELLER_VAT=$(get_field SellerVatId "$DIR_FILE_CONTENT")
    SELLER_STREET=$(get_field SellerStreet "$DIR_FILE_CONTENT")
    SELLER_ZIP=$(get_field SellerZip "$DIR_FILE_CONTENT")
    SELLER_CITY=$(get_field SellerCity "$DIR_FILE_CONTENT")
    SELLER_COUNTRY=$(get_field SellerCountry "$DIR_FILE_CONTENT")
    BUYER_NAME=$(get_field BuyerName "$DIR_FILE_CONTENT")
    BUYER_STREET=$(get_field BuyerStreet "$DIR_FILE_CONTENT")
    BUYER_ZIP=$(get_field BuyerZip "$DIR_FILE_CONTENT")
    BUYER_CITY=$(get_field BuyerCity "$DIR_FILE_CONTENT")
    BUYER_COUNTRY=$(get_field BuyerCountry "$DIR_FILE_CONTENT")
    NET_AMOUNT=$(get_field NetAmount "$DIR_FILE_CONTENT")
    VAT_RATE=$(get_field VatRate "$DIR_FILE_CONTENT")
    TAX_AMOUNT=$(get_field TaxAmount "$DIR_FILE_CONTENT")
    PAYMENT_MEANS=$(get_field PaymentMeans "$DIR_FILE_CONTENT")
    IBAN=$(get_field IBAN "$DIR_FILE_CONTENT")
    BIC=$(get_field BIC "$DIR_FILE_CONTENT")
    PAYMENT_TERMS=$(get_field PaymentTerms "$DIR_FILE_CONTENT")
    SUB_BUDGET_CODE=$(get_field SubBudgetCode "$DIR_FILE_CONTENT")
    SOURCE_XML=$(get_field SourceXml "$DIR_FILE_CONTENT")
    REVIEWED_TEXT=$(get_field ReviewedText "$DIR_FILE_CONTENT")
    RETENTION_YEARS=$(get_field RetentionYears "$DIR_FILE_CONTENT")
    DISPOSAL_ACTION=$(get_field DisposalAction "$DIR_FILE_CONTENT")
    FILEPLAN_LINK=$(get_field FileplanLink "$DIR_FILE_CONTENT")
    VERSION_COUNT=$(get_field VersionCount "$DIR_FILE_CONTENT")
    CURRENT_VERSION="$(get_field CurrentVersion "$DIR_FILE_CONTENT")"

    case "$ICON" in
        folder-green)  STATUS="Reviewed" ;;
        folder-red)    STATUS="Open" ;;
        folder-yellow) STATUS="In Progress" ;;
        folder-grey)   STATUS="Cancelled" ;;
        *)             STATUS="$ICON" ;;
    esac

	CURRENT_VERSION_PATH="$DIR/$CURRENT_VERSION"
    TYPE="Analog"
	if [ -n "$CURRENT_VERSION" ] && [ -f "$CURRENT_VERSION_PATH" ] ; then
		# In case the current version of the file is known query it directly
		case "$CURRENT_VERSION_PATH" in
			*.xml)
				if head -n 30 "$XML_FILE" | grep -qI "CrossIndustryInvoice\|Invoice-2\|ubl:Invoice" ; then
					TYPE="XRechnung"
				fi
				;;
			*.pdf)
				if grep -qia "zugferd\|factur-x\|crossindustryinvoice" "$CURRENT_VERSION_PATH" ; then
					TYPE="ZUGFeRD"
				fi
				;;
		esac
	else
		# In case it is not known inspect the files in the directory
		if [ -n "$CURRENT_VERSION" ] ; then 
			echo "ERROR: The current version file at $CURRENT_VERSION_PATH does not exist! Falling back to inspecting the directory."
		fi
	    while IFS= read -r -d '' XML_FILE; do
	        head -n 30 "$XML_FILE" | grep -qI "CrossIndustryInvoice\|Invoice-2\|ubl:Invoice"2>/dev/null && { TYPE="XRechnung"; break; }
	    done < <(find "$DIR" -maxdepth 1 ! -name ".*" -iname "*.xml" -print0)
	    
	    if [ "$TYPE" = "Analog" ]; then
	        while IFS= read -r -d '' PDF_FILE; do
	            grep -qia "zugferd\|factur-x\|crossindustryinvoice" "$PDF_FILE" 2>/dev/null && { TYPE="ZUGFeRD"; break; }
	        done < <(find "$DIR" -maxdepth 1 ! -name ".*" -iname "*.pdf" -print0)
	    fi
	fi

    VERSIONS=$(find "$DIR" -maxdepth 1 ! -name ".*" -iname "*.pdf" | wc -l)

	[ -z "$INSERT_SQL_VALUES" ] || INSERT_SQL_VALUES="$INSERT_SQL_VALUES,"
	INSERT_SQL_VALUES="$INSERT_SQL_VALUES(
        $(sql "$FOLDER_NAME"), $(sql "$SELLER"), $(sql "$ID"),
        $(sql "$ISSUE_DATE"), $(sql "$RECEIPT_DATE"), $(sql "$BUYER_REF"),
        $(to_real "$AMOUNT"), $(sql "$CURRENCY"), $(to_real "$FORMAT_VERSION"),
        $(sql "$CREATED_AT"), $(sql "$UPDATED_AT"), $(sql "$PROCESSED_BY"),
        $(to_bool "$REVIEWED"), $(sql "$NOTE"),
        $(sql "$STATUS"), $(sql "$TYPE"), $VERSIONS,
        $(to_int "$INVOICE_TYPE"), $(sql "$DUE_DATE"), $(sql "$LEITWEG_ID"), $(sql "$SELLER_VAT"),
        $(sql "$SELLER_STREET"), $(sql "$SELLER_ZIP"), $(sql "$SELLER_CITY"), $(sql "$SELLER_COUNTRY"),
        $(sql "$BUYER_NAME"), $(sql "$BUYER_STREET"), $(sql "$BUYER_ZIP"), $(sql "$BUYER_CITY"), $(sql "$BUYER_COUNTRY"),
        $(to_real "$NET_AMOUNT"), $(to_real "$VAT_RATE"), $(to_real "$TAX_AMOUNT"),
        $(sql "$PAYMENT_MEANS"), $(sql "$IBAN"), $(sql "$BIC"), $(sql "$PAYMENT_TERMS"),
        $(sql "$SUB_BUDGET_CODE"), $(sql "$SOURCE_XML"), $(sql "$REVIEWED_TEXT"),
        $(to_int "$RETENTION_YEARS"), $(sql "$DISPOSAL_ACTION"), $(sql "$FILEPLAN_LINK"),
        $(to_int "$VERSION_COUNT"), $(sql "$CURRENT_VERSION")
    )"

    if [ "$(( COUNTER % 20 ))" -eq 0 ] ; then
		flush_to_database
	fi
done

flush_to_database

COUNT="$(sqlite3 "$DBFILE" "SELECT COUNT(*) FROM invoices;")"
echo "Done: $DBFILE ($COUNT rows)"
