from django.core.management.base import BaseCommand
from colleges.models import College, FacultySection, Faculty
from django.conf import settings
import os

FACULTY_DATA = [
    {
        "name": "Dr Harihara Sudan",
        "position": "Professor & Principal (Pharmacy)",
        "department": "Allied Sciences",
        "qualifications": "",
        "image_filename": "Hari.jpg"
    },
    {
        "name": "Prof. Sonia Sharma",
        "position": "Professor & Principal (Nursing)",
        "department": "Allied Sciences",
        "qualifications": "",
        "image_filename": "Sonia.jpg"
    },
    {
        "name": "Dr. Sandeep Darbari",
        "position": "Associate Professor & HOD (Pharmacy)",
        "department": "Allied Sciences",
        "qualifications": "",
        "image_filename": "Sandeep.jpg"
    },
    {
        "name": "Ms. Mansi Mourya",
        "position": "Assistant Professor",
        "department": "Allied Sciences",
        "qualifications": "",
        "image_filename": "Manshi.jpg"
    },
    {
        "name": "Mr. Vishal Baliyan",
        "position": "Assistant Professor",
        "department": "Allied Sciences",
        "qualifications": "",
        "image_filename": "Vishal.jpg"
    },
    {
        "name": "Ms. Vinita Pandey",
        "position": "Assistant Professor",
        "department": "Allied Sciences",
        "qualifications": "",
        "image_filename": "Vinita.jpg"
    },
    {
        "name": "Ms. Kajal Kandari",
        "position": "Assistant Professor",
        "department": "Allied Sciences",
        "qualifications": "",
        "image_filename": "Kajal.jpg"
    },
    {
        "name": "Ms. Rajat Saini",
        "position": "Assistant Professor",
        "department": "Allied Sciences",
        "qualifications": "",
        "image_filename": "Rajat.jpg"
    },
    {
        "name": "Ms. Sofia Ansari",
        "position": "Assistant Professor",
        "department": "Allied Sciences",
        "qualifications": "",
        "image_filename": "Sofiya.jpg"
    },
]

class Command(BaseCommand):
    help = 'Automate Our Faculty data for Roorkee College of Allied Sciences.'

    def handle(self, *args, **options):
        college_slug = 'roorkee-college-of-allied-sciences'
        try:
            college = College.objects.get(slug=college_slug)
        except College.DoesNotExist:
            self.stdout.write(self.style.ERROR(f'College with slug "{college_slug}" not found.'))
            return

        faculty_section, created = FacultySection.objects.get_or_create(
            college=college,
            defaults={
                'title': 'Our Faculty',
                'description': 'Meet our esteemed faculty members.'
            }
        )
        if not created:
            self.stdout.write(self.style.WARNING('FacultySection already exists. Adding faculty members.'))
        else:
            self.stdout.write(self.style.SUCCESS('FacultySection created.'))

        for faculty_data in FACULTY_DATA:
            image_path = os.path.join(settings.BASE_DIR, 'rawimages', faculty_data['image_filename'])
            if not os.path.exists(image_path):
                self.stdout.write(self.style.ERROR(f"Image not found: {image_path}"))
                continue
            with open(image_path, 'rb') as img_file:
                faculty_obj, created = Faculty.objects.get_or_create(
                    faculty_section=faculty_section,
                    name=faculty_data['name'],
                    defaults={
                        'position': faculty_data['position'],
                        'department': faculty_data['department'],
                        'qualifications': faculty_data['qualifications'],
                        'image': faculty_data['image_filename']
                    }
                )
                if created:
                    faculty_obj.image.save(faculty_data['image_filename'], img_file)
                    faculty_obj.save()
                    self.stdout.write(self.style.SUCCESS(f"Added faculty: {faculty_data['name']}"))
                else:
                    self.stdout.write(self.style.WARNING(f"Faculty already exists: {faculty_data['name']}"))
        self.stdout.write(self.style.SUCCESS('Faculty data automation complete.'))
