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. Himanshu Verma",
        "position": "Associate Professor & HOD (Computer Science & Engineering)",
        "department": "Smart Computing",
        "qualifications": "",
        "image_filename": "Himanshu.jpg"
    },
    {
        "name": "Dr. Rohit Kumar",
        "position": "Associate Professor & HOD (Computer Application)",
        "department": "Smart Computing",
        "qualifications": "",
        "image_filename": "Rohithod.jpg"
    },
    {
        "name": "Dr. Vipin Kumar Saini",
        "position": "Professor",
        "department": "Smart Computing",
        "qualifications": "",
        "image_filename": "Vipin.jpg"
    },
    {
        "name": "Dr. Ravinder Arya",
        "position": "Professor",
        "department": "Smart Computing",
        "qualifications": "",
        "image_filename": "Ravinder.jpg"
    },
    {
        "name": "Dr. Sulochana",
        "position": "Assistant Professor",
        "department": "Smart Computing",
        "qualifications": "",
        "image_filename": "Sulochna.jpg"
    },
    {
        "name": "Mr. Lalit Mohan Joshi",
        "position": "Assistant Professor",
        "department": "Smart Computing",
        "qualifications": "",
        "image_filename": "Lalit.jpg"
    },
    {
        "name": "Ms. Mrinalinee Singh",
        "position": "Assistant Professor",
        "department": "Smart Computing",
        "qualifications": "",
        "image_filename": "Mrinalinee.jpg"
    },
    {
        "name": "Mr. Bhanu Pratap",
        "position": "Assistant Professor",
        "department": "Smart Computing",
        "qualifications": "",
        "image_filename": "Bhanu.jpg"
    },
    {
        "name": "Mr. Abhishek Parmar",
        "position": "Assistant Professor",
        "department": "Smart Computing",
        "qualifications": "",
        "image_filename": "Abhishekh.jpg"
    },
    {
        "name": "Ms. Pooja Parmar",
        "position": "Assistant Professor",
        "department": "Smart Computing",
        "qualifications": "",
        "image_filename": "Pooja.jpg"
    },
    {
        "name": "Mr. Narayan Jee",
        "position": "Assistant Professor",
        "department": "Smart Computing",
        "qualifications": "",
        "image_filename": "Narayan.jpg"
    },
    {
        "name": "Mr. Rajiv Ranjan Patel",
        "position": "Assistant Professor",
        "department": "Smart Computing",
        "qualifications": "",
        "image_filename": "Rajiv.jpg"
    },
    {
        "name": "Mr. Vikalpa Tyagi",
        "position": "Assistant Professor",
        "department": "Smart Computing",
        "qualifications": "",
        "image_filename": "Vikalpa.jpg"
    },
    {
        "name": "Mr. Mukesh Kumar",
        "position": "Assistant Professor",
        "department": "Smart Computing",
        "qualifications": "",
        "image_filename": "Mukesh.jpg"
    },
]

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

    def handle(self, *args, **options):
        college_slug = 'roorkee-college-of-smart-computing'
        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.'))
