from __future__ import annotations

import json
from django.core.management.base import BaseCommand

from apps.ocpp.transactions_io import import_transactions


class Command(BaseCommand):
    help = "Import OCPP transactions from a JSON file"

    def add_arguments(self, parser) -> None:
        parser.add_argument(
            "input", help="Path to JSON file generated by export_transactions"
        )

    def handle(self, *args, **options):
        with open(options["input"], "r", encoding="utf-8") as fh:
            data = json.load(fh)
        imported = import_transactions(data)

        self.stdout.write(self.style.SUCCESS(f"Imported {imported} transactions"))
