#!/usr/bin/env python3
# Fixes Hetzner CSV reports by cleaning the CSV and merging project name from PDF

import argparse
import os
import sys

import hetzner_fix_report

if __name__ == '__main__':
    parser = argparse.ArgumentParser(
            description='Fixes Hetzner CSV reports by cleaning the CSV and merging project name from PDF')
    parser.add_argument('-o', '--output', help='Output file path. Render to stdout if unused', default=False)
    parser.add_argument('-f', '--format', help='Format to be used, defaults to CSV', default='csv',
                        choices=['csv', 'pandas'])
    parser.add_argument('-V', '--version', help='Print version number and exit', action='store_true')
    parser.add_argument('base_csv', help='Path to csv base file', nargs='?')
    parser.add_argument('base_pdf', help='Path to pdf base file', nargs='?')

    args = parser.parse_args()

    if args.version:
        print(hetzner_fix_report.__version__)
        sys.exit(0)

    if len(sys.argv) < 3:
        parser.print_help(sys.stderr)
        sys.exit(1)

    # Check if files exists
    for f in [args.base_csv, args.base_pdf]:
        if not os.path.isfile(f):
            print(f"File not found: {f}")
            sys.exit(1)

    df = hetzner_fix_report.hetzner_fix_report(args.base_csv, args.base_pdf)

    if args.format == 'csv':
        if not args.output:
            print(df.to_csv(index=False))
        else:
            df.to_csv(args.output, index=False)
    elif args.format == 'pandas':
        print(df)
    else:
        print(f'Invalid format specified: {args.format}')
