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. Himadri Phukan",
        "position": "Professor & HOD",
        "department": "Business Studies",
        "qualifications": "",
        "image_filename": "Himadri.jpg"
    },
    {
        "name": "Ms. Neha Pal",
        "position": "Assistant Professor",
        "department": "Business Studies",
        "qualifications": "",
        "image_filename": "Neha.jpg"
    },
    {
        "name": "Mr. Gaurav Kumar Yadav",
        "position": "Assistant Professor",
        "department": "Business Studies",
        "qualifications": "",
        "image_filename": "Gaurav.jpg"
    },
    {
        "name": "Mr. Narullah Abbas Jari",
        "position": "Assistant Professor",
        "department": "Business Studies",
        "qualifications": "",
        "image_filename": "Noorullah.jpg"
    },
    {
        "name": "Mr. Javed Ali",
        "position": "Assistant Professor",
        "department": "Business Studies",
        "qualifications": "",
        "image_filename": "Javed.jpg"
    },
    {
        "name": "Ms. Akansha Mishra",
        "position": "Assistant Professor",
        "department": "Business Studies",
        "qualifications": "",
        "image_filename": "Akansha.jpg"
    },
    {
        "name": "Ms. Shelly Sharma",
        "position": "Assistant Professor",
        "department": "Business Studies",
        "qualifications": "",
        "image_filename": "Shelly.jpg"
    },
]

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

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