import csv
from django.core.management.base import BaseCommand , CommandError
from backend.core.models import Risk



class Command(BaseCommand):
    help = "Import risks from CSV file"

    def add_arguments(self, parser):
        parser.add_argument('csv_file',type=str, help="CSV file to import risks from")

    def handle(self, *args, **options):
        csv_file_path = options['csv_file']

        with open(csv_file_path, mode="r") as csv_file:
            reader = csv.DictReader(csv_file)
            for row in reader:
                Risk.objects.update_or_create(
                    risk_id=row["Risk ID"],
                    defaults = {
                        'category':row['Category'],
                        'risk_name': row['Risk Name'],
                        'primary_impact': row['Primary Impact'],
                        'secondary_impact': row['Secondary Impact'],
                        'tretiary_impact':row['Tertiary Impact'],
                        'detection_difficulty': row['Detection Difficulty'],
                        'recovery_complexity':row['Recovery Complexity'],
                        'businnes_impact_severity': row['Business Impact Severity']
                    }
                )
        self.stdout.write(self.style.SUCCESS('Risks imported successfully'))