# management/commands/import_medicines.py
from django.core.management.base import BaseCommand
from ...models import Medicine
import csv

class Command(BaseCommand):
    help = "Import medicines from CSV"

    def handle(self, *args, **kwargs):
        with open("medicine.csv", newline='', encoding="utf-8") as csvfile:
            reader = csv.DictReader(csvfile)
            for row in reader:
                Medicine.objects.update_or_create(
                    name=row["brand name"].strip(),
                    defaults={
                        "generic": row.get("generic", "").strip(),
                        "strength": row.get("strength", "").strip(),
                        "manufacturer": row.get("manufacturer", "").strip(),
                        "dosage_form": row.get("dosage form", "").strip(),  # ✅ new
                    },
                )
        self.stdout.write(self.style.SUCCESS("Medicines imported successfully"))
